Solved

SQL trigger- only insert select details

Posted on 2008-06-20
2
689 Views
Last Modified: 2008-06-28
I have a trigger shown in the snippet,

It inserts the records in opdetm joined by opheadm.order_no = opdetm.order_no, into opdetm_audit when opheadm's status is updated to 9,

what i want to add now is that it will only add the opdetm records that do not exist in opdetm_audit2 (this is not a typo, there is an audit and an audit2 table)
joined by
opdetm.order_no = opdetm_audit2.order_no and
opdetm.order_line_no = opdetm_audit2.order_line_no,

How would this be done?
ALTER TRIGGER [scheme].[metauddel]
   ON  [scheme].[opheadm] 
   AFTER UPDATE
AS 
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;
IF UPDATE (status)
 
declare @Status char(1)
declare @OrderNo char (10)
declare @OrderLineNo char(1)
declare @DateEntered datetime
declare @Exist float
 
select @Status = [status]from inserted with (NOLOCK)
select @OrderNo = order_no from inserted with (NOLOCK)
select @DateEntered = date_entered from inserted with (NOLOCK)
 
if @Status = 9 
	Begin
		Insert into opdetm_audit (order_no,order_line_no,line_type,order_line_status,order_qty,despatched_qty,allocated_qty,val) 
Select 
		order_no,
		order_line_no,
		line_type,
		order_line_status,
		order_qty,
		despatched_qty,
		allocated_qty,
		val
 
from [scheme].[opdetm]with (NOLOCK) where order_no = @OrderNo
and @DateEntered < CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME)
 
	End
 
END

Open in new window

0
Comment
Question by:meteorelec
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 21829751
ALTER TRIGGER [scheme].[metauddel]
   ON  [scheme].[opheadm]
   AFTER UPDATE
AS
BEGIN
      -- SET NOCOUNT ON added to prevent extra result sets from
      -- interfering with SELECT statements.
      SET NOCOUNT ON;
IF UPDATE (status)
 
declare @Status char(1)
declare @OrderNo char (10)
declare @OrderLineNo char(1)
declare @DateEntered datetime
declare @Exist float
 
select @Status = [status]from inserted with (NOLOCK)
select @OrderNo = order_no from inserted with (NOLOCK)
select @DateEntered = date_entered from inserted with (NOLOCK)
 
if @Status = 9
      Begin
            Insert into opdetm_audit (order_no,order_line_no,line_type,order_line_status,order_qty,despatched_qty,allocated_qty,val)
Select
            A.order_no,
            A.order_line_no,
            A.line_type,
            A.order_line_status,
            A.order_qty,
            A.despatched_qty,
            A.allocated_qty,
            A.val
 
from [scheme].[opdetm] A ,opdetm_audit2 B  with (NOLOCK) where order_no = @OrderNo
and @DateEntered < CAST(FLOOR(CAST(GETDATE() AS FLOAT))AS DATETIME)
and A.order_no = B.order_no and
A.order_line_no = B.order_line_no
 
      End
 
END
0
 
LVL 2

Accepted Solution

by:
meteorelec earned 0 total points
ID: 21831327
I figured it out myself, i just do a left join on the opdetm_audit2 where order no is null
0

Featured Post

The Orion Papers

Are you interested in becoming an AWS Certified Solutions Architect?

Discover a new interactive way of training for the exam.

Question has a verified solution.

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

Preparing an email is something we should all take special care with – especially when the email is for somebody you may not know very well. The pressures of everyday working life stacked with a hectic office environment can make this a real challen…
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…

695 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