Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

write a sql stored procedure to delete the data from the table?

I have a simple application which stores the data into the sql server database table named student table .The design of the table is as follows 3 colums. name ,sex and registeredtime(datetime).In my WPF window i have three fields to insert the data into the table.Now i want to have the delete button based on the input given by the user(which is a datetimepicker).

How to delete the data from the table which is 7 days old compared to the date given by the user. ?

I want to have a stored procedure which i can call from the c# code.i am able to try some thing like this but Select * from studenttable where registereddate < GetDate()-7 but i am unable to achieve what i am supposed to ...

like image 975
Macnique Avatar asked Oct 31 '25 14:10

Macnique


1 Answers

You will probably need to use the DATEADD function:

DELETE StudentTable
WHERE DATEADD(day,-7,GetDate()) > registeredDate

Make sure you do the equivalent select first to make sure you are deleting what you want:

SELECT * FROM StudentTable
WHERE DATEADD(day,-7,GetDate()) > registeredDate

Your stored proc would look something like this:

CREATE PROCEDURE DeleteRecent AS
BEGIN
    DELETE StudentTable
    WHERE DATEADD(day,-7,GetDate()) > registeredDate
END
GO
like image 170
Abe Miessler Avatar answered Nov 03 '25 04:11

Abe Miessler