How to set a reminder in store procedure using sql server?
0
votes
1
answer
46
views
I have a query, but want to create a store procedure that will set a reminder if 'Stock on Hand is 0' 'Blank' The following columns(Crystal, Cylical) needs to set a reminder to a recipient via email to action on them once if its blank or 0.
// sql query from the database
DECLARE @body NVARCHAR(MAX);
-- Prepare the email body with the list of items having Stock on Hand = 0
SET @body = N'
Stock Alert: Items with zero stock on hand
StockCode Description Warehouse QtyOnHand QtyAllocated QtyOnOrder QtyInTransit QtyOnBackOrder '; -- Add the rows to the email body SELECT @body = @body + N' ' + InvWarehouse.StockCode + N' ' + InvMaster.Description + N' ' + InvWarehouse.Warehouse + N' ' + CAST(InvWarehouse.QtyOnHand AS NVARCHAR) + N' ' + CAST(InvWarehouse.QtyAllocated AS NVARCHAR) + N' ' + CAST(InvWarehouse.QtyOnOrder AS NVARCHAR) + N' ' + CAST(InvWarehouse.QtyInTransit AS NVARCHAR) + N' ' + CAST(InvWarehouse.QtyOnBackOrder AS NVARCHAR) + N' ' FROM sysproR.dbo.InvMaster InvMaster JOIN sysproR.dbo.InvWarehouse InvWarehouse ON InvMaster.StockCode = InvWarehouse.StockCode WHERE InvWarehouse.QtyOnHand = 0; -- Close the HTML tags SET @body = @body + N''; -- Check if there are items with zero stock on hand IF EXISTS ( SELECT 1 FROM sysproR.dbo.InvWarehouse WHERE QtyOnHand = 0 ) BEGIN -- Send the email EXEC msdb.dbo.sp_send_dbmail @profile_name = 'Test', -- Replace with your Database Mail profile name @recipients = 'training@metrocon.com', @subject = 'Stock Alert: Items with zero stock on hand', @body = @body, @body_format = 'HTML'; END
Asked by Gcobza
(95 rep)
Jul 30, 2024, 04:47 PM
Last activity: Jul 30, 2024, 05:06 PM
Last activity: Jul 30, 2024, 05:06 PM