如下代码中,@condition变量链接出错了,该怎么先定义好再在SQL中引用呢?
declare @sql varchar(2000),@condition varchar(2000)
set @sql='' set @condition='where nclassid=36 and nexamid=19 and nschoolid=1'
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course
where id in (select ncourseid from t_card_examcou ''''+@condition+'''' )) aexec('select name as 姓名,'+ @sql+ '
sum(descore) descore
into #temp
from
(
select b.name ,a.descore,c.scoursename
from t_card_score a,t_stu b,t_card_course c,t_card_examcou d
where
a.nstuid=b.id and
a.ncouexamid in (select id from t_card_examcou '''+@condition+''') and
a.ncouexamid=d.id and
d.ncourseid=c.id
group by b.name,a.descore,c.scoursename
)as a
group by nameselect *,place=(select count(descore) from #temp where descore>x.descore)+1
from #temp x
order by place
')
declare @sql varchar(2000),@condition varchar(2000)
set @sql='' set @condition='where nclassid=36 and nexamid=19 and nschoolid=1'
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course
where id in (select ncourseid from t_card_examcou ''''+@condition+'''' )) aexec('select name as 姓名,'+ @sql+ '
sum(descore) descore
into #temp
from
(
select b.name ,a.descore,c.scoursename
from t_card_score a,t_stu b,t_card_course c,t_card_examcou d
where
a.nstuid=b.id and
a.ncouexamid in (select id from t_card_examcou '''+@condition+''') and
a.ncouexamid=d.id and
d.ncourseid=c.id
group by b.name,a.descore,c.scoursename
)as a
group by nameselect *,place=(select count(descore) from #temp where descore>x.descore)+1
from #temp x
order by place
')
where 前加空格
/******************************************************************************************************************************************************
动态语句语法:exec\sp_executesql语法整理人:中国风(Roy)日期:2008.06.06
******************************************************************************************************************************************************/
动态语句语法:--方法1查询表改为动态
select * from sysobjects
exec('select ID,Name from sysobjects')
exec sp_executesql N'select ID,Name from sysobjects'--多了一个N为unicode--方法2:字段名,表名,数据库名之类作为变量时,用动态SQL
declare @FName varchar(20)
set @FName='ID'
exec('select '+@FName+' from sysobjects where '+@FName+'=5' )
declare @s varchar(1000)
set @s=N'select '+@FName+' from sysobjects where '+@FName+'=5'
exec sp_executesql @s--会报错
declare @s nvarchar(1000)--改为nvarchar
set @s=N'select '+@FName+' from sysobjects where '+@FName+'=5'
exec sp_executesql @s--成功
--方法3:输入参数declare @i int,@s nvarchar(1000)
set @i=5
exec('select ID,Name from sysobjects where ID='+@i)set @s='select ID,Name from sysobjects where ID=@i'
exec sp_executesql @s,N'@i int',@i--此处输入参数要加上N--方法4:输出参数declare @i int,@s nvarchar(1000)
set @s='select @i=count(1) from sysobjects'--用exec
exec('declare @i int '+@s+' select @i')--把整个语句用字符串加起来执行--用sp_executesql
exec sp_executesql @s,N'@i int output',@i output--此处输出参数要加上N
select @i
--方法5:输入输出--用sp_executesql
declare @i int,@con int,@s nvarchar(1000)
set @i=5
select @s='select @con=count(1) from sysobjects where ID>@i'
exec sp_executesql @s,N'@con int output,@i int',@con output ,@i
select @con--用exec
declare @i int,@s nvarchar(1000)
set @i=5
select @s='declare @con int select @con=count(1) from sysobjects where ID>'+rtrim(@i)+' select @con'
exec(@s)
參照用法
select ncourseid from t_card_examcou '+@condition+'
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course
where id in (select ncourseid from t_card_examcou ''''+@condition+'''' )) a
這一段也屬于一個動態的sql,應該也是需要exec(@sql)才能把這一段的值賦給@sql,帶到下一段。
可在每次exec前先print @sql查看該語句是否符合預期。
set @sql=''
select @sql=@sql+N'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course
where id in (select ncourseid from t_card_examcou where nclassid=36 and nschoolid=1 and nexamid=19 )) aexec('select name as 姓名,'+ @sql+ '
sum(descore) descore
into #temp
from
(
select b.name ,a.descore,c.scoursename
from t_card_score a,t_stu b,t_card_course c,t_card_examcou d
where
a.nstuid=b.id and
a.ncouexamid in (select id from t_card_examcou where nclassid=36 and nexamid=19 and nschoolid=1) and
a.ncouexamid=d.id and
d.ncourseid=c.id
group by b.name,a.descore,c.scoursename
)as a
group by nameselect *,place=(select count(descore) from #temp where descore>x.descore)+1
from #temp x
order by place
')
上面的这段代码中我想用一个变量将
where nclassid=36 and nexamid=19 and nschoolid=1
替换掉,但是变量用的不对,请大家帮忙看看
declare @sql varchar(2000),@condition varchar(2000)
set @sql='' set @condition='where nclassid=36 and nexamid=19 and nschoolid=1'
select @sql=@sql+'max(case scoursename when '''+scoursename+''' then descore else 0 end) '+scoursename+','
from (select distinct scoursename from t_card_course
where id in (select ncourseid from t_card_examcou '''+@condition+''' )) a
@condition前后各多了一个单引号,去掉试试
============
同意!