SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Adding a trigger for new Customer Order


Adding a trigger for new Customer Order

Author
Message
Gillian Pappas
Gillian Pappas
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
I need a trigger for when a new Customer Order Header (table) is added, then I want it to add a new record onto an OSR table based on information
stored on the newly created Customer Order Header.
drew.allen
drew.allen
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24494 Visits: 11782
If you have a specific problem, it would help if you specified what it was. If you just want to know how to get started, look up CREATE TRIGGER in Books-On_Line.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Gillian Pappas
Gillian Pappas
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
Hi Drew. Thank you for your response.

This is my first trigger ever. I have looked at all of the forums on triggers, but I have not been able to find one that does what I want.

Specifically, when a user has added a new Customer Order record, (which is on the CO table), I want the trigger to automatically add/insert a new
record onto another table called the OSR table. I need to pass the information from the newly created Customer Order header record and insert that information into
a new record which does not yet exist on the OSR table.

I have looked at triggers for insert instead, which almost look like it will do what I want it to do, but am unsure.

Thanks very much again, for your help.

Gillian
Gillian Pappas
Gillian Pappas
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
Drew. This is what I have so far.....

SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
ALTER TRIGGER dbo.KS_CoAdd
ON co
FOR INSERT, UPDATE
AS
declare
@Severity int
, @InsertFlag tinyint
, @Infobar InfobarType


IF @@ROWCOUNT = 0 RETURN

-- Skip trigger operations as required.
DECLARE @SkipBaseTrigger bit
SET @SkipBaseTrigger = dbo.SkipBaseTrigger()
IF @SkipBaseTrigger = 1 or dbo.SkipAllUpdate() = 1
RETURN

DECLARE @Today DateType
SET @Today = dbo.midnightof(GETDATE())

SET @Severity = 0

SELECT
@InsertFlag = CASE
WHEN EXISTS ( SELECT 1 FROM deleted (NOLOCK)) THEN 0 --KSC02: Added (NOLOCK)
ELSE 1
END

Begin

select ksosrs where ksosrs.oco_num = co.co_num

if not exists (select 1 from ksosrs with (NOLOCK)
-- where ks_CoBal.co_num = @CoNum
-- and ks_CoBal.BalanceDate = @today
-- and ks_CoBal.co_line = @coline)
insert into ksosrs
(Oco_num
, OBy
, Ostatus
, Osdes
, OByDate
)
values
( CO.Co_Num
, @UserName
, 'OE_NEW'
, 'NEW ORDER'
, @today
, @today
)
end


IF @Severity != 0
BEGIN
EXEC dbo.RaiseErrorSp @Infobar, @Severity, 1

EXEC @Severity = dbo.RollbackTransactionSp
@Severity

IF @Severity != 0
BEGIN
ROLLBACK TRANSACTION
RETURN
END
END

/*============ PROCESSING SECTION ============*/




GO
SET QUOTED_IDENTIFIER OFF
GO
SET ANSI_NULLS ON
GO
drew.allen
drew.allen
SSC-Insane
SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)SSC-Insane (24K reputation)

Group: General Forum Members
Points: 24494 Visits: 11782
The main issue is that you are using
INSERT INTO TABLE(Fields)
VALUES(Values)


when you should be using
INSERT INTO TABLE(Fields)
SELECT expressions
FROM INSERTED



I would also recommend using TRY...CATCH blocks.

There are also undefined variables in your trigger. You CANNOT directly pass variables from your main insert to the trigger. There are ways to get around that, but the best approach is to only use values from the INSERTED records.

You may want to have separate INSERT and UPDATE triggers so that you don't need to test whether there are any records in the DELETED table.

I also hate the RETURN in your code. It should be
IF @@ROWCOUNT > 0
BEGIN
<rest of code...>
END



But that's not even necessary, because there won't be any records in the INSERTED table if there were no records inserted/updated.

Drew

J. Drew Allen
Business Intelligence Analyst
Philadelphia, PA
How to post data/code on a forum to get the best help.
How to Post Performance Problems
Make sure that you include code in the appropriate IFCode tags, e.g. [code=sql]<your code here>[/code]. You can find the IFCode tags under the INSERT options when you are writing a post.
Gillian Pappas
Gillian Pappas
Forum Newbie
Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)Forum Newbie (4 reputation)

Group: General Forum Members
Points: 4 Visits: 12
Hi Drew.

Thank you for your help. I will make changes to the trigger and let you know how they work out.

I appreciate your help, very much.

Sincerely;

Gillian Pappas
Systems Analyst
Kewaunee Scientific Corp.
Statesville, NC
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search