Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
tradingpassion
Starting Member
24 Posts |
Posted - 2005-05-25 : 12:41:35
|
| Hi I am new to SQL and need help to figure out something...Basically what I am trying to do is creating a trigger so that whenever there is a new entry added in the table it copies the row and paste it paste it in the new table(new_table and from that table it will send an email to person abc@domain.comI got everything figured out including it's sending an email to abc@domain.com but the problem whenever there is a new customer added it sends an email with the list of all the new customers added previously from the(new_table) What I would like to do is send and email whenever there is a new customer added that particular day. For example if there is a new customer added (Customer A) on Monday April 5 at 10:00 am.I want to send an email to abc@domain.com that day only with Customer A AND if later in the day there is a new customer added(Customer B) on the same day around 11:00 am I would like to send an email again with Customer A and Customer B's information but if there is a new customer added on April 6th then I only want to send an email to xyz@domain.com with the list of customers added on April 6th and not April 5thI know you can probably do that using getdate() function or something but I am not sure how..and I beleive it has to be somewhere by the "IF" statement??OR I can use status field but I need help with implementing it but not sure how?ALTER TRIGGER Trigger1ON m_userFOR INSERTASINSERT Trigger_table(company_name, locn, user_id)SELECT company_name, locn, user_idFROM insertedif (select count(*) from sms..Trigger_Project where company_name='ABC') >0BEGINEXEC master..xp_sendmail @recipients = 'xyz@domain.com', @query = 'select * from dbs..Trigger_Table where company_name=''XYZ''', @subject = 'xyz',@message = 'New customer added today',@width = 350END |
|
|
JimL
SQL Slinging Yak Ranger
1537 Posts |
Posted - 2005-05-25 : 12:56:19
|
| If you want to use timeAdd a date/time field with a default of Getdate() Example name : InsfdateThenALTER TRIGGER Trigger1ON m_userFOR INSERTASINSERT Trigger_table(company_name, locn, user_id)SELECT company_name, locn, user_idFROM inserted Where Insfdate between dateadd(mi,-2,Getdate()) and Getdate() --- From last two minutesif (select count(*) from sms..Trigger_Project where company_name='ABC') >0BEGINEXEC master..xp_sendmail @recipients = 'xyz@domain.com', @query = 'select * from dbs..Trigger_Table where company_name=''XYZ''', @subject = 'xyz',@message = 'New customer added today',@width = 350ENDJimUsers <> Logic |
 |
|
|
TG
Master Smack Fu Yak Hacker
6065 Posts |
Posted - 2005-05-25 : 13:19:53
|
As Jim said, You can use a datetime column to compare to getdate():and datediff(hour, [DatetimeCol], getdate()) <= 24 --within last 24 hoursBut I don't think you want the comparison on the insert, you want it in the IF and the @query, right?Couple of suggestions...Use exists instead of the result of count(*) because it's more efficient.For the @query parameter, select an explicit column list.In fact, always use an explicit column list. The exceptions are whenusing EXISTS/NOT EXISTS.--define query to select all records for ABC added within last 24 hoursdeclare @query varchar(2000)set @query = 'select <colList> from dbs..Trigger_Table where company_name=''XYZ'' and datediff(hour, [DatetimeCol], getdate()) <= 24)'--if any records have been added for ABC within last 24 hoursif exists (select * from sms..Trigger_Project where company_name='ABC' and datediff(hour, [DatetimeCol], getdate()) <= 24)begin EXEC master..xp_sendmail @recipients = 'xyz@domain.com', @query = @query, @subject = 'xyz', @message = 'New customer added today', @width = 350end Be One with the OptimizerTG |
 |
|
|
|
|
|
|
|