惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

V
Vulnerabilities – Threatpost
雷峰网
雷峰网
GbyAI
GbyAI
F
Fortinet All Blogs
MyScale Blog
MyScale Blog
MongoDB | Blog
MongoDB | Blog
博客园 - 聂微东
V
V2EX
Jina AI
Jina AI
Apple Machine Learning Research
Apple Machine Learning Research
C
CXSECURITY Database RSS Feed - CXSecurity.com
美团技术团队
Engineering at Meta
Engineering at Meta
T
Tenable Blog
P
Privacy & Cybersecurity Law Blog
Project Zero
Project Zero
Cloudbric
Cloudbric
Help Net Security
Help Net Security
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
L
LINUX DO - 热门话题
J
Java Code Geeks
WordPress大学
WordPress大学
S
Securelist
F
Full Disclosure
N
News and Events Feed by Topic
B
Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
I
InfoQ
P
Proofpoint News Feed
Hugging Face - Blog
Hugging Face - Blog
U
Unit 42
Google Online Security Blog
Google Online Security Blog
Spread Privacy
Spread Privacy
P
Proofpoint News Feed
宝玉的分享
宝玉的分享
S
Schneier on Security
T
The Exploit Database - CXSecurity.com
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Schneier on Security
Schneier on Security
Google DeepMind News
Google DeepMind News
cs.CV updates on arXiv.org
cs.CV updates on arXiv.org
月光博客
月光博客
Martin Fowler
Martin Fowler
T
Threat Research - Cisco Blogs
小众软件
小众软件
V2EX - 技术
V2EX - 技术
Last Week in AI
Last Week in AI
Stack Overflow Blog
Stack Overflow Blog
The Last Watchdog
The Last Watchdog
D
DataBreaches.Net

博客园 - pomp

Jquery资源-床头必备 全球网络的主色调是什么? user-agent检测详解 【转】前端开发大众手册(包括工具、网址、经验等) 详解Javascript事件驱动的来龙去脉【转】 基于JQuery的7款选项卡(Tabs)实例 什么是ImageLazyLoad技术 IE6下PNG图片透明 (二) 样式定义的权重问题 记录【转】 ie6 下最佳 PNG透明方案【转】 Javascript 判断浏览器是否为IE的最短方法 乔布斯:关于 Flash 的思考[转] Flex 开发必备10武器(转载) Flex4 SDK 新特性 PPT SQL存储过程学习中的概念汇总 CuteEditor编辑器(转) SqlDataSource查询设置 sql select语句详解 跨页传值方法集锦
SQL中获得EXEC后面的sql语句或者存储过程的返回值的方法 【收藏】
pomp · 2009-07-28 · via 博客园 - pomp


         前言:在数据库程序开发的过程中,我们经常会碰到利用EXEC来执行一段需要返回某些值的sql语句(通常是构造动态sql语句时使用),或者在一个存储过程中利用EXEC调用另一个有返回值的存储过程(必须获得返回值),那么如何获得这些返回值呢?

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
 上面的@rc这个sql语句的功能是找出特定时间段里所有有空的教室数量以及这些教室所能容纳的学生人数,因为涉及到动态的sql语句(@csql这句里条件中有一个列名是动态变化的)的构造,所以要放在exec里执行,但是同时我又要返回2个结果,所以执行时的代码为:


        exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法
       

这样就将返回值放到了,@cstucount,@ccount两个变量中,得到了我们想要的结果。

2,exec执行带返回值的存储过程的情况

我们来看一个简单的存储过程:

create procedure ProTest
(
         @name varchar(10),
         @money int output
)
as
begin
        if(@name='1')
                  set @money=1000
        else
                  set @money=2000
end
这个只是一个简单的示例,这个存储过程返回的是@money 这个参数的值,那么当我们在另外一个存储过程中调用此存储过程的时候如何获取这个参数呢,方法如下:

declare @m int ---用来接收返回值的变量
exec ProTest @name='1',@money=@m output --一定要注名是output
就这么简单,我们就获得了返回值,然后就可以利用它了
呵呵。。。这是我在做项目时碰到的情况时的解决办法(当然可能还有其他办法)。。。希望对您有帮助。。。

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/j_jake/archive/2007/07/15/1691738.aspx