超长字符串问题,急!

zhh007 2007-05-08 03:23:46
我把长的字符串放到临时表里
用的时候把每一行都加起来,不知道怎么做
出类事: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'。
...全文
295 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
-狙击手- 2007-05-10
  • 打赏
  • 举报
回复
exec (@sss )
sp4 2007-05-09
  • 打赏
  • 举报
回复
exec @sss
@sss 这里默认是存储过程,所以报未能找到存储过程
执行字符串SQL,那么加括号exec (@sss )

至于你的语句,觉得可能需要处理一下,太长了哦
---涛声依旧--- 2007-05-09
  • 打赏
  • 举报
回复
方式一:
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
netcoder 2007-05-09
  • 打赏
  • 举报
回复
同楼上!
OracleRoob 2007-05-09
  • 打赏
  • 举报
回复
declare @sss nvarchar(4000)

set @sss='select ''''+a from #t'

exec (@sss) --这里应该加括号
zhh007 2007-05-08
  • 打赏
  • 举报
回复
我是说@sss运行后的结果,我还要让它在执行
子陌红尘 2007-05-08
  • 打赏
  • 举报
回复
建议楼主最好还是用多个字符串把数据串起来执行
shuai45 2007-05-08
  • 打赏
  • 举报
回复
饿地神啊
sql可以优化地。
子陌红尘 2007-05-08
  • 打赏
  • 举报
回复
exec(@sss)

34,575

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧