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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T11:50:27+00:00 2026-05-26T11:50:27+00:00

I have tried it but not successful so far. Since my knowledge in query

  • 0

I have tried it but not successful so far. Since my knowledge in query is limited, I thought I will better post it here.

I have students table with the following structure

create table students(
id int not null primary key identity,
sname varchar(25),
status varchar(25),
renew varchar(15),
enrollment datetime,
)

I have a number of students who has an ID, studentName(sname),status(‘active’ or ‘not-active’), renew(‘no’ for new student, yes’ for renewed student) and enrollment date.

insert into students values('jay','active','no','2010-01-01')
insert into students values('Phil','active','no','2010-01-01')
insert into students values('Cru','active','no','2010-01-01')
insert into students values('slow','active','no','2010-01-01')
insert into students values('true','active','no','2010-01-01')
insert into students values('false','active','no','2010-01-01')

Now I have an INSERT Trigger which is suppose to deactive an old student when a student is renewed. So if I insert the following which has renewal set to ‘yes’, it should make the already existing record ‘inactive’.

insert into students values('false','active','yes','2011-01-01')

I wrote this INSERT Trigger and it works but it in-actives the old and the new inserted record both. I want only the original record to be inactivated. Also not that only enrollment date and nenew fields are different, the rest are the same between original and insert records. How to fix this? Here is my trigger

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE TRIGGER [dbo].[tr_renew_student]
ON [dbo].students
-- WITH ENCRYPTION
FOR INSERT
-- WITH APPEND
-- NOT FOR REPLICATION
AS
-- insert sql here
if exists(select * from inserted where Renew = 'yes')
BEGIN

UPDATE students 
    SET status = 'Inactive'

    FROM Inserted i
        INNER JOIN students T2
            ON i.sname = T2.sname

END

Note that this is close approximation to my problem. Thank you
enter image description here

  • 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-26T11:50:28+00:00Added an answer on May 26, 2026 at 11:50 am

    Have you looked at @@identity? Years back I dealt with something similar and used @@identity to get the last created identity value; basically getting the latest identity value then setting all the records matching the criteria except the one with the ID returned via @@identity.

    Read about the identity value getters here:
    http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

    Added: You’re right about the inserted table. If you didn’t want to / can’t use the inserted table, your trigger could look something like this.

    SELECT @@identity //  <- this gets the last identity value inserted.
    
    UPDATE students
    SET status = 'Inactive'
    WHERE students.name = (SELECT name FROM students WHERE id = @@identity)
      AND id <> @@identity
    

    Note: written from memory and not tested.

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

Sidebar

Related Questions

I have tried this but it does not work (even if I specify .wav
i have tried this code to redirect a php page.but it s not working
I have tried rectifying the code below. But I am not able to find
I have tried using the accordion plugin, but it does not work, and I
I have tried using a stemmer but the words it produces are just not
I have searched a lot and tried much but I can not find the
I have tried this but it doesn't work: tell application Preview set myfile to
How can I join 2 Point3f arrays together? I have tried this but it
i have tried looking online but had no luck, How i could delete all
I have tried the following but it seems the folder gets created on the

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.