嘿,我有SQL writer块。所以这是我基于伪代码要做的事情
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, col2, 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中无法使用数组,但在此处列出了它来解释我的目标。
这就是您要的。
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,col2,col3) SELECT col1, col2, col3 FROM table1 WHERE col1 = @i AND idType = 1 select @i = min(ID) from @IDList where ID > @i end
但是,如果您要在循环中完成所有操作,则应改用Barry的答案。