Solved

SQL Trigger syntax

Posted on 2014-03-12
5
332 Views
Last Modified: 2014-03-12
I have a table and want to create a trigger that sets a field to "Y" on insert.

Table name APSupplier
Field name OnHold

Can anyone help with syntax?
0
Comment
Question by:HKFuey
[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
  • 2
  • 2
5 Comments
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39923000
Assuming the field is in the table that the trigger is on just set a default value on the field
0
 
LVL 25

Expert Comment

by:Lee Savidge
ID: 39923003
Sorry, posted a bit too early there. To set a default value on a field in a table.

ALTER TABLE APSupplier
ALTER COLUMN OnHold SET DEFAULT 'Y'

Open in new window


This will set the field value to Y for every new record inserted.
0
 
LVL 143

Expert Comment

by:Guy Hengel [angelIII / a3]
ID: 39923016
note: the default will ONLY work if the value is not explicitly set to some value during the insert ...
so, if you want to actually override in such cases, to make sure you have 100% on insert => Y in that field, you indeed need a trigger. otherwise, the above is just fine and more efficient
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 39923019
if you need a trigger, you will need a primary key on the table. say it's named PK
CREATE TRIGGER after_insert_table1 ON yourtable 
AFTER INSERT  
AS
BEGIN
  UPDATE t 
      set t.OnHold = 'Y'
     FROM yourtable t
   WHERE t.PK in ( SELECT i.PK FROM INSERTED )
          and ( t.OnHold IS NULL OR t.OnHold <> 'Y' )
 END 

Open in new window

0
 

Author Closing Comment

by:HKFuey
ID: 39923087
Thanks Guy, that is spot on!
0

Featured Post

NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

Question has a verified solution.

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

If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

635 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