如何将存储过程的输出返回到sql server中的变量中 [英] How to return the output of stored procedure into a variable in sql server

查看:24
本文介绍了如何将存储过程的输出返回到sql server中的变量中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在 SQL Server 中执行一个存储过程并将输出分配给一个变量(它返回一个值)?

I want to execute a stored procedure in SQL Server and assign the output to a variable (it returns a single value) ?

推荐答案

这取决于您要返回的信息的性质.

That depends on the nature of the information you want to return.

如果是单个整数值,可以使用return语句

If it is a single integer value, you can use the return statement

 create proc myproc
 as 
 begin
     return 1
 end
 go
 declare @i int
 exec @i = myproc

如果你有一个非整数值,或者多个标量值,你可以使用输出参数

If you have a non integer value, or a number of scalar values, you can use output parameters

create proc myproc
  @a int output,
  @b varchar(50) output
as
begin
  select @a = 1, @b='hello'
end
go
declare @i int, @j varchar(50)
exec myproc @i output, @j output

如果要返回数据集,可以使用insert exec

If you want to return a dataset, you can use insert exec

create proc myproc
as 
begin
     select name from sysobjects
end
go

declare @t table (name varchar(100))
insert @t (name)
exec myproc

你甚至可以返回一个游标,但这太可怕了,所以我不举个例子:)

You can even return a cursor but that's just horrid so I shan't give an example :)

这篇关于如何将存储过程的输出返回到sql server中的变量中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆