Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Write a TRIGGER based on the attribute / field value being insert

Posted on 2013-05-16
3
Medium Priority
?
289 Views
Last Modified: 2013-05-20
I would like to write a MS-SQL Server TRIGGER based on a particular Field value and not sure if it is possible or not.

For example if (and Only if ) someone or any application has insert a record with a certain field (e.g. Field A) value is "U", then this event will fire this trigger, is it possible on MS-SQL ?

My company are using an MS-SQL Server 2008 Express Edition.

Thank you so much for your help in advance.
0
Comment
Question by:patricktam
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39170692
CREATE TRIGGER triggername
ON tablename
FOR INSERT
AS
BEGIN
  if exists (select myfield from inserted where myfield = 'U')
  begin
    -- do stuff
  end
END
0
 
LVL 21

Expert Comment

by:Alpesh Patel
ID: 39171279
create instead of trigger and check if the inserted value is as expected then move ahead to insert record in table.


CREATE TRIGGER triggername
ON tablename
INSTEAD OF INSERT
AS
BEGIN
  if exists (select myfield from inserted where myfield = 'U')
  begin
    -- do stuff
  end
END
0
 
LVL 70

Accepted Solution

by:
Scott Pletcher earned 1500 total points
ID: 39172133
I don't think you can base whether the trigger fires or not on a column value: the trigger will always fire.

But you could have the trigger exit if the specific column you're concerned with was not modified.


CREATE TRIGGER triggername
ON dbo.tablename
AFTER INSERT
AS
SET NOCOUNT ON
IF NOT EXISTS(SELECT 1 FROM inserted WHERE certain_column = 'certain_value')
    --no row has the column value I'm concerned about, so exit the trigger immediately
    RETURN
--at least one row has the "trigger" value, do related processing here
--...processing code here
GO
0

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, when I was asked to create a new SQL 2005 cluster, Microsoft released a new service pack for MS SQL 2005 what is Service Pack 3. When I finished the installation of MS SQL 2005 I found myself troubled why the installation of SP3 failed …
by Mark Wills Attending one of Rob Farley's seminars the other day, I heard the phrase "The Accidental DBA" and fell in love with it. It got me thinking about the plight of the newcomer to SQL Server...  So if you are the accidental DBA, or, simp…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Is your data getting by on basic protection measures? In today’s climate of debilitating malware and ransomware—like WannaCry—that may not be enough. You need to establish more than basics, like a recovery plan that protects both data and endpoints.…

610 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question