如何从另一个存储过程中迭代存储过程结果......没有游标?

问题描述:

我不确定这是否是我应该在 T-SQL 中做的事情,而且我很确定在这种情况下使用迭代"这个词是错误的,因为你永远不应该在 sql 中迭代任何东西.它应该是一个基于集合的操作,对吗?无论如何,这是场景:

I'm not sure if this is something I should do in T-SQL or not, and I'm pretty sure using the word 'iterate' was wrong in this context, since you should never iterate anything in sql. It should be a set based operation, correct? Anyway, here's the scenario:

我有一个存储过程,它返回许多唯一标识符(单列结果).这些 id 是另一个表中记录的主键.我需要在该表中的所有相应记录上设置一个标志.

I have a stored proc that returns many uniqueidentifiers (single column results). These ids are the primary keys of records in a another table. I need to set a flag on all the corresponding records in that table.

如何在不使用游标的情况下执行此操作?对你的 sql 大师来说应该是一件容易的事!

How do I do this without the use of cursors? Should be an easy one for you sql gurus!

这可能不是最有效的,但我会创建一个临时表来保存存储过程的结果,然后在与目标的连接中使用它桌子.例如:

This may not be the most efficient, but I would create a temp table to hold the results of the stored proc and then use that in a join against the target table. For example:

CREATE TABLE #t (uniqueid int)
INSERT INTO #t EXEC p_YourStoredProc

UPDATE TargetTable 
SET a.FlagColumn = 1
FROM TargetTable a JOIN #t b 
    ON a.uniqueid = b.uniqueid

DROP TABLE #t