SQL一个存储过程调用另一个存储过程 获得返回值问
来源:互联网 发布:仿淘宝大花朵钩花帽子 编辑:程序博客网 时间:2024/05/22 03:22
第一种方法: 使用output参数
USE AdventureWorks;GOIF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL DROP PROCEDURE Production.usp_GetList;GOCREATE PROCEDURE Production.usp_GetList @product varchar(40) , @maxprice money , @compareprice money OUTPUT , @listprice money OUTAS SELECT p.name AS Product, p.ListPrice AS 'List Price' FROM Production.Product p JOIN Production.ProductSubcategory s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice;-- Populate the output variable @listprice.SET @listprice = (SELECT MAX(p.ListPrice) FROM Production.Product p JOIN Production.ProductSubcategory s ON p.ProductSubcategoryID = s.ProductSubcategoryID WHERE s.name LIKE @product AND p.ListPrice < @maxprice);-- Populate the output variable @compareprice.SET @compareprice = @maxprice;GO
另一个存储过程调用的时候:
Create Proc TestasDECLARE @compareprice money, @cost money EXECUTE Production.usp_GetList '%Bikes%', 700, @compareprice OUT, @cost OUTPUTIF @cost <= @compareprice BEGIN PRINT 'These products can be purchased for less than $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'ENDELSE PRINT 'The prices for all products in this category exceed $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
第二种方法:创建一个临时表
create proc GetUserNameasbegin select 'UserName'endCreate table #tempTable (userName nvarchar(50))insert into #tempTable(userName)exec GetUserNameselect #tempTable
--用完之后要把临时表清空
drop table #tempTable--需要注意的是,这种方法不能嵌套。例如:
procedure a begin ... insert #table exec b end procedure b begin ... insert #table exec c select * from #table end procedure c begin ... select * from sometable end
--这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
--会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
第三种方法:声明一个变量,用exec(@sql)执行:
1);EXEC 执行SQL语句
declare @rsql varchar(250) declare @csql varchar(300) declare @rc nvarchar(500) declare @cstucount int declare @ccount int set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1''' --exec(@rsql) set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in ' set @rc=@csql+@rsql exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法 --select @csql+@rsql --select @cstucount
0 0
- SQL一个存储过程调用另一个存储过程 获得返回值问
- SQL一个存储过程调用另一个存储过程 获得返回值问题
- SQL一个存储过程调用另一个存储过程 获得返回值
- SQL一个存储过程调用另一个存储过程 获得返回值问题
- SQL一个存储过程调用另一个存储过程 获得返回值问题
- SQL一个存储过程调用另一个存储过程 获得返回值问题
- SQL一个存储过程调用另一个存储过程 获得返回值
- SQL一个存储过程调用另一个存储过程 获得返回值问题
- 在一个存储过程中调用返回一个游标的另一个存储过程
- oracle 在一个存储过程中调用另一个返回游标的存储过程
- oracle 在一个存储过程中调用另一个返回游标的存储过程
- oracle 在一个存储过程中调用另一个返回游标的存储过程
- 用SQL语句获得一个存储过程返回的表
- sql中用存储过程调用另一个存储过程
- SQL 存储过程里调用另一个存储过程
- 在MSSQL中一个存储过程调用另一个存储过程
- Sql server 跨服务器在一个存储过程sp_A中调用另一个存储过程sp_B
- C语言调用存储过程并且获得返回值
- 安装perl的CPAN模块(mysql-mmm perl报错)
- Spring MVC-自定义拦截器解决方案
- 自定义view 重写一个构造函数报错的问题
- 中山大学 CSU 1393
- LINUX start_kernel 分析,一位大师级的人物写的,不看要后悔的哟!!
- SQL一个存储过程调用另一个存储过程 获得返回值问
- Python全局变量和局部变量
- 八大排序算法总结
- 怎么判断UITextField 输入为空 输入全为空格
- 用于windbg检查堆是否被破坏的python脚本
- Property 'c' cannot be found in forward class object 'CALayer *"
- 仿多页面滑动切换时背景指示图(如TAB文字下边的白条等)的动画实现原理,例PagerSlidingTabStrip
- 《重构:改善既有代码的设计》——关于代码注释的唠叨
- Python实现有道翻译