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

时间:2023-02-05 19:17:11

I want to display 2 tables of 1 column each as my output in the stored procedure

我想在存储过程中显示2个表,每一列都是我的输出。

Defined as

定义为

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

Executed as

执行

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

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

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

However, this just displays a single row . What am I doing wrong?

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

1 个解决方案

#1


2  

If you want to display those values as a 1 column, 2 rows - use UNION:

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

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

Note that both values will display under same column name 'Name'

注意,两个值都将显示在相同的列名称“name”中

If you want to display strings 'Name' and 'Company' as well you will have to assure order of rows by another column:

如果您想要显示字符串的名称和“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

#1


2  

If you want to display those values as a 1 column, 2 rows - use UNION:

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

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

Note that both values will display under same column name 'Name'

注意,两个值都将显示在相同的列名称“name”中

If you want to display strings 'Name' and 'Company' as well you will have to assure order of rows by another column:

如果您想要显示字符串的名称和“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