我有一个存储过程,输出一个select语句,如下所示:
select case when count(*) > 10 then ''A1'' else ''B1'' end as id, name, address from database.dbo.student
现在我想编写一个存储过程,它将select
语句作为字符串输入,并返回所有上述输出。我不确定在哪里分配输出变量。
ALTER PROCEDURE dbo.selectAttributes
@select_statement Nvarchar(MAX),
@id Nvarchar(255) OUT,
@name nvarchar(255) OUT,
@address nvarchar(255) OUT
AS
BEGIN
DECLARE @query nvarchar(MAX)
SET @query = @select_statement
EXEC sp_executesql @query,
N'@select_statement Nvarchar(MAX),
@idOUT Nvarchar(255) OUTPUT,
@nameOUT nvarchar(255) OUTPUT,
@addressOUT nvarchar(255) OUTPUT',
@select_statement, @id OUTPUT, @name OUTPUT, @address OUTPUT
END
答案 0 :(得分:1)
我认为你很接近:
ALTER PROCEDURE dbo.selectAttributes (
@select_statement Nvarchar(MAX),
@id Nvarchar(255) OUTPUT,
@name nvarchar(255) OUTPUT,
@address nvarchar(255) OUTPUT
) AS
BEGIN
EXEC sp_executesql @select_statement,
N'@id Nvarchar(255) OUTPUT, @name nvarchar(255) OUTPUT, @address nvarchar(255) OUTPUT',
@id = @id OUTPUT,
@name = @name OUTPUT,
@address = @address OUTPUT
END;
换句话说:
OUTPUT
关键字放在任何地方。