找到原始记录并仅更改不插入的记录

发布于 2024-12-10 17:57:00 字数 1543 浏览 0 评论 0原文

我已经尝试过,但到目前为止还没有成功。由于我对查询的了解有限,我想我最好将其发布在这里。

我有具有以下结构的学生表

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

我有许多学生,他们有 ID,studentName(sname),status('active' 或 'not-active'), renew('no' 表示新学生,yes' 表示续订学生)和入学日期。

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')

现在我有一个 INSERT 触发器,它应该在更新学生时停用旧学生。因此,如果我插入以下将续订设置为“是”的内容,它应该使现有记录变为“非活动”。

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

我编写了这个 INSERT 触发器,它可以工作,但它会使旧的和新的插入记录都处于非活动状态。我只想停用原始记录。此外,原始记录和插入记录之间不仅注册日期和新字段不同,其余字段都相同。如何解决这个问题?这是我的触发器

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

请注意,这非常接近我的问题。谢谢 在此处输入图像描述

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

她说她爱他 2024-12-17 17:57:00

将您的更新更改为:

UPDATE students 
    SET status = 'Inactive'

    FROM Inserted i
        INNER JOIN students T2
            ON i.sname = T2.sname
            AND i.id <> t2.id

这将检查您正在更新的行不是新插入的行。

Change your update to this:

UPDATE students 
    SET status = 'Inactive'

    FROM Inserted i
        INNER JOIN students T2
            ON i.sname = T2.sname
            AND i.id <> t2.id

This checks that the row you are updating is NOT the newly inserted row.

不乱于心 2024-12-17 17:57:00

你看过@@identity吗?几年前,我处理过类似的事情,并使用 @@identity 来获取最后创建的身份值;基本上获取最新的身份值,然后设置除通过 @@identity 返回的 ID 之外的所有符合条件的记录。

在这里阅读有关身份值获取者的信息:
http://blog.sqlauthority.com/2007/03/25/sql-server-identity-vs-scope_identity-vs-ident_current-retrieve-last-inserted-identity-of-record/

已添加:您对插入表的看法是正确的。如果您不想/不能使用插入的表,您的触发器可能如下所示。

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

注:凭记忆写的,未经测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文