Solved

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

Posted on 2009-05-18
4
152 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

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.

Join & Write a Comment

Introduction Hopefully the following mnemonic and, ultimately, the acronym it represents is common place to all those reading: Please Excuse My Dear Aunt Sally (PEMDAS). Briefly, though, PEMDAS is used to signify the order of operations (http://en.…
Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

708 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

13 Experts available now in Live!

Get 1:1 Help Now