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

Reading number is top 10 articles
ASP.NET编程精选25种函数源程序_[Asp.Net教程]
对GridView数据控件进行绑定
.net发送邮件的一些技巧_.net资料_编程技术
基于AJAX技术的DataGrid控件编程_[Asp.Net教程]
动态网页制作技术PHP入门:什么是Apache?_php资料_编程技术
在Asp.Net2.0中可以方便的访问配置文件web.config_[Asp.Net教程]
C#中多媒体概述
在动态网页技术PHP5中类(CLASS)的新特征_php资料_编程技术
ASP.NET自动识别GB2312与UTF-8编码的文件_.net资料_编程技术
WML学习(一)-概述和基本规则_[XML教程]
Reading number is top 10 pictures
Gang rape
红楼梦金陵十二钗(2)
Athena chu perspective cheongsam shine with New York
In the world the most mysterious 21 place landscape4
怀春少女-石一伊
The girl of like self-time
9.3阅兵全景图3-外国方阵梯队和坦克方阵梯队
Female model behind the bitterness, often being overcharged3
Earthquake hedge common sense
俄罗斯台球天后惊艳魅惑2
Download software ranking
Adobe Flash Player(IE) 10.0.32.18 浏览器专用的FLASH插件
Unix video tutorial1
Tram sex maniac 2 (H) rar bag8
matrix1
Boxer's Top ten classic battle1
Ashlynn Video4
Unix video tutorial2
Unix video tutorial18
艳兽都市
I for your crazy
归海一刀 published in(发表于) 2014/2/3 6:43:02 Edit(编辑)
动态sql语句基本语法_[SQL Server教程]

动态sql语句基本语法_[SQL Server教程]

动态sql语句基本语法_[SQL Server教程]

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.