例:
create table test(indust varchar(10)
,[200301] varchar(10)
,[200302] varchar(10)
,[200303] varchar(10))
insert test select 'a','111','222','333'
union all select 'b','444','555','666'
union all select 'c','777','888','999'
union all select 'd','789','910','012'
go--数据处理
declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
select @f1='',@f2='',@f3=''
select @f1=@f1+',['+indust+']='''+[200301]+'''' --这儿为什么是1四个单引号 2改成两个''
,@f2=@f2+','''+[200302]+''''
,@f3=@f3+','''+[200303]+''''
from testprint 'select 日期=''200301'''+@f1
+' union all select ''200302'''+@f2
+' union all select ''200303'''+@f31出来的结果为:
select 日期='200301',[a]='111',[b]='444',[c]='777',[d]='789' union all select '200302','222','555','888','910' union all select '200303','333','666','999','012'2出来的结果就是:select 日期='200301',[a]='111,[b]='444,[c]='777,[d]='789 union all select '200302','222','555','888','910' union all select '200303','333','666','999','012'这究竟是为什么呢?
希望老手能举例讲解遇到这种引号嵌套的怎么用:
create table test(indust varchar(10)
,[200301] varchar(10)
,[200302] varchar(10)
,[200303] varchar(10))
insert test select 'a','111','222','333'
union all select 'b','444','555','666'
union all select 'c','777','888','999'
union all select 'd','789','910','012'
go--数据处理
declare @f1 varchar(8000),@f2 varchar(8000),@f3 varchar(8000)
select @f1='',@f2='',@f3=''
select @f1=@f1+',['+indust+']='''+[200301]+'''' --这儿为什么是1四个单引号 2改成两个''
,@f2=@f2+','''+[200302]+''''
,@f3=@f3+','''+[200303]+''''
from testprint 'select 日期=''200301'''+@f1
+' union all select ''200302'''+@f2
+' union all select ''200303'''+@f31出来的结果为:
select 日期='200301',[a]='111',[b]='444',[c]='777',[d]='789' union all select '200302','222','555','888','910' union all select '200303','333','666','999','012'2出来的结果就是:select 日期='200301',[a]='111,[b]='444,[c]='777,[d]='789 union all select '200302','222','555','888','910' union all select '200303','333','666','999','012'这究竟是为什么呢?
希望老手能举例讲解遇到这种引号嵌套的怎么用:
你所看到的那个是两个单引号,
在动态SQL语句里,要用两个单引号代表一个单引号~
那么我上面例题中的'''' 又代表什么意思呢?
'号是字符分割符(表示字符串的边界)楼主看一下:
select 'aa'
select 'aaa'+'bbb'select '''' --最边上2个是表示边界,中间那2个'表示输入一个'
应该就会明白了