June 5, 2008 at 2:04 pm
I work with a database that we are trying to free up some memory. The database is 23 GB on a 25 GB Drive. All tables (around 5500) are all the same format 3 rows- TSTAMP, VALUE, STATUS. I want to delete all rows from prior to 2006 which I can do with this statement:
DELETE FROM rowname
WHERE (TSTAMP < '2007-01-01 00:00:00')
However I do not want to do this for every table it would take days. Is there a way to start at the first table run this query then goto the next run query....
Thanks for any help.
Out of memory.
June 5, 2008 at 2:13 pm
Do a search for this undocumented Microsoft stored procedure: sys.sp_MSforeachtable.
😎
June 5, 2008 at 2:24 pm
If you use this query you can get all tables in a database
select table_Name from information_schema.tables where table_Type = 'BASE TABLE'
Use a cursor / loop to loop thru the results and build the delete statement into a varchar variable (for ex @sql)
and use Exec(@Sql) inside the loop
Chandra
June 5, 2008 at 2:26 pm
this sounds like a more easier solution than what I proposed.
June 5, 2008 at 2:31 pm
Sounds like I need to read about cursor / loops. I am also researching sys.sp_MSforeachtable.
June 5, 2008 at 2:54 pm
So after looking around sp_MSforeachtable here is what I came up with:
exec sp_MSforeachtable
@command1 = 'DELETE FROM',
@whereand = '(TSTAMP < '2007-01-01 00:00:00')'
June 5, 2008 at 3:15 pm
One thing I will suggest (strongly), test it in a test DB first to ensure it does what you expect.
June 5, 2008 at 8:41 pm
Or you could avoid the use of Cursors and Loops altogether:
Declare @sql Varchar(max);
Select @sql = ''
DELETE FROM [' + TABLE_CATALOG + '].[' + TABLE_NAME + ']
WHERE TSTAMP < ''2007-01-01 00:00:00'';'
From INFORMATION_SCHEMA.TABLES
EXEC(@sql) --Do it!
[font="Times New Roman"]-- RBarryYoung[/font], [font="Times New Roman"] (302)375-0451[/font] blog: MovingSQL.com, Twitter: @RBarryYoung[font="Arial Black"]
Proactive Performance Solutions, Inc. [/font][font="Verdana"] "Performance is our middle name."[/font]
June 5, 2008 at 9:17 pm
Depending on the number of rows, you might also want to limit the batch size, meaning delete 1000 at a time.
Also, you're not freeing memory, you're freeing disk space.
June 6, 2008 at 9:06 am
Thanks for the help. I will be testing out different options on a backup server to see what works the best.
June 6, 2008 at 11:36 am
A word of caution either make sure you have frequent transaction log backups or enough log drive space.
😉
cheers.
* Noel
June 11, 2008 at 11:29 am
When executing query:
exec sp_MSforeachtable
@command1 = 'DELETE FROM',
@whereand = '(TSTAMP < '2007-01-01 00:00:00')'
I receive this error message:
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '2007'.
Any ideas on the correct syntax to make this work?
June 11, 2008 at 11:34 am
Michael (6/11/2008)
When executing query:exec sp_MSforeachtable
@command1 = 'DELETE FROM',
@whereand = '(TSTAMP < '2007-01-01 00:00:00')'
I receive this error message:
Server: Msg 170, Level 15, State 1, Line 3
Line 3: Incorrect syntax near '2007'.
Any ideas on the correct syntax to make this work?
exec sp_MSforeachtable
@command1 = 'DELETE FROM ? ',
@whereand = '(TSTAMP < '2007-01-01 00:00:00')'
* Noel
June 11, 2008 at 11:43 am
Also - since you're trying to put a single quote within the SQL statement, you need to double up on the single quotes, as in:
exec sp_MSforeachtable
@command1 = 'DELETE FROM ? ',
@whereand = '(TSTAMP < ''2007-01-01 00:00:00'')'
----------------------------------------------------------------------------------
Your lack of planning does not constitute an emergency on my part...unless you're my manager...or a director and above...or a really loud-spoken end-user..All right - what was my emergency again?
June 11, 2008 at 11:49 am
exec sp_MSforeachtable
@command1 = 'DELETE FROM ? ',
@whereand = '(TSTAMP < ''2007-01-01 00:00:00'')'
Results the same:
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'TSTAMP'.
Viewing 15 posts - 1 through 15 (of 22 total)
You must be logged in to reply to this topic. Login to reply