Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Track Updates in Quantity Field Expand / Collapse
Author
Message
Posted Monday, March 5, 2012 9:33 PM
Grasshopper

GrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopperGrasshopper

Group: General Forum Members
Last Login: Saturday, March 10, 2012 12:14 AM
Points: 13, Visits: 139
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
Post #1261938
Posted Monday, March 5, 2012 10:06 PM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Tuesday, July 22, 2014 9:03 PM
Points: 2,262, Visits: 5,405
You may want to read about Triggers which do exactly what you wanted!

Link : DML Triggers
Post #1261942
Posted Wednesday, March 7, 2012 7:27 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 7:25 PM
Points: 191, Visits: 895
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' ;
Post #1262981
Posted Wednesday, March 7, 2012 8:19 AM


SSC-Dedicated

SSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-DedicatedSSC-Dedicated

Group: Administrators
Last Login: Today @ 3:06 PM
Points: 33,169, Visits: 15,304
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.







Follow me on Twitter: @way0utwest

Forum Etiquette: How to post data/code on a forum to get the best help
Post #1263041
Posted Wednesday, March 7, 2012 8:47 AM
SSC-Enthusiastic

SSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-EnthusiasticSSC-Enthusiastic

Group: General Forum Members
Last Login: Today @ 7:25 PM
Points: 191, Visits: 895
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.
Post #1263061
Posted Wednesday, March 7, 2012 9:02 AM
SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: Monday, August 18, 2014 8:36 AM
Points: 2,836, Visits: 5,066
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!"
(So many miracle inventions provided by MS to us...)

How to post your question to get the best and quick help
Post #1263074
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse