关于交叉查询语句中 IMAGE字段的问题 再问。

lsj2 2006-08-24 06:24:50
由于报表输出要用到交叉查询语句, 我是这样写的:
select medID,
Image1=(case imageid when 1 then imagedata end),
Image2=(case imageid when 2 then imagedata end),
....
from images
where medID=1
group by medID
imagedata是image类型.
报错:imagedata不包含在任何聚合函数中.

有人给出如下方法:
select medID,
Image1=max(cast( (case imageid when 1 then imagedata end) as binary)), --或者binary(8000)
Image2=max(cast( (case imageid when 2 then imagedata end) as binary)),
....
from images
where medID=1
group by medID

测试语法没有错,但得到的图像数据不对,显示不出来。 该如何写才能得到准确的图像数据?
...全文
253 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
wgsasd311 2006-08-25
  • 打赏
  • 举报
回复
--try
DECLARE @t TABLE(gid int, id int, a image)
INSERT @t SELECT 1, 1, 0x1
UNION ALL SELECT 1, 2, 0x2
UNION ALL SELECT 2, 1, 0x4
UNION ALL SELECT 2, 2, 0x8
UNION ALL SELECT 2, 3, 0x16

declare @tmp table(gid int,a1 image,a2 image,a3 image)
declare @i int,@gid int
declare cur cursor for select gid from @t group by gid
open cur
fetch next from cur into @gid
while @@fetch_status=0
begin
set @i=1
while @i<=3
begin
if @i=1
begin insert into @tmp(gid,a1)select @gid,a from @t where gid=@gid and id=1 end
else
if @i=2 update a set a2=b.a from @tmp a,@t b where b.gid=@gid and b.id=2 and a.gid=@gid
else
update a set a3=b.a from @tmp a,@t b where b.gid=@gid and b.id=3 and a.gid=@gid
set @i=@i+1
end
fetch next from cur into @gid
end
select * from @tmp
deallocate cur

lsj2 2006-08-25
  • 打赏
  • 举报
回复
继续报错:‘在这一子查询或聚合表达式中,text,ntext,image类型无效’?

我是在进行报表打印的时候要用到该语句。 比如打印出来的样子是这样的:

张三的报告
----------------
|张三 |
| |
| 图1 图2 图3 |
|______________|

李四的报告
———————
|李四 |
| |
| 图1 图2 图3 |
| |
|______________|
lsj2 2006-08-25
  • 打赏
  • 举报
回复
谢谢大家的指点,问题已经解决。
zjcxc 2006-08-24
  • 打赏
  • 举报
回复
-- 如果一定要在sql中处理, 可以用这种低效的方法

DECLARE @t TABLE(gid int, id int, a image)
INSERT @t SELECT 1, 1, 0x1
UNION ALL SELECT 1, 2, 0x2
UNION ALL SELECT 2, 2, 0x4

SELECT
[1] = (SELECT TOP 1 a FROM @t WHERE id = 1 AND gid = a.gid),
[2] = (SELECT TOP 1 a FROM @t WHERE id = 2 AND gid = a.gid)
FROM @t A
GROUP BY gid

-- 结果
1 2
------- --------
0x01 0x02
NULL 0x04
zjcxc 2006-08-24
  • 打赏
  • 举报
回复
为什么要对图像做交叉表呢?

如果是前台显示的话, 完全可以直接读取图像数据啊.
华芸智森 2006-08-24
  • 打赏
  • 举报
回复
不要读图象,读指针即可.

22,298

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧