Track Updates in Quantity Field

  • Hi,

    Thanks for your time.

    I only have basic info about Sql Server database handling. What I need to do is:

    I have a table containing stock quantity. What I need to do is whenever quantity changes in Stock field, database send an email to me.

    Please advice.

    Thanks!

    Zeeshan

  • You may want to read about Triggers which do exactly what you wanted!

    Link : DML Triggers

  • Also, if you want that trigger to send you email, you will need to turn on Database Mail (DBMail) in order to use it.

    To get you started, here is a simple script to send a message, once DBMail is turned on and set up.

    --simple message

    EXEC msdb.dbo.sp_send_dbmail

    @profile_name = 'SQL Server Agent Mail Profile', -- this should be the actual profile name you give it when set up

    @recipients = 'someaddres@somecompany.com',-- use ; between them if more than one

    @body = 'This is a test of the emergency broadcast system....',

    @subject = 'This is only a test' ;

  • Be careful with this. Lots of people start out with this requirement and once they 50 emails in an hour, they decide it was a bad idea.

    What you might want is to track the changes in another table, and then periodically send out an update to yourself. Or only send an update if there hasn't been another update sent in the last hour.

  • I agree totally. I would recommend adding an audit table, and keep track of changes there by using a trigger to insert the important information to the audit table (who, what, when).

    Then you can just do a select when you want to find the information.

  • Having a SQL trigger sending email when data is changed is not a good idea.

    However, you may still want to get email ASAP, faster than periodic job...

    You can follow the previous advise to create a trigger which audit the changes into another table. Then design a service which pools the table and sends emails...

    Beeing in SQL2008, instead of triggers you may be interesting to try in-built CDC (change tracking)

    _____________________________________________
    "The only true wisdom is in knowing you know nothing"
    "O skol'ko nam otkrytiy chudnyh prevnosit microsofta duh!":-D
    (So many miracle inventions provided by MS to us...)

    How to post your question to get the best and quick help[/url]

Viewing 6 posts - 1 through 5 (of 5 total)

You must be logged in to reply to this topic. Login to reply