找到原始记录并仅更改不插入的记录
我已经尝试过,但到目前为止还没有成功。由于我对查询的了解有限,我想我最好将其发布在这里。
我有具有以下结构的学生表
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将您的更新更改为:
这将检查您正在更新的行不是新插入的行。
Change your update to this:
This checks that the row you are updating is NOT the newly inserted row.
你看过@@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/
已添加:您对
插入
表的看法是正确的。如果您不想/不能使用插入的表,您的触发器可能如下所示。注:凭记忆写的,未经测试。
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.Note: written from memory and not tested.