一个表A,有字段id和id_name,现在准备写一个存储过程,参数为SP_inherit(int SourceID,int DestinationID),
实现效果如下:
先从表A中检索id=SourceID,把检索到所有的数据,用DestinationID插入表A中,比如:
A
id id_name
1 test1
1 test2
1 test3
3 test3现在运行SP_inherit(1,2)后,变成
A
id id_name
1 test1
1 test2
1 test3
2 test1
2 test2
2 test3
3 test3
实现效果如下:
先从表A中检索id=SourceID,把检索到所有的数据,用DestinationID插入表A中,比如:
A
id id_name
1 test1
1 test2
1 test3
3 test3现在运行SP_inherit(1,2)后,变成
A
id id_name
1 test1
1 test2
1 test3
2 test1
2 test2
2 test3
3 test3
select DestinationID, id_name from [表A] where id=SourceID不过实在想不到这样做是为什么
id id_name
1 test1
1 test2
1 test3
2 test1
,再次执行该存储过程,将不会正确执行,因为已经有2 test1记录了
select DestinationID, id_name from [表A] where id=SourceID and id_name not in (select id_name from [表A] where id=DestinationID )
@DestinationID intasinsert into #temp1 select @DestinationID,id_name from #temp1 where id=@SourceID