调用存储过程 [英] Calling Stored Procedures
问题描述
大家好吧
我想知道是否有办法从内部调用存储过程
另一个存储过程。因此,例如我的第一个程序将调用
秒存储过程,该程序在执行时将返回一条记录,并且我希望在调用存储过程中使用此数据。这可能吗?
提前致谢
Hi All
I was wondering if there is a way to call a stored procedure from inside
another stored procedure. So for example my first procedure will call a
second stored procedure which when executed will return one record and i
want to use this data in the calling stored procedure. Is this possible ?
Thanks in advance
推荐答案
" Jarrod Morrison" < JA ***** @ ihug.com.au>在留言新闻中写道:< bv ********** @ lust.ihug.co.nz> ...
"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message news:<bv**********@lust.ihug.co.nz>...
大家好我
我是想知道是否有办法从另一个存储过程内部调用存储过程。因此,例如我的第一个程序将调用第二个存储过程,该程序在执行时将返回一个记录,并且我想在调用存储过程中使用此数据。这可能吗?
提前致谢
Hi All
I was wondering if there is a way to call a stored procedure from inside
another stored procedure. So for example my first procedure will call a
second stored procedure which when executed will return one record and i
want to use this data in the calling stored procedure. Is this possible ?
Thanks in advance
有几种选择 - 见这里:
如果是一条记录你的意思是一个标量值,然后一个OUTPUT参数
会起作用;如果你的意思是一行的结果集,那么你需要
其他方法之一。
Simon
There are several options - see here:
http://www.sommarskog.se/share_data.html
If by "one record" you mean a scalar value, then an OUTPUT parameter
would work; if you mean a result set of one row, then you would need
one of the other approaches.
Simon
Jarrod,
有两种方法可以做到这一点。 (可能更多,但这些是最常见的两种方式)。这两个都使用Northwind数据库,所以如果需要你可以自己测试
:
WAY 1(这是我最喜欢的,因为它可以让你返回多个值):
创建程序sp_test1
作为开始
从订单中选择前1名orderID
其中customerID =''tomsp''
结束
创建程序sp_test2
as begin
声明@my_value varchar(20)
exec @my_value = sp_test1
打印@my_value
结束
exec sp_test2
WAY 2(这可能更常见,但语法有点奇怪。
注意使用关键字OUTPUT的位置。两者都是必需的):
创建程序sp_test1a @@ outparam varchar(20)输出
as begin
select top 1 @@ outparam = orderID来自订单
其中customerID =''tomsp''
结束
创建程序sp_test2a
开始
声明@my_value varchar(20)
exec sp_test1a @my_value输出
打印@my_value
结束
exec sp_test2a
您可以在
www.TechnicalVideos.net
祝你好运,
查克Conover
www.TechnicalVideos.net
" Jarrod Morrison < JA ***** @ ihug.com.au>在消息中写道
新闻:bv ********** @ lust.ihug.co.nz ...
Jarrod,
There are 2 ways to do this. (probably more, but these are the 2 most
common ways). Both of these use the Northwind database, so you can test
yourself, if needed:
WAY 1 (this is my favorite because it lets you return multiple values):
create procedure sp_test1
as begin
select top 1 orderID from orders
where customerID = ''tomsp''
end
create procedure sp_test2
as begin
declare @my_value varchar(20)
exec @my_value = sp_test1
print @my_value
end
exec sp_test2
WAY 2 (this is probably more common, but the syntax is a little strange.
Note BOTH places where the keyword OUTPUT is used. Both are necessary):
create procedure sp_test1a @@outparam varchar(20) OUTPUT
as begin
select top 1 @@outparam = orderID from orders
where customerID = ''tomsp''
end
create procedure sp_test2a
as begin
declare @my_value varchar(20)
exec sp_test1a @my_value OUTPUT
print @my_value
end
exec sp_test2a
You can find these and many more questions answered at
www.TechnicalVideos.net
Best regards,
Chuck Conover
www.TechnicalVideos.net
"Jarrod Morrison" <ja*****@ihug.com.au> wrote in message
news:bv**********@lust.ihug.co.nz...
大家好
<我想知道是否有办法从另一个存储过程内部调用存储过程。因此,例如我的第一个程序将调用第二个存储过程,该程序在执行时将返回一个记录,并且我想在调用存储过程中使用此数据。这可能吗?
提前致谢
Hi All
I was wondering if there is a way to call a stored procedure from inside
another stored procedure. So for example my first procedure will call a
second stored procedure which when executed will return one record and i
want to use this data in the calling stored procedure. Is this possible ?
Thanks in advance
Chuck Conover(cc ****** @commspeed .net)写道:
Chuck Conover (cc******@commspeed.net) writes:
创建程序sp_test1
不要你的技术视频告诉人们远离sp_
前缀?此前缀是从系统过程中保留的,而SQL Server
首先在master中查找这些前缀。有一点轻微的性能损失,
如果MS发布了一个新的系统程序,你可能会感到意外。
as begin
select top 1 orderID来自订单
其中customerID =''tomsp''
结束
创建程序sp_test2
作为开始
声明@my_value varchar(20)
exec @my_value = sp_test1
打印@my_value
结束
create procedure sp_test1
Don''t your technical videos tell people to stay away from the sp_
prefix? This prefix is reserved from system procedures, and SQL Server
first looks for these in master. There is a slight performance penalty,
and if MS ships a new system procedure, you might be in for a surprise.
as begin
select top 1 orderID from orders
where customerID = ''tomsp''
end
create procedure sp_test2
as begin
declare @my_value varchar(20)
exec @my_value = sp_test1
print @my_value
end
我不知道应该是什么样子,但它赢了不会飞。 sp_test1
没有RETURN语句,所以它总是会返回0. sp_test1
也会产生一个结果集,它会转到客户端。在sp_test2
中,您在varchar(20)中收到返回值,但是从存储过程返回的值是一个整数值。
-
Erland Sommarskog,SQL Server MVP, so****@algonet.se
SQL Server SP3联机丛书
http://www.microsoft.com/sql/techinf...2000/books.asp
这篇关于调用存储过程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!