当前位置: 代码迷 >> Sql Server >> 为什么这个存储过程在执行时老出错?该如何解决
  详细解决方案

为什么这个存储过程在执行时老出错?该如何解决

热度:77   发布时间:2016-04-27 16:39:35.0
为什么这个存储过程在执行时老出错?
begin
begin   transaction
select   @i=count(*)   from   sp_chargetype   where   dept_num= '5201 '
set   @a=1
declare   @sql   nvarchar(4000)
set   @sql= 'CREATE   TABLE   #temp3   (corp_name   nvarchar(50)   null   '
while   @i> 0
    begin
            set   @sql   [email protected]+ ',fee '+rtrim(@a)+ '   decimal(13,2)   null,sumfee '+rtrim(@a)+ '   decimal(13,2)   null '
            set   @[email protected]+1
            set   @[email protected]
    end  
set   @[email protected]+ ') '
exec(@sql)
insert   into   #temp3   (corp_name)  
select   max(corp_name)   from   #temp1   group   by   corp_name
commit

end

------解决方案--------------------
--try


begin
begin transaction
select @i=count(*) from sp_chargetype where dept_num= '5201 '
set @a=1
declare @sql nvarchar(4000)
set @sql= 'CREATE TABLE #temp3 (corp_name nvarchar(50) null '
while @i> 0
begin
set @sql [email protected]+ ',fee '+rtrim(@a)+ ' decimal(13,2) null,sumfee '+rtrim(@a)+ ' decimal(13,2) null '
set @[email protected]+1
set @[email protected]
end

set @[email protected]+ ') '

set @sql = @sql + ' go '

set @sql = @sql + '
insert into #temp3 (corp_name)
select max(corp_name) from #temp1 group by corp_name '

exec(@sql)

commit

end
  相关解决方案