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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
10 Tips to Protect Your Business from Ransomware

Did you know that ransomware is the most widespread, destructive malware in the world today? It accounts for 39% of all security breaches, with ransomware gangsters projected to make $11.5B in profits from online extortion by 2019.

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
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft SQL Server

From novice to tech pro — start learning today.