Hi Team
I need some help, i have a query and want to write a sql server report that will do a check . If "Stock on Hand = 0" Then it must send a reminder to the recipient mail. That "Critical, Cyclical, Standard and Slow Movement".
SQL
SELECT InvWarehouse.StockCode, InvMaster.Description, InvWarehouse.Warehouse, InvWarehouse.QtyOnHand, InvWarehouse.QtyAllocated, InvWarehouse.QtyOnOrder, InvWarehouse.QtyInTransit, InvWarehouse.QtyOnBackOrder
FROM sysproR.dbo.InvMaster InvMaster, sysproR.dbo.InvWarehouse InvWarehouse
WHERE InvMaster.StockCode = InvWarehouse.StockCode
Guest UserRank #877 · 2.5K reputationPosted Jul 30, 2024, 3:43 AM EDT
Hi Jithu
Thanks for your responce, let me work on this will give an update. This should be enough for me to test and write.
Jithu ThomasRank #437 · 5.7K reputationPosted Jul 30, 2024, 3:30 AM EDT
Configure Database Mail - reference: - https://learn.microsoft.com/en-us/sql/relational-databases/database-mail/configure-database-mail?view=sql-server-ver16
Modify your query to check for
QtyOnHand = 0and format the results for the email.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 = 'YourMailProfile', -- Replace with your Database Mail profile name @recipients = '[email protected]', -- Replace with the recipient email address @subject = 'Stock Alert: Items with zero stock on hand', @body = @body, @body_format = 'HTML'; ENDCreate a SQL Server Agent Job:
Jithu ThomasRank #437 · 5.7K reputationPosted Jul 30, 2024, 6:45 AM EDT
Once its work please accept my answer :) happy coding....