我有2个存储过程:up_proc1和up_proc2。
这是(简化版)up_proc2:
CREATE PROCEDURE dbo.up_proc2
@id_campaign uniqueidentifier, @id_subcampaign uniqueidentifier,
@id_lead uniqueidentifier, @offer NVARCHAR(1000) = NULL
AS
SET NOCOUNT ON
DECLARE @id UNIQUEIDENTIFIER
SELECT @id = id FROM prospects WHERE id_lead = @id_lead
AND id_campaign = @id_campaign AND id_subcampaign = @id_subcampaign
IF @id IS NULL
BEGIN
SET @id = newid ()
INSERT INTO prospects (id, id_campaign, id_subcampaign, id_lead, offer)
values (@id, @id_campaign, @id_subcampaign, @id_lead, @offer)
END
ELSE
BEGIN
UPDATE prospects set offer = @offer WHERE id=@id
END
SELECT @id AS ID
GO
从up_proc1我调用up_proc2。我想要实现的是将up_proc2的@id存储在up_proc1中声明的变量中。这可能不使用输出参数吗?
这就是up_proc1的样子:
CREATE PROCEDURE dbo.up_proc1
AS
SET NOCOUNT ON
DECLARE @fromProc2 UNIQUEIDENTIFIER
-- NOT WORKING
-- select @fromProc2 = exec up_insertProspects [snip]
-- ALSO NOT WORKING
-- exec @fromProc2 = up_insertProspects [snip]
答案 0 :(得分:9)
您可以做的是将输出存储到表变量中:
DECLARE @tmpTable TABLE (ID UNIQUEIDENTIFIER)
INSERT INTO @tmpTable
EXEC dbo.up_proc2 ..........
然后从那里开始并稍后使用该表变量。
答案 1 :(得分:4)
您当然可以将其作为proc2中的输出参数使用,而不会影响C#代码检索最终结果集的方式。
ALTER PROCEDURE dbo.up_proc2
@id_campaign uniqueidentifier,
@id_subcampaign uniqueidentifier,
@id_lead uniqueidentifier,
@offer NVARCHAR(1000) = NULL,
@fromProc2 UNIQUEIDENTIFER = NULL OUTPUT
AS
BEGIN
SET NOCOUNT ON;
...
C#可以忽略新参数,因为它可以为空(但由于单个输出参数比数据读取器更有效,因此您可以考虑更新C#代码以便稍后利用输出参数)。
现在在proc1:
ALTER PROCEDURE dbo.up_proc1
AS
BEGIN
SET NOCOUNT ON;
DECLARE @fromProc2 UNIQUEIDENTIFIER;
EXEC dbo.up_proc2
--... other parameters ...,
@fromProc2 = @fromProc2 OUTPUT;
-- now you can use @fromProc2
END
GO