我有一个带有UPDATE
语句的存储过程,我想更新另一个存储过程的结果中的字段。
第一个存储过程:
ALTER PROCEDURE [dbo].[usp_sproc1]
AS
BEGIN
SET NOCOUNT ON;
SELECT *
FROM Database1.dbo.tbl_dB1table AS t1
INNER JOIN Database2.dbo.tbl_dB2table AS t2 ON t1.field = t2.field
INNER JOIN Database3.dbo.tbl_dB3table AS t3 ON t3.field2 = t1.field2 AND t3.field2 = t2.field2
INNER JOIN Database4.dbo.tbl_dB4table AS t4 ON t4.field3 = t2.field3
WHERE (field5 = 'c') AND (some_date IS NULL)
END
第二个存储过程:
CREATE PROCEDURE [dbo].[usp_ASCRoadAmericaUpdateDateInternal]
AS
BEGIN
SET NOCOUNT ON;
UPDATE Database1.dbo.tbl_dB1table
SET spr1.some_date = GETDATE()
FROM (EXEC dbo.[usp_sproc1]) AS spr1
END
所以我想检索第一个存储过程的结果集,然后使用今天的日期更新Database1.tbl_dB1table
中的some_date字段,但只更新第一个存储过程返回的那些记录。
是否可以在不将第一个存储过程的结果转储到CURSOR
并迭代每个记录的情况下完成此操作?
答案 0 :(得分:2)
CREATE PROCEDURE dbo.usp_sproc2
AS
BEGIN
SET NOCOUNT ON;
UPDATE t1
SET some_date = GETDATE() -- you probably mean CONVERT(DATE, GETDATE())?
FROM Database1.dbo.tbl_dB1table AS t1
INNER JOIN Database2.dbo.tbl_dB2table AS t2
ON t1.field = t2.field
INNER JOIN Database3.dbo.tbl_dB3table AS t3
ON t3.field2 = t1.field2
AND t3.field2 = t2.field2
INNER JOIN Database4.dbo.tbl_dB4table AS t4
ON t4.field3 = t2.field3
WHERE (field5 = 'c') AND (t1.some_date IS NULL);
-------------^^^^^^ what table does this column come from?
END
GO