简体   繁体   English

SQL Server触发操作,没有游标

[英]SQL Server trigger operation with out cursor

I have a scenario where in I have to generate SQL script after every DML operation. 我有一种情况,我必须在每个DML操作之后生成SQL脚本。 We have a trigger that runs after every Insert, Update, Delete. 我们有一个触发器,该触发器在每次插入,更新,删除之后运行。

Primarily it takes data from Inserted/Deleted tables and generates a SQL script for each row and performs an insert in to another table. 首先,它从“插入/删除”表中获取数据,并为每一行生成一个SQL脚本,然后插入到另一个表中。

For example, If we insert 例如,如果我们插入

  1. Take all inserted values 取所有插入的值
  2. Open a cursor 打开游标
  3. For each inserted row, generate a SQL script that will be inserted in to another table for audit purpose 对于每个插入的行,生成一个SQL脚本,该脚本将插入到另一个表中以进行审核
  4. close cursor. 关闭光标。

This trigger is taking almost 4-5 hours for 10,000 rows of 5 columns. 10,000行5列需要花费近4-5个小时。 Columns are int , date and varchar(100) . 列为intdatevarchar(100)

We are trying to replace the trigger. 我们正在尝试替换触发器。 Need suggestions on same. 需要相同的建议。

Set @syncScript = 'Insert Into table('

            Declare insertCursor Cursor Local Forward_Only For
            Select a, b, c From Inserted

            Open insertCursor

            Fetch Next From insertCursor Into @a, @b, @c

            While (@@Fetch_Status = 0)
            Begin
                Declare @valuesList varchar(max)

                Set @columnList = ''
                Set @paramsList = '['
                Set @valuesList = ''

                Select @columnList = @columnList + 'col6, ', @paramsList = @paramsList + '''' + COALESCE(dbo.ConvertFloatToVarchar(col6), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.a= @a And I.b= @b And I.c= @c And 1 = 1
                Select @columnList = @columnList + 'col5, ', @paramsList = @paramsList + '''' + COALESCE(Convert(varchar(max), col5), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.a= @a And I.b= @b And I.c= @c And 1 = 1
                Select @columnList = @columnList + 'col4, ', @paramsList = @paramsList + '''' + COALESCE(Convert(varchar(max), col4), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.a= @a And I.b= @b And I.c= @c And 1 = 1
                Select @columnList = @columnList + 'col3, ', @paramsList = @paramsList + '''' + COALESCE(Convert(varchar(max), col3), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.a= @a And I.b= @b And I.c= @c And 1 = 1
                Select @columnList = @columnList + 'col2, ', @paramsList = @paramsList + '''' + COALESCE(Convert(varchar(max), col2), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.a= @a And I.b= @b And I.c= @c And 1 = 1
                Select @columnList = @columnList + 'col1, ', @paramsList = @paramsList + '''' + COALESCE(Convert(varchar(max), col1), 'NULL') + ''', ', @valuesList = @valuesList + '?, ' From Inserted I Where I.TraitValueMemberId = @a And I.ValueSetId = @b And I.LanguageCulture = @c And 1 = 1

                Set @columnList = SUBSTRING(@columnList, 1, Len(@columnList) - 1) + ') Values ('
                Set @valuesList = SUBSTRING(@valuesList, 1, Len(@valuesList) - 1) + ')'
                Set @paramsList = @paramsList + ']'
                Set @bigScript = (COALESCE(@syncScript, '') + COALESCE(@columnList, '') + COALESCE(@valuesList, ''))



                            Insert Into Log (CreatedDate, [Message]) Select GetDate(), 'tablename INSERT syncScript = ' + @bigScript +'paramsList = ' + COALESCE(@paramsList, '')

                Insert Into [dbo].[LogAudit] (GuidId, Query, Params, TableName, CreatedDate, Operation, UserId)
                Values ('00000000-0000-0000-0000-000000000000', @bigScript, @paramsList, @table, GetDate(), @operation, @debugUser)

                Fetch Next From insertCursor Into @a, @b, @c
            End

            Close insertCursor
            Deallocate insertCursor

In case of update we join across inserted and deleted tables to generate update script like shown above. 在进行更新的情况下,我们将跨插入和已删除的表联接以生成如上所示的更新脚本。

I resolved the issue using SET Operation rather than Trigger 我使用SET操作而不是触发器解决了该问题

INSERT Into [dbo].[LogAudit] (GuidId, Query, Params, TableName, CreatedDate, Operation, UserId)
            SELECT '00000000-0000-0000-0000-000000000000',  
                 'INSERT INTO Table(col6, col5, col4, col3, col2, col1) VALUES (?, ?, ?, ?, ?, ?)', 
            '[' +   '''' + COALESCE(dbo.ConvertFloatToVarchar(col6), 'NULL') + ''','  + '''' + COALESCE(Convert(varchar(max), col5), 'NULL') + ''', ' +  '''' + COALESCE(Convert(varchar(max), col4), 'NULL') + ''', ' + '''' + COALESCE(Convert(varchar(max), col3), 'NULL') + ''', ' + '''' + COALESCE(Convert(varchar(max), col2), 'NULL') + ''', ' +  '''' + COALESCE(Convert(varchar(max), col1), 'NULL') + ''', ' + ']', @table, GetDate(), @operation, @debugUser FROM Inserted

            -- Insert into Log
            Insert Into Log (CreatedDate, [Message]) Select GetDate(), 'MTDE_TraitValueMemberByLanguage_Sync INSERT 
                    syncScript = INSERT Into [dbo].[LogAudit (GuidId, Query, Params, TableName, CreatedDate, Operation, UserId) VALUES VALUES (?, ?, ?, ?, ?, ?)' + 
                    'paramsList = '  +  '[' +   '''' + COALESCE(dbo.ConvertFloatToVarchar(col6), 'NULL') + ''','  + '''' + COALESCE(Convert(varchar(max), col5), 'NULL') + ''', ' +  '''' + COALESCE(Convert(varchar(max), col4), 'NULL') + ''', ' + '''' + COALESCE(Convert(varchar(max), col3), 'NULL') + ''', ' + '''' + COALESCE(Convert(varchar(max), col2), 'NULL') + ''', ' +  '''' + COALESCE(Convert(varchar(max), col1), 'NULL') + ''', ' + ']' FROM Inserted

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM