Solved

How do I access values from the inserted and deleted tables ,when wrting a trigger

Posted on 2009-05-18
4
154 Views
Last Modified: 2012-05-07
How do I access values from the inserted and deleted tables ,when wrting a trigger?
Consider the following trigger written on table CONFIG_PROJECTS.The trigger is created successfully but doesnt fire.


ALTER TRIGGER [dbo].[UTRG_InitializeResources] 
   ON   [dbo].[CONFIG_PROJECTS] 
   FOR INSERT,UPDATE 
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
 
    -- Insert statements for trigger here
 
declare @pmcode varchar(10)
select @pmcode =(select str_empcode_PM  from inserted)
if (@pmcode  != null)
insert into TRANS_RESOURCES values(4,@pmcode,null,'WSZ001',40.3,1)
 
END

Open in new window

0
Comment
Question by:MaithreeD
  • 2
4 Comments
 
LVL 1

Assisted Solution

by:venkatca
venkatca earned 150 total points
ID: 24419217
remove this line and it should work

if (@pmcode  != null)
0
 
LVL 1

Assisted Solution

by:venkatca
venkatca earned 150 total points
ID: 24419230
if you still need to check null values try this instead

if isnull(@pmcode,'')<>''
0
 
LVL 22

Assisted Solution

by:pivar
pivar earned 150 total points
ID: 24419235
Hi,

Since an insert/update can affect multiple rows change your trigger like this. Otherwise this should run fine on inserts and update to the table CONFIG_PROJECTS where str_empcode_PM
is not null.

/peter


ALTER TRIGGER [dbo].[UTRG_InitializeResources] 
   ON   [dbo].[CONFIG_PROJECTS] 
   FOR INSERT,UPDATE 
AS 
BEGIN
        -- SET NOCOUNT ON added to prevent extra result sets from
        -- interfering with SELECT statements.
        SET NOCOUNT ON;
 
    -- Insert statements for trigger here
 
insert into TRANS_RESOURCES 
select 4,str_empcode_PM,null,'WSZ001',40.3,1  
from inserted
where str_empcode_PM!= null
 
END

Open in new window

0
 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 200 total points
ID: 24419267
where str_empcode_PM!= null
will not work, unless you had ANSI_NULLS set to OFF.

instead:
where str_empcode_PM IS NOT null
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
What is wrong with the below insert statement. Getting error when executing. 5 34
SQL Server syntax question 13 32
SYbase 4 31
SQL Insert parts by customer 12 34
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Confronted with some SQL you don't know can be a daunting task. It can be even more daunting if that SQL carries some of the old secret codes used in the Ye Olde query syntax, such as: (+)     as used in Oracle;     *=     =*    as used in Sybase …
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

777 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