我把长的字符串放到临时表里
用的时候把每一行都加起来,不知道怎么做
出类事:exec(@sql1+@sql2)select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn, '0到6' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson.Code Like '00
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '7到12' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson.
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '13到18' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '19到24' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '25到30' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '31到36' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '37到42' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '43到48' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '49到54' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn,  '55到60' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson
 Union All select case when(grouping(IsCheckIn) = 1) then '汇总' else case when (IsCheckIn=1) then '已检' else '未检' end end as IsCheckIn, '大于60' As AgeLevel,  count(*) As Count from tbPerson,tbMedical where tbPerson.PersonId = tbMedical.PersonId and tbPerson.C/*
以上是我的sql union后的结果
*/但是我怎么运行它?declare @sss nvarchar(4000)
set @sss='select ''''+a from #t'
exec @sss但是报错:
服务器: 消息 2812,级别 16,状态 62,行 94
未能找到存储过程 'select ''+a from #t'。

解决方案 »

  1.   

    饿地神啊
       sql可以优化地。
      

  2.   

    declare @sss nvarchar(4000)set @sss='select ''''+a from #t'exec (@sss)       --这里应该加括号
      

  3.   

    方式一:
    exec (@sss)方式二:
    EXECUTE sp_executesql @sss例子:
    DECLARE @pageindex int,@pagesize int
    SET @pageindex=convert(int,@pParam1)
    SET @pagesize=convert(int,@pParam2) SET @SqlStr='
    SELECT TOP ' + str(@pagesize) + '
    RowID,
    EmpNo,
    DptID,
    CanOperate
    FROM T_Sys_BaseUserDept(nolock)'
    IF @pageindex<>1      --Not the first page
    BEGIN
    SET @SqlStr=@SqlStr + 'WHERE RowID>(SELECT MAX(RowID) FROM (SELECT TOP ' + str((@pageindex-1)*@pagesize)+ 
    ' RowID FROM T_Sys_BaseUserDept ORDER BY RowID ASC) AS tmptable)'
    END
    SET @SqlStr=@SqlStr+' ORDER BY RowID ASC'
    EXECUTE sp_executesql @sqlstr,N'@pageindex int,@pagesize int',@pageindex,@pagesize
      

  4.   

    exec @sss 
    @sss 这里默认是存储过程,所以报未能找到存储过程
    执行字符串SQL,那么加括号exec (@sss )至于你的语句,觉得可能需要处理一下,太长了哦