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
Solved

SQL Trigger syntax

Posted on 2014-03-12
5
328 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 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL group by query to return records with highest value 6 25
SQL Server 2008 R2, need a pivot/cross tab query... 4 29
SQL Select in Access 2003 3 26
Negative isnull? 3 15
Naughty Me. While I was changing the database name from DB1 to DB_PROD1 (yep it's not real database name ^v^), I changed the database name and notified my application fellows that I did it. They turn on the application, and everything is working. A …
In this article I will describe the Backup & Restore 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.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

829 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