MsSQL   发布时间:2022-05-16  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了基于ID列表的SQL LOOP INSERT大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。
嘿我有sql编写器阻止.所以这里是我正在做的基于伪代码
int[] ids = SELECT id FROM (table1) WHERE idType = 1 -> SELEcTing a bunch of record ids to work with
FOR(int i = 0; i <= ids.Count(); ++i) -> loop through based on number of records retrieved
{
    INSERT INTO (table2)[col1,col2,col3] select col1,col3 FROM (table1)
    WHERE col1 = ids[i].Value AND idType = 1 -> InserTing into table based on one of the ids in the array

    // More inserts based on Array ID's here
}

这是我想要实现的一个想法,我明白数组在sql中@R_874_8351@,但我在这里列出来解释我的目标.

解决方法

这是你要求的.
declare @IDList table (ID int)

insert into @IDList
SELECT id
FROM table1
WHERE idType = 1

declare @i int
SELEct @i = min(ID) from @IDList
while @i is not null
begin
  INSERT INTO table2(col1,col3) 
  select col1,col3
  FROM table1
  WHERE col1 = @i AND idType = 1

  SELEct @i = min(ID) from @IDList where ID > @i
end

但是如果这是你在循环中所做的一切,你应该真正地使用Barry的答案.

大佬总结

以上是大佬教程为你收集整理的基于ID列表的SQL LOOP INSERT全部内容,希望文章能够帮你解决基于ID列表的SQL LOOP INSERT所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: