当前位置: 代码迷 >> Sql Server >> 各位高人帮忙修改一存储过程!该怎么处理
  详细解决方案

各位高人帮忙修改一存储过程!该怎么处理

热度:30   发布时间:2016-04-27 21:15:19.0
各位高人帮忙修改一存储过程!!!
alter   proc   [dbo].[Dis]  
as
begin

declare   @insertstring   varchar(2000)
declare   @deletestring   varchar(2000)
declare   @city   varchar   (50)

declare   cursor1   cursor   for   select   [Name]   from   table1
open   cursor1
fetch   cursor1   into   @city
while   (@@fetch_status   =   0)
begin

set   @insertstring   =  
'select   *   into   '   +   @city   +  
'   from     table2     where   [col1]   like ' ' '   +   @city+   '% ' ' '

set   @deletestring   =  
'delete   from   table2   where   [col1]   like   ' ' '+   @city+   '% ' ' '


begin   tran

exec(@insertstring)
if   @@ERROR <> 0
rollback   tran

exec(@deletestring)
if   @@ERROR <> 0
rollback   tran
commit   tran
fetch   cursor1   into   @city
end


close   cursor1
deallocate   cursor1

end

提示有错:第   33   行
COMMIT   TRANSACTION   请求没有对应的   BEGIN   TRANSACTION
请各位高手给于解决一下!!!

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

alter proc [dbo].[Dis]
as
begin

declare @insertstring varchar(2000)
declare @deletestring varchar(2000)
declare @city varchar (50)

declare cursor1 cursor for select [Name] from table1
open cursor1
fetch cursor1 into @city
while (@@fetch_status = 0)
begin

set @insertstring =
'select * into ' + @city +
' from table2 where [col1] like ' ' ' + @city+ '% ' ' '

set @deletestring =
'delete from table2 where [col1] like ' ' '+ @city+ '% ' ' '


begin tran

exec(@insertstring)
if @@ERROR <> 0
begin
rollback tran
goto closeCur
end
exec(@deletestring)
if @@ERROR <> 0
begin
rollback tran
goto closeCur
end
commit tran

fetch cursor1 into @city
end

closeCur:
close cursor1
deallocate cursor1

end


-----------
try
  相关解决方案