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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T06:56:40+00:00 2026-05-30T06:56:40+00:00

Say for example, one has a table with certain rows, how does one prevent

  • 0

Say for example, one has a table with certain rows, how does one prevent updates to certain rows based on a condition equalling true but allow the update to commit on all rows where the condition is false.

take this example where I “lock” all rows prior to 1/4/2007 by aborting the whole transaction

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
SET ANSI_PADDING ON
GO
CREATE TABLE [dbo].[tbl_TriggerTest] (
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Value] [varchar](25) NULL,
    [Date] [datetime] NULL,
 CONSTRAINT [PK_tbl_TriggerTest] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
SET ANSI_PADDING OFF
GO
SET IDENTITY_INSERT [dbo].[tbl_TriggerTest] ON 

GO
INSERT [dbo].[tbl_TriggerTest] ([ID], [Value], [Date]) VALUES (1, N'12', CAST(0x0000979A00000000 AS DateTime))
GO
INSERT [dbo].[tbl_TriggerTest] ([ID], [Value], [Date]) VALUES (2, N'13', CAST(0x00009A7500000000 AS DateTime))
GO
INSERT [dbo].[tbl_TriggerTest] ([ID], [Value], [Date]) VALUES (3, N'14', CAST(0x00009BE200000000 AS DateTime))
GO
INSERT [dbo].[tbl_TriggerTest] ([ID], [Value], [Date]) VALUES (4, N'4', CAST(0x00009D4F00000000 AS DateTime))
GO
SET IDENTITY_INSERT [dbo].[tbl_TriggerTest] OFF
GO
CREATE TRIGGER [dbo].[LockOldWelshRows] ON [dbo].[tbl_TriggerTest]
    FOR UPDATE
AS
    BEGIN
        DECLARE @Count INT
        SELECT  @Count = COUNT([ID])
        FROM    INSERTED
        WHERE   [Date] < CONVERT(DATETIME, '01/04/2007 00:00:00', 103)

        IF @Count > 0 
            BEGIN
                RAISERROR('Rows prior to 01/04/2007 are locked',16,1) 
                ROLLBACK TRANSACTION
                RETURN ;
            END
    END

GO

If one was to run the following:

UPDATE [tbl_TriggerTest] SET [Value] = [Value] + 'M'

The transaction would fail with error:

Msg 50000, Level 16, State 1, Procedure LockOldWelshRows, Line 12
Rows prior to 01/04/2007 are locked
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.

Is there a way of modifying this trigger to allow the transaction to commit, but only rows where the date is > 1/4/2007

This is a VERY brief example (the tables i am working with are far more complex), and if i am honest, i think it’s cleaner if the whole transaction fails, i was just curious as to how it could be done.

  • 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-30T06:56:41+00:00Added an answer on May 30, 2026 at 6:56 am

    You’d need a BEFORE aka INSTEAD OF trigger and filter to allowed rows only.

    Untested:

    CREATE TRIGGER [dbo].[LockOldWelshRows] ON [dbo].[tbl_TriggerTest]
        INSTEAD OF UPDATE
    AS
        BEGIN
    
          UPDATE 
             T
          SET 
             T.SomeCol = I.SomeCol...
          FROM
             [dbo].[tbl_TriggerTest] T
             JOIN 
             INSERTED I ON T.keycol = I.keycol 
          WHERE
             T.[Date] >= '20070401';
    
        END
    
    GO
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a complex one. But I have a table which has a DATETIME
My query is based on the following example I have table say 'Table1'. Table1
How would I run both of them under one main website, say www.example.com, which
A simple example: Let's say I have one alias being sourced somewhere as: alias
I have 2 websites, lets say - example.com and example1.com example.com has a database
Performance consideration: Spread rows in multiple tables vs concentrate all rows in one table.
As in title. I have one column which has some text values. For example
Say I have Table A that has many Table B's. Table B has only
We have a SQL server 2008 and one of the tables, say table A
How do you elegantly format a timespan to say example 1 hour 10 minutes

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.