I want to create a stored procedure for the documents whose NStatusFlag
should not be equal to 5
and 14
And also it should send Reminder to the respective user if the Document is with them for more than 3 days and if it is for more than 5 days then it should send it to its senior.
The date should be calculated with the U_datetime
of the table and with the SYSTEM
date from the system.
My NStatusFlag
suggest which all documents are opened.
I tried below:
ALTER PROCEDURE GET_INWARD_REMINDER_REPORT
AS
BEGIN
Select * from inward_doc_tracking_trl
where NStatus_flag <> 5
and NStatus_flag <> 14
END
GO
So I got all the documents which are opened, like below
But I am stuck how to get the all user by comparing with the date and send the reminders.
Also, I will get the Name of the USer with which the document is assigned from the inward_doc_tracking_hdr
table.
NOTE
inward_doc_tracking_hdr
table mkey
is related to ref_mkey
from the inward_doc_tracking_trl
table.
UPDATE
I get all the User from this query
ALTER PROCEDURE GET_INWARD_REMINDER_REPORT
AS
BEGIN
Select distinct a.mkey, b.mkey, a.first_name + ' ' + a.last_name from user_mst a
inner join inward_doc_tracking_hdr b
on a.mkey = b.User_Id
END
GO
UPDATE to get Senior name
select * from inward_doc_tracking_hdr order by mkey desc -- here I get To_User
select * from user_mst where mkey = 187 -- here I get To_User
select Reporting_To,* from emp_mst where mkey = 122 -- here I get Senior
1条答案
按热度按时间soat7uwm1#
This query gets the tracking lines of people with documents for 3 days or more (you might need to be careful with the time component). Please test and see if this gets what you want.
This query then works out if its 3 or 5 days overdue and populates a second column if it's 3 days overdue:
You need to run that every day, I suggest inside a scheduled SQL Agent Job
With regards to 'sending a reminder', then assuming this is email, I suggest you first set up SQL Mail: https://msdn.microsoft.com/en-au/library/hh245116.aspx
Then use
sp_send_dbmail
https://msdn.microsoft.com/en-au/library/ms190307.aspx to send the email messageYou'll also need to use the above query in a cursor and feed that into your
sp_send_dbmail
How to send multiple emails from sql server database using sp_send_dbmail procedureHave a crack at that and ask more questions if you like