September 2, 2010 at 1:11 pm
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.
September 2, 2010 at 1:27 pm
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
September 2, 2010 at 1:34 pm
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
September 2, 2010 at 1:46 pm
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
September 2, 2010 at 2:26 pm
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
September 2, 2010 at 2:33 pm
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
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply