高手进,如何把一个表的不同记录的image字段合并成导入另外一个表的一个image字段

zachary7833 2007-03-04 11:17:39
把一个表的不同记录的image字段合并成导入另外一个表的一个image字段
多谢!!!
...全文
410 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
gc_ding 2007-03-08
  • 打赏
  • 举报
回复
CREATE TABLE A(pkid int identity(1,1) primary key, myclass nvarchar(16), data image)
INSERT A
select 'A', 0x111111 union all
select 'A', 0x222222 union all
select 'A', 0x333333 union all
select 'A', 0x444444 union all
select 'B', 0x111111 union all
select 'B', 0x222222
GO

-- 处理
SELECT pkid, flag = 0, myclass, data
INTO #
FROM A

DECLARE @pkid int, @myclass nvarchar(16), @myclass1 nvarchar(16),@p1 binary(16), @p2 binary(16),@SP nvarchar(10)

SET @SP = ''

DECLARE tb CURSOR LOCAL FOR SELECT pkid, myclass, TEXTPTR(data) FROM # ORDER BY myclass, pkid
OPEN tb FETCH tb INTO @pkid, @myclass, @p2

WHILE @@FETCH_STATUS = 0
BEGIN
IF @myclass1 = @myclass
BEGIN
UPDATETEXT #.data @p1 NULL 0 @SP
UPDATETEXT #.data @p1 NULL 0 #.data @p2
END
ELSE
BEGIN
UPDATE # SET flag = 1, @p1 = @p2, @myclass1 = @myclass
WHERE pkid = @pkid
END
FETCH tb INTO @pkid, @myclass, @p2
END

CLOSE tb
DEALLOCATE tb

SELECT id=(SELECT COUNT(1) FROM # WHERE flag = 1 AND myclass<a.myclass) +1,myclass, data
FROM # a
WHERE flag = 1
GO

DROP TABLE #, A

--结果:
/*
id myclass data
-------------------------------------------------
1 A 0x111111222222333333444444
2 B 0x111111222222
*/
zachary7833 2007-03-07
  • 打赏
  • 举报
回复
我顶
zachary7833 2007-03-05
  • 打赏
  • 举报
回复
新开一贴,写了具体的例子,高手来接分啊:
http://community.csdn.net/Expert/topic/5377/5377943.xml?temp=.1513788

我举个例子来说明下我的意图:
--Table tbsrc,
create table tbsrc(id int identity(1,1) primary key,myclass nvarchar(16),data image);
id myclass data
---- ------ --------
1 A 0x111111
2 A 0x222222
3 A 0x333333
4 A 0x444444
5 B 0x111111
6 B 0x222222

只举例,实际上要处理的不只6条记录,2个类。

create table tbdest(id int identity(1,1) primary key,myclass nvarchar(16),data image);
目标:
合并到Talbe tbdest的结果:
id myclass data
---- ------ --------
1 A 0x111111222222333333444444
2 B 0x111111222222
scckobe 2007-03-05
  • 打赏
  • 举报
回复
游标的用法如下:
create table test_table1(id int identity(1,1) primary key,data image)
go
create table test_table2(id int identity(1,1) primary key,data image)
go

declare @curorsImage image
declare @AllIamge image
declare cursor_Field cursor for
select data
where data is not null

open cursor_Field
--如果何必我不懂,只告诉你游标怎么使用
fetch next from cursor_Field into @curorsImage

while @@fetch_status=0
begin
fetch next from cursor_Field into @curorsImage
end
close cursor_Field
deallocate cursor_Field
zachary7833 2007-03-05
  • 打赏
  • 举报
回复
多谢楼上各位弟兄姐妹帮助.我需要多条记录的合并,UPDATETEXT我也想过,就是没办法嵌入select语句来遍历表记录同时更新另外一个表的image字段,是不是要用游标来做啊?我游标没写过不会写啊,哪位弟兄姐妹指教下要怎么做.或是有什么其他办法能实现吗?

To dawugui(潇洒老乌龟):
我的合并意思就是把字段的值接起来,如hinco(桃色德鲁依)所做.

To hinco(桃色德鲁依):
我需要多条记录的合并,@from,@target需要能分别遍历两个表的image指针,该怎么写呢?
Hinco 2007-03-05
  • 打赏
  • 举报
回复
我给个简单的例子,多记录合并的话还需LZ自己写存储过程
create table test_table1(id int identity(1,1) primary key,data image)
create table test_table2(id int identity(1,1) primary key,data image)
insert into test_table1 select 0x111111 union all select 0x222222

select * from test_table1

DECLARE @target binary(16),@from binary(16)
insert into test_table2 select data from test_table1 where id=1
select @from=TEXTPTR(data) from test_table1 where id=2
select @target=TEXTPTR(data) from test_table2 where id=1
UPDATETEXT test_table2.data @target null 0 test_table1.data @from

select * from test_table2

drop table test_table1
drop table test_table2

(所影响的行数为 2 行)

id data
---- ---------------
1 0x111111
2 0x222222

(所影响的行数为 2 行)


(所影响的行数为 1 行)

id data
------ ------------
1 0x111111222222

(所影响的行数为 1 行)
dawugui 2007-03-05
  • 打赏
  • 举报
回复
把一个表的不同记录的image字段合并成导入另外一个表的一个image字段

你这里的合并是什么意思啊?
withcsharp 2007-03-05
  • 打赏
  • 举报
回复
Update 操作 UPDATETEXT 参数
替换现有数据 指定一个非空 insert_offset 值、非零 delete_length 值和要插入的新数据。
删除现有数据 指定一个非空 insert_offset 值、非零 delete_length 值。不指定要插入的新数据。
插入新数据 指定 insert_offset 值、为零的 delete_length 值和要插入的新数据。
withcsharp 2007-03-05
  • 打赏
  • 举报
回复
UPDATETEXT
更新现有 text、ntext 或 image 字段。使用 UPDATETEXT 在适当的位置更改 text、ntext 或 image 列的一部分。使用 WRITETEXT 来更新和替换整个 text、ntext 或 image 字段。

zachary7833 2007-03-04
  • 打赏
  • 举报
回复
最好用存储过程写啊。

22,209

社区成员

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

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