当前位置: 代码迷 >> Sql Server >> TSQL语句太长了,怎么处理?
  详细解决方案

TSQL语句太长了,怎么处理?

热度:24   发布时间:2016-04-27 14:31:26.0
TSQL语句太长了,怎么办?!
请看以下代码,主要看最后一行即exec(@ss)
[email protected]

SQL code
set ANSI_NULLS ONset QUOTED_IDENTIFIER ONGO-- =============================================-- Author:        <Author,,Name>-- Create date: <Create Date,,>-- Description:    <Description,,>-- =============================================ALTER PROCEDURE [dbo].[fsd]@km as varchar(max)ASBEGIN    drop table zffsd    exec zf @km    declare @s varchar(max),@ss varchar(max)    declare @i int,@j int,@top int,@bottom int,@from int,@to int    declare @bj int    set @s=''set @ss=''    set @top=700    set @bottom=200    set @[email protected]        declare my_cursor cursor scroll dynamic        for        select distinct [班级] from  mcb        open my_cursor                fetch next from my_cursor into @bj        set @s='select '''+ltrim(str(@top))+'以上'''+' 段次'        while(@@fetch_status=0)        begin            --print 'Query ID: ' + cast(@bj as varchar)            set @[email protected]+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>='+ltrim(str(@top))+') 计'+ltrim(str(@bj))+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>='+ltrim(str(@top))+') 累'+ltrim(str(@bj))            fetch next from my_cursor into @bj        end        set @[email protected]+',(select count(*) from mcb where 总分>='+ltrim(str(@top))+') 总计'+',(select count(*) from mcb where 总分>='+ltrim(str(@top))+') 总累 into zffsd'--print @s        --select @s uuu        --exec(@s)set @[email protected][email protected]+' union '        close my_cursor        deallocate my_cursor    while @j>[email protected]+10    begin        set @[email protected]        set @[email protected]        declare my_cursor cursor scroll dynamic        for        select distinct [班级] from  mcb        open my_cursor                fetch next from my_cursor into @bj        set @s='select '''+ltrim(str(@j-10))+''' 段次'        while(@@fetch_status=0)        begin            --print 'Query ID: ' + cast(@bj as varchar)            set @[email protected]+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>='+ltrim(str(@from))+' and 总分<'+ltrim(str(@to))+') 计'+ltrim(str(@bj))+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>='+ltrim(str(@from))+') 累'+ltrim(str(@bj))            fetch next from my_cursor into @bj        end        set @[email protected]+',(select count(*) from mcb where 总分>='+ltrim(str(@from))+' and 总分<'+ltrim(str(@to))+') 总计'+',(select count(*) from mcb where 总分>='+ltrim(str(@from))+') 总累'        --select @s        --exec(@s)set @[email protected][email protected]+' union '        close my_cursor        deallocate my_cursor        set @[email protected]    end        set @[email protected]        declare my_cursor cursor scroll dynamic        for        select distinct [班级] from  mcb        open my_cursor                fetch next from my_cursor into @bj        set @s='select '' '+ltrim(str(@bottom))+'以下'''+' 段次'        while(@@fetch_status=0)        begin            --print 'Query ID: ' + cast(@bj as varchar)            set @[email protected]+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>0 and 总分<'+ltrim(str(@bottom))+') 计'+ltrim(str(@bj))+',(select count(*) from mcb where 班级='+ltrim(str(@bj))+' and 总分>0) 累'+ltrim(str(@bj))            fetch next from my_cursor into @bj        end        set @[email protected]+',(select count(*) from mcb where 总分>0 and 总分<'+ltrim(str(@bottom))+') 总计'+',(select count(*) from mcb where 总分>0) 总累'        --select @s uuu        --exec(@s)set @[email protected][email protected]+' order by 段次 desc'        close my_cursor        deallocate my_cursorENDselect @ss--print @ssexec(@ss)





------解决方案--------------------
分开,分几个变量

exec(@[email protected]+...)
------解决方案--------------------
05上的用max试试!
------解决方案--------------------