?
Solved

SQL trigger- only insert select details

Posted on 2008-06-20
2
Medium Priority
?
691 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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

In Part II of this series, I will discuss how to identify all open instances of Excel and enumerate the workbooks, spreadsheets, and named ranges within each of those instances.
Code that checks the QuickBooks schema table for non-updateable fields and then disables those controls on a form so users don't try to update them.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
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…
Suggested Courses

752 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