TR_BUSINESSPROCESSOUTPUT_DELETE

Definition

Copy


                CREATE trigger [dbo].[TR_BUSINESSPROCESSOUTPUT_DELETE] on [dbo].[BUSINESSPROCESSOUTPUT]
                with execute as owner
                after delete
                not for replication
                as
                    set nocount on;

                    declare @TABLENAME nvarchar(128);
                    declare @SQL nvarchar(300);

                    declare deleted_businessprocessoutput_cursor cursor local fast_forward for
                        select TABLENAME from DELETED order by DATEADDED desc,TSLONG desc;

                    open deleted_businessprocessoutput_cursor;

                        fetch next from deleted_businessprocessoutput_cursor into @TABLENAME;

                        while @@fetch_status = 0 begin
                            --JamesWill 269654-030907 2007/03/09 Only try to drop tables that actually exist

                            if exists (select 1 from INFORMATION_SCHEMA.TABLES 
                                        where TABLE_NAME = @TABLENAME
                                        and TABLE_TYPE = 'BASE TABLE')
                            begin
                                set @SQL = 'drop table dbo.[' + @TABLENAME + ']';
                                exec sp_executesql @SQL;
                            end

                            fetch next from deleted_businessprocessoutput_cursor into @TABLENAME;
                        end


                    --When a cursor is used, it should be explicitly closed/deallocated in case of blocking or running long

                    close deleted_businessprocessoutput_cursor;
                    deallocate deleted_businessprocessoutput_cursor;