We help IT Professionals succeed at work.
Get Started

SQL Syntax: Table trigger

203 Views
Last Modified: 2015-03-04
I have some SQL that works partially, I am looking for a change in a field (OrderStatus) and sending an email when the status changes from "S" to anything else AND the OrderType ='O'. My problem is the program front end sets the 'OrderStatus' to zero when an order is maintained so the trigger fires. I only want the trigger to fire on a genuine status change. Is this possible?

    select @nvchOldStatus = OrderStatus from deleted
    select @nvchNewStatus = OrderStatus from inserted
	select @nvchDocType = DocumentType from inserted
    -- If there has been a change in the status field

    if @nvchNewStatus <> @nvchOldStatus and @nvchDocType ='O' and @nvchNewStatus <> '0'
	select @nvchOrdNum = SalesOrder from inserted
	select @nvchCust = Customer from inserted
	select @nvchUser = LastOperator from inserted
	SET @nvchText = 'Order Number: ' + @nvchOrdNum + ' For Customer: ' + @nvchCust + ' Released from Suspense by: ' + @nvchUser

    begin
        if @nvchOldStatus ='S'
        begin
            -- send your email

Open in new window

Comment
Watch Question
Director, Practice Manager and Computing Consultant
Awarded 2014
Top Expert 2014
Commented:
This problem has been solved!
Unlock 6 Answers and 15 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE