January 14, 2011 at 1:13 pm
Hello all,
I need a dynamic sql script to loop through all the tables in a database and delete records older than certain date.
Note: i dont want to use sp_msforeachtable stored procedure, i need the dynamic sql script
January 14, 2011 at 1:54 pm
Dj463 (1/14/2011)
Hello all,I need a dynamic sql script to loop through all the tables in a database and delete records older than certain date.
Note: i dont want to use sp_msforeachtable stored procedure, i need the dynamic sql script
Not a difficult request for you to perform in your own right. Here's the steps:
1) Examine your database and confirm your inserted/updated date field is the same name in all tables. If not, correct this.
2) Generate a cursor off sys.tables for only user databases for their name.
3) Build out the SQL string, using the table variable from the cursor above and including it into the DELETE FROM @table WHERE Mycolumn <= @DateToDeleteUntil.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
January 14, 2011 at 2:52 pm
Actually, it's not quite so easy to pull off in the presence of DRI. You'll need code to identify dependencies and that's not always the easiest thing to determine.
--Jeff Moden
Change is inevitable... Change for the better is not.
January 14, 2011 at 3:25 pm
Jeff Moden (1/14/2011)
Actually, it's not quite so easy to pull off in the presence of DRI. You'll need code to identify dependencies and that's not always the easiest thing to determine.
Sorry, DRI? I'm assuming you're talking about foreign key relationships and the like, where you'll have to deal with any children you'd need to delete if you haven't turned on cascades.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
January 14, 2011 at 8:00 pm
could you please provide me with some of examples
January 18, 2011 at 12:41 am
Dj463 (1/14/2011)
could you please provide me with some of examples
Show us your current work, and current code, and explain where you're having difficulty, and we'll see if we can't help you.
Never stop learning, even if it hurts. Ego bruises are practically mandatory as you learn unless you've never risked enough to make a mistake.
For better assistance in answering your questions[/url] | Forum Netiquette
For index/tuning help, follow these directions.[/url] |Tally Tables[/url]
Twitter: @AnyWayDBA
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply