SQL trigger- send email to user based on the dates on the Table

New in using SQL trigger
I have a table as shown in the Excel sheet
Table: Customers
Check the [Date Contract Ends], and run the rpt notifying the user that [Date Contract Ends]before 15 days
In the rpt show the Customer Name & [Date Contract Ends]
Using SQL Trigger
mercybthomas74Asked:
Who is Participating?
 
mdagisCommented:
CREATE PROCEDURE cLink

AS

IF DateAdd(day, 15, DateContractEnds) > getDate() Then
Begin
        exec sp_sendmail

    @SUBJECT               = 'Test Message'
,   @TO                    = 'nospam@nospams.com'
,   @FROM                  = 'nospam@nospams.com'
,   @MESSAGE               = 'Hello World'
,   @CONTENTTYPE           = 'text/plain'


End

GO
0
 
mercybthomas74Author Commented:
Attachement - Excel File
SQL-trigger.xls
0
 
Anthony PerkinsCommented:
Do not use a TRIGGER for this purpose.  TRIGGERs are not intended for that purpose.  Instead build a job that you can schedule to run as frequently as necessary.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
mdagisCommented:
CREATE TRIGGER TR_MyTable ON MyTable
FOR UPDATE
AS

IF DateAdd(day, 15, DateContractEnds) > getDate() Then
Begin
        exec sp_sendmail

    @SUBJECT               = 'Test Message'
,   @TO                    = 'nospam@nospams.com'
,   @FROM                  = 'nospam@nospams.com'
,   @MESSAGE               = 'Hello World'
,   @CONTENTTYPE           = 'text/plain'


End

End
0
 
TejasShahMscITCommented:
Hi,

SQL trigger is intended to do stuff when something get insert/update in database table.

In your case, you don't need to use trigger to achieve this thing.
You just need to import data to sql server and develop one Stored Procedure.
This Stored Procedure should contain logic to send email based on data inserted into table(imported from excel).

You can write trigger code (given by "mdagis") in Stored procedure.

If you insists to use trigger then use trigger given by "mdagis". just also run trigger for "INSERT"

CREATE TRIGGER TR_MyTable ON MyTable
FOR INSERT, UPDATE
..
..
.
END

Thanks,
Tejas
SQLYoga.com
0
 
mercybthomas74Author Commented:
Here is what i have so far below is the code

How can i show the record having the condition in the message of the email . I tried to add
@message = @SQL
but it prints SQL statement,  not the result
DECLARE @sql varchar(8000),@ExpDate datetime 

SELECT @ExpDate = [Date Contract Ends]
FROM [CMU Item Optimization Tracking List]

SELECT @sql = '[Customer Name], [Supplier Name], [Date Contract Ends]'

IF (getdate()+ 15)<= @ExpDate
Begin
        EXEC  master.dbo.xp_sendmail @recipients = 'xxxxx@xxx.com', @subject = '30 days remaining', @message = 'Testing '

END

SELECT @sql = @sql + ' FROM  [CMU Item Optimization Tracking List]'
EXEC (@sql) 

Open in new window

0
 
mercybthomas74Author Commented:
any suggestion to do this in SQL stored procedure
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.