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 7947323
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T01:23:55+00:00 2026-06-04T01:23:55+00:00

Given a column with bit type in SQL Server 2008, how can I write

  • 0

Given a column with bit type in SQL Server 2008, how can I write a trigger to allow updates from 0 to 1, but disallow updating from 1 to 0?

In other words, once the bit is set to 1, it should always be 1.

The trigger must work for multiple updates, e.g. :

UPDATE Table SET BitField = 0

Should fail for any row where BitField = 1.

EDIT: To give some background, the bit/flag in question marks whether or not a monetary transaction needs to be processed. If the bit =1, the transaction has already been processed. If the bit is reset to 0, the transaction may be duplicated, so I need to enforce at the database level that the bit can not be reset to 0.

I need to protect against direct queries run against the database as well as application level bugs. I can’t be sure that a stored procedure will always be used to update the table, so I believe a trigger is the only way to enforce this logic.

  • 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-06-04T01:23:56+00:00Added an answer on June 4, 2026 at 1:23 am

    Looks like you are in need of a simple after trigger

    CREATE TABLE YourTable(
        PK int Primary key,
        bitCol bit
    )
    
    CREATE TRIGGER YourTableTrigger
       ON  YourTable
       AFTER UPDATE
    AS 
    
        DECLARE @nrOfViolations int 
    
        select @nrOfViolations = count(*) from deleted  d
        join YourTable t on d.PK = t.PK
        where d.bitCol = 1 and t.bitCol = 0
    
        if @nrOfViolations > 0
        BEGIN
            RAISERROR('Failed', 16, 1);
            ROLLBACK TRANSACTION
        END 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given a columns' index, how can you get an Excel column name? The problem
OK, I am using SQL Server 2008 R2 and have a couple of relations
How can I Select the Sum of the ProjectPossibilityRatio column from the ProjectCompletion table
In SQL Server 2005, given the following resultset ID | InstanceNumber | IsArchived 5000
I've got a column in my Microsoft SQL CE 3.5 SP2 database called Type
I have a nullable DateTime column in my SQL Server 2005 table called DateTimeDeleted.
Column: pwdhash Type: char Many web pages give me the bit size, not a
Given the following table in SQL Server 2005: ID Col1 Col2 Col3 -- ----
I was looking into sorting tables by a column designated given some input, and
Given a table where the first column is seconds past a certain reference point

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.