Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Adding a trigger for new Customer Order Expand / Collapse
Author
Message
Posted Thursday, September 2, 2010 1:11 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 6, 2010 2:10 PM
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.

Post #979843
Posted Thursday, September 2, 2010 1:27 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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
Post #979851
Posted Thursday, September 2, 2010 1:34 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 6, 2010 2:10 PM
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
Post #979856
Posted Thursday, September 2, 2010 1:46 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 6, 2010 2:10 PM
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

Post #979867
Posted Thursday, September 2, 2010 2:26 PM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, September 3, 2013 8:24 AM
Points: 1,240, Visits: 5,421
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
Post #979900
Posted Thursday, September 2, 2010 2:33 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, October 6, 2010 2:10 PM
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

Post #979907
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse