SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


msforeachtable


msforeachtable

Author
Message
rpatil22
rpatil22
SSC Veteran
SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)SSC Veteran (205 reputation)

Group: General Forum Members
Points: 205 Visits: 626
How to avoid some tables while using msforeachtable?
Lowell
Lowell
SSC Guru
SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)SSC Guru (69K reputation)

Group: General Forum Members
Points: 69740 Visits: 40917
the best way is to switch to your own cursor, and do whatever you were going to do to the specific tables;
here's a generic example; note how you could exclude just specific tables, or do it the better way and select only the tables you REALLY need:

declare
@isql varchar(2000),
@tbname varchar(64)

declare c1 cursor for
--EXAMPLE1
--every table except specific ones
--select name from sys.tables where name not in ('accounting','orders','invoices','payroll')
--EXAMPLE 2
-- only the suite of tables startingwith exp
--select name from sys.tables like 'EXP%'
--EXAMPLE3
--only tables with the column name i was looking for
select object_name(object_id) from sys.columns where name ='COMPLETEDDT'
open c1
fetch next from c1 into @tbname
While @@fetch_status <> -1
begin
select @isql = 'UPDATE @tbname SET COMPLETEDDT = GETDATE() where COMPLETEDDT IS NULL'
select @isql = replace(@isql,'@tbname',@tbname)
print @isql
exec(@isql)

fetch next from c1 into @tbname
end
close c1
deallocate c1




Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Jack Corbett
  Jack Corbett
SSC-Forever
SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)SSC-Forever (43K reputation)

Group: General Forum Members
Points: 43397 Visits: 14925
You can actually provide a criteria to sp_MSForEachTable. Like this:

sys.sp_MSforeachtable
@command1 = 'Select * from ?', -- nvarchar(2000)
@whereand = 'and O.name not like ''%sys%'''



You need the alias as the sysobjects table is aliased as O. At least it is in 2000/2005/2008.



Jack Corbett

Applications Developer

Don't let the good be the enemy of the best. -- Paul Fleming
At best you can say that one job may be more secure than another, but total job security is an illusion. -- Rod at work

Check out these links on how to get faster and more accurate answers:
Forum Etiquette: How to post data/code on a forum to get the best help
Need an Answer? Actually, No ... You Need a Question
How to Post Performance Problems
Crosstabs and Pivots or How to turn rows into columns Part 1
Crosstabs and Pivots or How to turn rows into columns Part 2
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search