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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T10:29:39+00:00 2026-05-25T10:29:39+00:00

I have this trigger : CREATE trigger [dbo].[DeriveTheAge] on [dbo].[Student] after insert,update as begin

  • 0

I have this trigger :

CREATE trigger [dbo].[DeriveTheAge] on [dbo].[Student]
after insert,update
as
begin
    declare @sid as int;
    declare @sdate as date;
    select @sid= [Student ID] from inserted;
    select @sdate=[Date of Birth] from inserted;
    commit TRANSACTION
    if(@sdate is not null)
    begin
        update Student set Age=DATEDIFF(YEAR,@sdate,GETDATE()) where [Student ID]=@sid;
    end
    print 'Successfully Done'
end

as it suggests, the trigger automatically calculates the Derived attribute “Age” from the date of birth. But I get this error when I do the insert :

(1 row(s) affected)
Successfully Done
Msg 3609, Level 16, State 1, Line 1
The transaction ended in the trigger. The batch has been aborted.

Initially I avoided this error because the rows were getting updated inspite of the error. But now when I am inserting a record from the FORNT END, the record is not updated. Instead, it throws this exception : enter image description here

Can anyone please help me out?

btw, mine is SQL Server 2008 R2 and Visual Studio 2010.

CORRECTION : The Records are still getting updated. But the Exception is the Vilan.

Update

CREATE TRIGGER [dbo].[DeriveTheAge] 
ON [dbo].[Student]
FOR INSERT, UPDATE
AS
BEGIN
    UPDATE s 
      SET Age = DATEDIFF(YEAR, [Date of Birth], CURRENT_TIMESTAMP)
      FROM dbo.Student AS s
      INNER JOIN inserted AS i
      ON s.[Student ID] = i.[Student ID]
      WHERE i.[Date of Birth] IS NOT NULL;
      commit transaction
END
GO
  • 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-25T10:29:40+00:00Added an answer on May 25, 2026 at 10:29 am

    Why are you committing in the trigger? Why are you not handling multi-row inserts or updates? You can’t just declare variables and assign them from inserted – what values do you think will get assigned when you update 2, or 15, or 6000 rows?

    CREATE TRIGGER [dbo].[DeriveTheAge] 
    ON [dbo].[Student]
    FOR INSERT, UPDATE
    AS
    BEGIN
        UPDATE s 
          SET Age = DATEDIFF(YEAR, [Date of Birth], CURRENT_TIMESTAMP)
          FROM dbo.Student AS s
          INNER JOIN inserted AS i
          ON s.[Student ID] = i.[Student ID]
          WHERE i.[Date of Birth] IS NOT NULL;
    END
    GO
    

    That all said, why on earth would you need a trigger to calculate someone’s age? You can get this from the birth date right now at query time and know that it will be accurate, unlike this stale value you’ve stored in the table. Note that if their row is not updated for over a year, the age you’ve put in the table is out of date. When do you go back and update the Age for all rows in the table? Once a day? Anything less and your Age column is completely unreliable and pointless.

    Also, DATEDIFF(YEAR is not a reliable way to calculate age in the first place. All it does is count the number of year boundaries that have been crossed, it has no idea if the person’s actual birthday is Jan 1 or Dec 31 or anywhere in between.

    Finally, I wouldn’t print from the trigger. Who is going to consume that print statement when you’re not debugging?

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

Sidebar

Related Questions

I have this trigger: create or replace trigger t_calctotal after insert or update on
This is what I currently have: CREATE OR REPLACE TRIGGER MYTRIGGER AFTER INSERT ON
I've defined table with this schema : CREATE TABLE [dbo].[Codings] ( [Id] [int] IDENTITY(1,1)
I have a table that has a Trigger associated with it for Update, Insert
I'm new to triggers . I have table like this CREATE TABLE [dbo].[Positions]( [Id]
I have a table that looks like this: CREATE TABLE [dbo].[SomeTable]( [Guid] [uniqueidentifier] NOT
I have something like this create function Answers_Index(@id int, @questionID int) returns int as
I have a trigger that executes a function on table insert or update. It
I have this Trigger in Postgresql that I can't just get to work (does
I have this piece of logic I would like to implement as a trigger,

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.