Insert record to Database Table based on condition using Stored Procedure in SQL Server

Last Reply one month ago By pandeyism

Posted one month ago

Hi Team ,

I have a table Sale_Details where the date is define for last audit date.

I want to insert data in 2nd table in below case only.

If Current date(-5) > Last audit date then data needs to insert in 2nd table on every day till Last audit date = current date.

Can you please help me with stored procedure to this.

 

Thanks ..Great day !!

Posted one month ago

Hi Waghmare,

Refer below sample query.

SQL

CREATE TABLE #Sale_Details(Id INT,lastauditdate DATETIME)
INSERT INTO #Sale_Details VALUES(1,GETDATE())
DECLARE @Date DATETIME
SET @Date = GETDATE()-5
CREATE TABLE #TemTable(Id INT, lastauditdate DATETIME)
IF(GETDATE()- 5 < (SELECT TOP 1 lastauditdate FROM #Sale_Details ORDER BY Id DESC))
BEGIN
	INSERT INTO #TemTable(Id, lastauditdate)
	SELECT TOP 1 Id,lastauditdate FROM #Sale_Details ORDER BY Id DESC
END
SELECT * FROM #TemTable
DROP TABLE #TemTable
DROP TABLE #Sale_Details