Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6814909
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:44:40+00:00 2026-05-26T20:44:40+00:00

I have a table called Card that I am auditing with a shadow table

  • 0

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.

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

I am trying to do this via this here 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();
    }
}

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T20:44:41+00:00Added an answer on May 26, 2026 at 8:44 pm

    Not sure myself, but whe you alter the table the trigger is stuffed, probably narked as invalid or something.

    There is an a parameter in alter table comand to disable and enable triggers
    so maybe disable trigger(s) alter table, alter trigger, enable triggers would be a goer.

    Given what you are doing though I’d just drop the trigger and then create it again…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Table called Address. I have a Trigger for insert on that
I have a table called Users that has a column called deleted, a boolean
I have table called 'shipped_data' https://i.stack.imgur.com/JXBej.png and need go get all 'caseNumbers' that match
i have table called users , if i want to delete some user (User
I have table called Table A and I have created a trigger that handles
Have a table called user_products that contains a record for each product the user
I have table called stats . In am inserting yes or no in the
I have a table called OffDays, where weekends and holiday dates are kept. I
I have a Table called Product and I have the Table StorageHistory . Now,
I have a table called ApprovalTasks... Approvals has a status column I also have

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.