All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
打造一个通用ASP.NET数据分页控件_[Asp.Net教程]
探讨一种AJAX性能的改进方法_[Asp.Net教程]
浅谈几个SQL的日志概念_[SQL,Server教程]
利用.NET绘图技术制作水晶按钮控件_.net资料_编程技术
ASP.NET中利用存储过程实现模糊查询_[Asp.Net教程]
C#网络应用编程基础练习题与答案(五)_[Asp.Net教程]
Visual C++ 6.0教程:c++数据类型之枚举
JS捕捉网页浏览器窗口的关闭与刷新_JavaScript技术_编程技术
一个读取扩展名为xml的资源文件的方法_.net资料_编程技术
C#文件处理技术:FileInfo和DirectoryInfo
Reading number is top 10 pictures
中国女孩大胆自拍,显露完美身材
程序员的悲哀
人美胸美腿更美2
2012 national geographic daily picture5
在加油站厕所门口看到的告示
全球十大灵异酒店
传奇套装
Sora aoi on twitter2
沙漠里的美女
随便发几张图
Download software ranking
Unix video tutorial2
Ashlynn Video2
Tram sex maniac 2 (H) rar bag18
Macromedia Dreamweaver 8
Unix video tutorial3
Kung fu panda - the secret of the teacher
The Bermuda triangle3
matrix2
Unix video tutorial15
1400篇各类破解文章
归海一刀 published in(发表于) 2014/2/3 6:44:24 Edit(编辑)
学习SQL应知道的动态SQL语句基本语法_[SQL Server教程]

学习SQL应知道的动态SQL语句基本语法_[SQL Server教程]

学习SQL应知道的动态SQL语句基本语法_[SQL Server教程]

学习SQL应知道的动态SQL语句基本语法



1 、普通SQL语句可以用Exec执行


eg: Select * from tableName
Exec('select * from tableName')
Exec sp_executesql N'select * from tableName' -- 请注意字符串前一定要加N


2、字段名,表名,数据库名之类作为变量时,必须用动态SQL


eg:
declare @fname varchar(20)
set @fname = 'FiledName'
Select @fname from tableName -- 错误,不会提示错误,但结果为固定值FiledName,并非所要。
Exec('select ' + @fname + ' from tableName') -- 请注意 加号前后的 单引号的边上加空格


当然将字符串改成变量的形式也可
declare @fname varchar(20)
set @fname = 'FiledName' --设置字段名


declare @s varchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句会报错



declare @s Nvarchar(1000) -- 注意此处改为nvarchar(1000)
set @s = 'select ' + @fname + ' from tableName'
Exec(@s) -- 成功
exec sp_executesql @s -- 此句正确


3、输出参数


declare @num int,
@sqls nvarchar(4000)
set @sqls='select count(*) from tableName'
exec(@sqls)
--如何将exec执行结果放入变量中?


declare @num int,
@sqls nvarchar(4000)
set @sqls='select @a=count(*) from tableName '
exec sp_executesql @sqls,N'@a int output',@num output
select @num


此外,如果想要在SQL语句 字符串中使用 单引号 '' 可以 使用 ''''


来源:网络







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.