如何避免使用 Cursor 来实现此伪代码 - SQL Server
CREATE PROCEDURE p_processDataFor @accountId
BEGIN
for each item in
(select * from Accounts where accountId = @accountId and isProcessed = 0)
BEGIN
CASE current row
WHEN has x Condition THEN
exec p_x <Pass all data of current row>
WHEN has y Condition THEN
exec p_y <Pass all data of current row>
WHEN has z Condition THEN
exec p_z <Pass all data of current row>
END
END
END
CREATE PROCEDURE p_processDataFor @accountId
BEGIN
for each item in
(select * from Accounts where accountId = @accountId and isProcessed = 0)
BEGIN
CASE current row
WHEN has x Condition THEN
exec p_x <Pass all data of current row>
WHEN has y Condition THEN
exec p_y <Pass all data of current row>
WHEN has z Condition THEN
exec p_z <Pass all data of current row>
END
END
END
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
通常无法避免循环,因为您正在调用 EXEC,而这不能作为基于 SET 的操作来完成;它必须一一完成。
如果您只是想避免使用 CURSOR,则可以使用 WHILE 循环来实现。
否则,另一个选择是使用 SELECT + FOR XML 语句,该语句将 EXEC 语句作为单个 NVARCHAR(MAX) 语句构建到变量中,然后仅执行该动态 SQL。
You cannot normally avoid looping since you are calling EXEC, which cannot be done as a SET-based operation; it has to be done one by one.
If you just want to avoid CURSOR in general, you can implement it using a WHILE loop.
Otherwise, another option is to use a SELECT + FOR XML statement which builds the EXEC statements as a single NVARCHAR(MAX) statement into a variable, then EXEC just that dynamic SQL.
好的,这个示例只执行条件 X 的插入,但希望向您展示可以继续进行的方式:
Val 是我的表,其中包含要处理的行(在您的示例中为“帐户”)。 T1 和 T2 是当前由
p_x
过程插入/更新的两个表。只是一些示例数据 - 我有 3 行,其中 2 行匹配“条件 x”:
对于您的实际工作,您需要 3 个上述副本 - x、y 和 z 各一个。如果它位于同一个存储过程中,则需要为 @Inter 表使用不同的名称。 合并语句正在被轻微滥用,因为您不能使用OUTPUT 子句,从插入语句引用其他表。但我们使用它是为了捕获从 T1 生成的 IDENTITY 值,以及将插入到其他表中的相应数据。
因此,现在我们将使用表变量 @Inter 进一步插入到 T2 中,并最终更新 Val 以指示行已被处理。如果存在需要插入和获取标识值的表链,则需要引入更多合并语句和表变量。
我们得到了结果:
因此,我们已经执行了条件 X 的所有工作,始终保持代码集为基础。
Okay, this example only does the insert for condition X, but hopefully shows you the way you could proceed:
Val is my table containing rows to be dealt with (in your example, Accounts). T1 and T2 are two tables that are currently inserted into/updated by your
p_x
procedure.Just some sample data - I've got 3 rows, 2 of which match "condition x":
For your actual work, you'd want 3 copies of the above - one for each of x, y and z. If it's inside the same stored proc, you'd need to use a different name for the @Inter table. The merge statement is being slightly abused, because you can't use an OUTPUT clause that references other tables from an insert statement. But we're using that in order to capture the generated IDENTITY values from T1, along with the corresponding data that's going to be inserted into other tables.
So now we'll use the table variable
@Inter
for a further insert into T2, and to eventually update Val to indicate that the rows have been processed. If there's a chain of tables where you need to insert and grab identity values, you'd need to introduce more merge statements and table variables.And we get our results:
So we've performed all of our work for condition X, keeping the code set based throughout.