在T-SQL存储过程中显示多个输出参数

时间:2013-07-31 17:59:36

标签: sql sql-server tsql

我想在存储过程

中显示2个表,每列1列作为我的输出

定义为

create procedure p1
        @name     varchar(20) OUTPUT,
        @company  varchar(20) OUTPUT
As

BEGIN
      select @name = t1.name from table1 t1;

      select @company = t2.company from table2; 

END

执行

declare @name varchar(20), @company varchar(20)

exec dbo.p1 @name = @name, @company = @company

select @name as 'Name', @company as 'Company'

但是,这只显示一行。我做错了什么?

1 个答案:

答案 0 :(得分:2)

如果要将这些值显示为1列,2行 - 请使用UNION:

select @name as 'Name'
UNION ALL
select @company

请注意,这两个值都会显示在相同的列名称“名称”

如果你想显示字符串'Name'和'Company',你必须确保另一列的行顺序:

select 'Name' as Info, 0 as Sort
UNION ALL
select @name, 1
UNION ALL
select 'Company', 2
UNION ALL
select @company, 3
Order by Sort