Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL trigger- only insert select details

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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

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.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …

618 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