Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2008 Forums
 Transact-SQL (2008)
 Easy problem - need to add one condition

Author  Topic 

scholin
Yak Posting Veteran

56 Posts

Posted - 2009-09-02 : 15:37:22
I admit I am a newbie. I have the following trigger working just fine to add a new record into another table but I am stuck on how to add one condition.

I want this trigger to fire only if the column SSEP=True in the dbo.patient table.

How would I add this to this trigger?

Thank you!


USE [USMON]
GO
/****** Object: Trigger [dbo].[Create_New_Ssep_Rec] Script Date: 09/01/2009 19:20:48 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER TRIGGER [dbo].[Create_New_Ssep_Rec]
ON [dbo].[Patient]
After INSERT
AS
insert dbo.SSEP_Result(Patient_ID)
select DISTINCT Patient_ID from inserted i
where not exists (select * from dbo.SSEP_Result where Patient_ID = i.Patient_ID)

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-02 : 16:07:20
if exists(select * from inserted where SSEP = 'True')
insert dbo.SSEP_Result(Patient_ID)
select DISTINCT Patient_ID from inserted i
where not exists (select * from dbo.SSEP_Result where Patient_ID = i.Patient_ID)

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page

scholin
Yak Posting Veteran

56 Posts

Posted - 2009-09-02 : 16:48:54
Tara,

I love it when these things work out on the very first try!

That is exactly it - thank you.

Mike
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2009-09-02 : 17:09:58
You're welcome.

You could also add the EXISTS to the SELECT query, either way it should be the same.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog

"Let's begin with the premise that everything you've done up until this point is wrong."
Go to Top of Page
   

- Advertisement -