sql server - The current transaction cannot be committed and cannot support operations that write to the log file. Rollback the transaction -
i'm encountering error when running script in sql server 2008. when restore database in sql server 2012, runs , did not encounter errors.
thanks in advance!
here stored procedure:
create procedure updatedependentviews ( @tablename nvarchar(128), @alldependents bit = 1 ) set nocount on; create table #dependencies ( [counter] [int] identity(1,1) not null, [view_name] [nvarchar](128), ) on [primary]; create index counter on #dependencies(counter); /* first degree dependent views. */ insert #dependencies(view_name) select v.[name] [view_name] sys.sql_expression_dependencies sd inner join sys.views v on sd.referencing_id = v.object_id inner join sys.objects d on sd.referenced_id = d.object_id sd.referencing_minor_id = 0 , sd.referenced_minor_id = 0 , sd.referencing_class = 1 , d.type in ('u', 'v') , d.is_ms_shipped = 0 , v.is_ms_shipped = 0 , d.[name] = @tablename select * sys.sql_expression_dependencies referenced_entity_name null /* refresh dependent views. */ declare @viewname nvarchar(128) declare @counter int set @counter = 1; declare @errors nvarchar(max) set @errors = '' while exists (select [view_name] #dependencies counter = @counter) begin select @viewname = view_name #dependencies counter = @counter; /*get inner view dependencies */ if isnull(@alldependents, 0) = 1 begin if isnull(@alldependents, 0) = 1 , exists(select 1 sys.sql_expression_dependencies sd inner join sys.objects d on sd.referenced_id = d.object_id sd.referencing_minor_id = 0 , sd.referenced_minor_id = 0 , sd.referencing_class = 1 , d.type in ('u', 'v') , d.is_ms_shipped = 0 , d.[name] = @viewname) begin insert #dependencies(view_name) select v.[name] [view_name] sys.sql_expression_dependencies sd inner join sys.views v on sd.referencing_id = v.object_id inner join sys.objects d on sd.referenced_id = d.object_id sd.referencing_minor_id = 0 , sd.referenced_minor_id = 0 , sd.referencing_class = 1 , d.type in ('u', 'v') , d.is_ms_shipped = 0 , v.is_ms_shipped = 0 , isnull(d.[name], '') <> '' , d.[name] = @viewname , v.[name] not in (select view_name #dependencies ) end end; /* refresh view */ begin try --begin transaction exec sp_refreshview @viewname --commit transaction end try begin catch --rollback transaction if exists (select 1 [issimodel(15)].sys.objects [name] = @viewname) set @errors = @errors + char(13) + 'error: failed refreshview ' + @viewname + '. message: ' + error_message() end catch set @counter = @counter + 1; end; if isnull(@errors, '') <> '' raiserror (@errors, 16, 1) drop table #dependencies;
this occurs when use try\catch block in code: make sure has flow in code:
declare @errormum int declare @errormessage varchar(200) declare @severity int declare @state int begin try begin tran t1 <your code goes here> commit tran t1 end try begin catch if @@trancount > 0 begin rollback tran t1 end set @errormum = error_number() set @errormessage = error_message() set @severity = error_severity() set @state = error_state() raiserror(@errormessage, @severity, @state) end catch
Comments
Post a Comment