日期:2014-05-19  浏览次数:20482 次

各位高人帮忙修改一存储过程!!!
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