Note that there are some explanatory texts on larger screens.

plurals
  1. POaltering a trigger to use column created within the same transaction that contains the trigger alter
    primarykey
    data
    text
    <p>I have a table called Card that I am auditing with a shadow table called Card_shadow. The shadow table gets a new row via insert, update and delete triggers on the Card table anytime a row is inserted, updated, or deleted accordingly. </p> <p>Now comes time to add a column to the card table. As I see it, I need to write some repeatable SQL that can be run in an existing transaction that will: 1. Add the column to the card table and the shadow table, and 2. Alter the triggers to use the new column</p> <p>I am trying to do this via this here code:</p> <pre><code>private string addVoidColumn(SqlConnection db, SqlTransaction transaction) { string sql = @" IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Card' AND COLUMN_NAME = 'c_void_d') ALTER TABLE [dbo].[Card] DROP COLUMN [c_void_d] IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'Card_shadow' AND COLUMN_NAME = 'c_void_d') ALTER TABLE [dbo].[Card_shadow] DROP COLUMN [c_void_d] ALTER TABLE dbo.Card ADD c_void_d datetime NULL ALTER TABLE dbo.Card_shadow ADD c_void_d datetime NULL"; executeNonQuery(sql, db, transaction); sql = @"ALTER TRIGGER [dbo].[tr_Card_Update] ON [dbo].[Card] FOR UPDATE AS INSERT INTO dbo.Card_shadow(c_id_n,c_void_d,AuditAction) SELECT c_id_n,c_void_d,'U' FROM Inserted"; executeNonQuery(sql, db, transaction); sql = @"ALTER TRIGGER [dbo].[tr_Card_Insert] ON [dbo].[Card] FOR INSERT AS INSERT INTO dbo.Card_shadow(c_id_n,c_void_d,AuditAction) SELECT c_id_n,c_void_d,'U' FROM Inserted"; executeNonQuery(sql, db, transaction); sql = @"ALTER TRIGGER [dbo].[tr_Card_Delete] ON [dbo].[Card] FOR DELETE AS INSERT INTO dbo.Card_shadow(c_id_n,c_void_d,AuditAction) SELECT c_id_n,c_void_d,'U' FROM Deleted"; executeNonQuery(sql, db, transaction); } private static void executeNonQuery(string sql, SqlConnection db, SqlTransaction transaction) { executeNonQuery(sql, db, transaction, 300); } private static void executeNonQuery(string sql, SqlConnection db, SqlTransaction transaction, int timeoutInSeconds) { using(SqlCommand cmd = new SqlCommand(sql,db,transaction)) { cmd.CommandTimeout = timeoutInSeconds; cmd.ExecuteNonQuery(); } } </code></pre> <p>This method is passed an open connection, and an existing transaction. When the calling code commits the transaction, it runs fine and throws no exceptions, however the triggers do not get altered. And this is where I'm stumped. I have a suspicion that the column adds need to be committed before the triggers can be altered, but i'm not sure. Any thoughts?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload