直接从存储过程插入输出参数

时间:2015-04-20 08:45:06

标签: sql sql-server tsql

我有一个存储过程,它返回一对输出参数 - ID和计算值。是否可以使用带有insert语句的触发器,直接插入这两个值?像这样的东西

CREATE TRIGGER Trig_FirstTable ON SecondTable AFTER UPDATE
AS 
BEGIN
INSERT INTO FirstTable (OtherID, OtherValue)
VALUES (@otherID, @otherValue)
FROM StoredProcedure inserted.ID, inserted.Value, @otherID OUTPUT, @otherValue OUTPUT
END

2 个答案:

答案 0 :(得分:1)

根据MSDN documentation,你可以使用INSERT和EXEC。 他们举了以下例子:

--INSERT...EXECUTE procedure example
INSERT author_sales EXECUTE get_author_sales

但我认为你的存储过程需要一个SELECT语句来返回数据而不是只填充输出参数。

答案 1 :(得分:0)

您可以从SP插入:

drop procedure uspTest
GO
create procedure uspTest
AS
select 1 as id, 'x' as val
union all 
select 2,'y'
GO

drop table #temp
GO
create table #temp (
    id int, 
    val char(1) 
)
GO

insert into #temp (id,val)
EXECUTE uspTest
GO

select
    *
from #temp

但您无法选择列的子集,因此如果您将来向SP添加更多输出,此方法显然会失败:

insert into #temp (id)
EXECUTE uspTest

另一种方法是将SP结果存储在变量中,然后将它们用于插入。

相关问题