为什么这个存储过程在执行时老出错?
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 =@sql+ ',fee '+rtrim(@a)+ ' decimal(13,2) null,sumfee '+rtrim(@a)+ ' decimal(13,2) null '
set @a=@a+1
set @i=@i-1
end
set @sql=@sql+ ') '
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 =@sql+ ',fee '+rtrim(@a)+ ' decimal(13,2) null,sumfee '+rtrim(@a)+ ' decimal(13,2) null '
set @a=@a+1
set @i=@i-1
end
set @sql=@sql+ ') '
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