当前位置: 代码迷 >> Sql Server >> 动态SQL字符串连接有异常
  详细解决方案

动态SQL字符串连接有异常

热度:87   发布时间:2016-04-27 14:38:27.0
动态SQL字符串连接有错误
如下代码中,@condition变量链接出错了,该怎么先定义好再在SQL中引用呢?
SQL code
  declare @sql varchar(2000),@condition varchar(2000)  set @sql='' set @condition='where nclassid=36 and nexamid=19 and nschoolid=1'  select @[email protected]+'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 [email protected]+'''' )) aexec('select name as 姓名,'+ @sql+ '          sum(descore) descoreinto #tempfrom(    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 [email protected]+''') and    a.ncouexamid=d.id and     d.ncourseid=c.id    group by b.name,a.descore,c.scoursename)as agroup by nameselect *,place=(select count(descore) from #temp where descore>x.descore)+1from #temp xorder by place') 


------解决方案--------------------
SQL code
--动态语句语法/******************************************************************************************************************************************************动态语句语法:exec\sp_executesql语法整理人:中国风(Roy)日期:2008.06.06******************************************************************************************************************************************************/动态语句语法:--方法1查询表改为动态select * from sysobjectsexec('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 [email protected]+' from sysobjects where [email protected]+'=5' )declare @s varchar(1000)set @s=N'select [email protected]+' from sysobjects where [email protected]+'=5'exec sp_executesql @s--会报错declare @s nvarchar(1000)--改为nvarcharset @s=N'select [email protected]+' from sysobjects where [email protected]+'=5'exec sp_executesql @s--成功--方法3:输入参数declare @i int,@s nvarchar(1000)set @i=5exec('select ID,Name from sysobjects where [email protected])set @s='select ID,Name from sysobjects where [email protected]'exec sp_executesql @s,[email protected] int',@i--此处输入参数要加上N--方法4:输出参数declare @i int,@s nvarchar(1000)set @s='select @i=count(1) from sysobjects'--用execexec('declare @i int [email protected]+' select @i')--把整个语句用字符串加起来执行--用sp_executesqlexec sp_executesql @s,[email protected] int output',@i output--此处输出参数要加上Nselect @i--方法5:输入输出--用sp_executesqldeclare @i int,@con int,@s nvarchar(1000)set @i=5select @s='select @con=count(1) from sysobjects where ID>@i'exec sp_executesql @s,[email protected] int output,@i int',@con output ,@i select @con--用execdeclare @i int,@s nvarchar(1000)set @i=5select @s='declare @con int select @con=count(1) from sysobjects where ID>'+rtrim(@i)+' select @con'exec(@s)
------解决方案--------------------
太長了。。。
select @[email protected]+'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 [email protected]+'''' )) a
這一段也屬于一個動態的sql,應該也是需要exec(@sql)[email protected],帶到下一段。
可在每次exec前先print @sql查看該語句是否符合預期。
------解决方案--------------------
SQL code
  declare @sql varchar(2000),@condition varchar(2000)  set @sql='' set @condition='where nclassid=36 and nexamid=19 and nschoolid=1'  select @[email protected]+'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 [email protected]+''' )) a
------解决方案--------------------
select ncourseid from t_card_examcou [email protected]+'''
  相关解决方案