Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2009-08-27 : 13:19:48
|
| Hi All,I have a very basic stored procedure(see below in bold). It delete all records from two tables where the date is today's date or greater. I want to change the stored procedure in a way that while executing the procedure if a user defines a particular date it will delete all the records from that date to all other greater dates. Otherwise If a user don't define a date it will delete all records from today's date and greater.Here is the cide for my stored procedure.CREATE PROCEDURE z_DeleteRecords ASBEGIN SET NOCOUNT ON;--Step 1--Remove records from table1DELETE FROM table1WHERE Date>=CONVERT(VARCHAR(10), GETDATE(), 120)--Step 2--Remove records from table2DELETE FROM table2 WHERE Date>=CONVERT(VARCHAR(10), GETDATE(), 120)ENDGOSo for example lets say today is 2009-08-27;so the following would delete all records, where the date is equal to or greater than '2008-01-01':Exec z_DeleteRecords ('2008-01-01')Whereas the following would delete all records from today's date to greater:Exec z_DeleteRecords ()How to make this change in my stored procedure.Thanks for your quick help.Zee |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-08-27 : 13:24:23
|
| [code]CREATE PROCEDURE z_DeleteRecords@DateVal datetime=NULL ASBEGINSET NOCOUNT ON;SET @DateVal=COALESCE(@DateVal,GETDATE())--Step 1--Remove records from table1DELETE FROM table1WHERE Date>=DATEADD(dd,DATEDIFF(dd,0,@DateVal),0)--Step 2--Remove records from table2DELETE FROM table2 WHERE Date>=DATEADD(dd,DATEDIFF(dd,0,@DateVal),0)ENDGO[/code] |
 |
|
|
cat_jesus
Aged Yak Warrior
547 Posts |
Posted - 2009-08-27 : 13:24:43
|
| [code]CREATE PROCEDURE z_DeleteRecords @FromDate as Datetime ASselect @fromDate = coalesce(@fromdate,getdate())BEGINSET NOCOUNT ON;--Step 1--Remove records from table1DELETE FROM table1WHERE Date>=CONVERT(VARCHAR(10), @fromDate, 120)--Step 2--Remove records from table2DELETE FROM table2 WHERE Date>=CONVERT(VARCHAR(10), @fromdate, 120)ENDGO[/code]There's one way.An infinite universe is the ultimate cartesian product. |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2009-08-27 : 13:27:54
|
| Thanks guys. I haven't tried it yet.But visakh16 is there a reason why you change my today's date function? |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-08-27 : 13:29:54
|
quote: Originally posted by zeeshan13 Thanks guys. I haven't tried it yet.But visakh16 is there a reason why you change my today's date function?
yup.there's no need of converting to varchar for dropping timepart |
 |
|
|
zeeshan13
Constraint Violating Yak Guru
347 Posts |
Posted - 2009-08-27 : 13:41:20
|
| But I want to delete the time part. So what to do here? |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2009-08-28 : 02:39:58
|
quote: Originally posted by zeeshan13 But I want to delete the time part. So what to do here?
His code will exclude the time partMadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|
|
|