Solved

SQL Trigger syntax

Posted on 2014-03-12
5
319 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
  • 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 142

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 142

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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

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.
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

747 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now