【发布时间】:2015-02-24 17:17:51
【问题描述】:
我什至不知道如何用这个问题来表达这个问题,但是就这样吧。我需要能够在同一个 SQL 脚本中循环遍历结果集,并在更多 SQL 中使用结果。
例如
begin
SELECT (SELECT ColumnA, ColumnB from SomeTable) as x
loop through x(
INSERT ColumnA into TableA
INSERT ColumnB into TableB
)
end
但我忘记了这样做的确切方法。我知道我以前在以前的职位上做过,但我在该公司的文件中找不到它的代码。
显然,这是一个非常粗略和基本的示例,我计划对结果集做更多的事情,但我只是举这个例子。
编辑:这是一个更接近的示例,说明我希望这样做以防万一这会有所帮助。
begin
while(select columnA, columnB, columnC, columnD from myTable) as x
begin
INSERT columnA, columnB into TableA
(get newly created ID of TableA - but that's a separate question involving @@IDENTITY)
INSERT NewID, columnC, columnD into TableB
end loop
end
【问题讨论】:
-
我也只是查找了游标,我不记得使用该方法来执行此操作。我对此持开放态度,但我认为我在使用其他东西。
-
光标。来吧,做的不仅仅是伪代码。 MSDN 有示例。
-
查看stackoverflow.com/q/5365629/73226了解如何获取分配的id值。
标签: sql sql-server sql-server-2008 tsql