动态SQL基本使用求解
--引用论坛上一兄弟的例子
if exists(select 1 from sysobjects where id=object_id('bm'))
drop table bm
go
CREATE TABLE bm(bmid int,bmname varchar(10) ,username varchar(10) ,hpmc varchar(10) ,gz int)
insert into bm select 1,'销售一部','小张','货品A',1000
insert into bm select 2,'销售二部','小王','货品B',3000
insert into bm select 3,'销售三部','小乐','货品C',2000
insert into bm select 4,'销售四部','小马','货品D',1000
declare @sql varchar(8000)
select @sql=isnull(@sql+',','')+name --正确情况的使用
from syscolumns where id=object_id('bm')
and name <>'gz'
print @sql
第二种
if exists(select 1 from sysobjects where id=object_id('bm'))
drop table bm
go
CREATE TABLE bm(bmid int,bmname varchar(10) ,username varchar(10) ,hpmc varchar(10) ,gz int)
insert into bm select 1,'销售一部','小张','货品A',1000
insert into bm select 2,'销售二部','小王','货品B',3000
insert into bm select 3,'销售三部','小乐','货品C',2000
insert into bm select 4,'销售四部','小马','货品D',1000
declare @sql varchar(8000)
set @sql=‘’
select @sql=@sql+name
from syscolumns where id=object_id('bm')
and name <>'gz'
print @sql
1. 这两种情况区别在于结果之间 多了几个逗号区分(ISNULL中是如何实现的,能详细点最好)
(第一种情况的结果)bmid,bmname,hpmc,username
其实这个问题好象跟是由第2个问题引起的。
2. 假如select @sql=@sql+name换成 select @sql=@sql+name 的话就只能取得一个值了(当有多值时,select只能返回结果中的最后一个值,这个我知道),而select @sql=@sql+name为什么能返回多个值,难道select @sql=@sql+name能实现循环的功能,将几个结果相加我一直都想不通,请高手能帮下我,能把详细的步骤说下吗。 谢谢
2.
...全文
相关推荐
sql语句、动态SQL语句基本语法
综上所述,掌握SQL语句和动态SQL的基本语法对于任何IT从业者来说都至关重要,无论是数据库管理员还是开发者,都需要熟练运用这些技能来高效地操作和管理数据库。
动态SQL语句基本语法
动态SQL语句基本语法动态SQL语句是指使用字符串来构建SQL语句,然后使用EXEC或sp_executesql语句来执行的SQL语句。
动态SQL语句基本语法。动态SQL语句基本语法
动态sql语句基本语法1 :普通SQL语句可以用Exec执行 例: Select * from tableName Exec('select * fr
Mybatis之动态sql标签的使用
动态SQL语言可以帮助我们摆脱手动拼接SQL语句的痛苦,同时也可以提高开发效率和代码的可读性。Mybatis之动态SQL标签的使用可以分为两部分:基本动态SQL标签和高级动态SQL标签。
学习SQL 应知道的动态SQL 语句基本语法
### 学习SQL应知道的动态SQL语句基本语法#### 动态SQL语句概述动态SQL是指在运行时动态构建的SQL语句。


