Can you call a SQL Stored Procedure that returns a

2019-07-08 12:04发布

Please consider the following SQL Server table and stored procedure.

create table customers(cusnum int, cusname varchar(50))

insert into customers(cusnum, cusname) values(1, 'Ken')
insert into customers(cusnum, cusname) values (2, 'Violet') --The Wife

create procedure getcus 
  @cusnum int
as 
Begin
   select cusname
   from customers (nolock)
   where cusnum = @cusnum
End

You know how you can write T-SQL code like this:

declare @cusname varchar(50)

select @cusname = cusname
from customers 
where cusnum = 1

Can I do this with my stored procedure?

for example the code would look like this:

declare @cusnum int
declare @cusname varchar(50)

set @cusnum = 1

exec @cusname = cusname pbogetcus @cusnum

Thanks in advance.

1条回答
\"骚年 ilove
2楼-- · 2019-07-08 12:54

No, you can't return values like that.

You need to use OutputParameters: MSDN.

EDIT:

This might be a better link:

SQL Team

Check out the section about midway down: Using OUTPUT variables

查看更多
登录 后发表回答