Solved

syntax for sp_msforeachtable sql server 2005

Posted on 2008-10-31
9
1,030 Views
Last Modified: 2012-05-05
Can anyone give me a syntax for sp_msforeachtable sql server 2005

I want to use it in "alter index ... rebuild"

I could not find this proceedure in book on line
I'll appreciate the help

Thanks
0
Comment
Question by:Greens8301
  • 4
  • 3
  • 2
9 Comments
 
LVL 60

Expert Comment

by:chapmandew
ID: 22851029
exec sp_msforeachtable 'alter index all on [?] rebuild'
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22851087
actually, the ? will have the [schema].[table] so it's not necessary

exec sp_msforeachtable 'alter index all on ? rebuild'
0
 

Author Comment

by:Greens8301
ID: 22851094
what ? => substitued for

Thanks
0
 
LVL 60

Accepted Solution

by:
chapmandew earned 300 total points
ID: 22851130
? is the name of the table (and schema in my case).  you can omit the []...it just bascially is a swap out of the table name when the statemetn is built
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22851131
in the sp_msforeach(table/db) the ? represents the object

So for sp_msforeachtable it's [schemaName].[table_name]

for sp_msforeachdb it's [databasename]

0
 
LVL 60

Expert Comment

by:chapmandew
ID: 22851164
or the ? could be anything if you write your own...

http://blogs.techrepublic.com.com/datacenter/?p=401
0
 

Author Comment

by:Greens8301
ID: 22851453

exec sp_msforeachtable 'alter index all on ? rebuild'

just like this should work, right.
"?" is  a place holder , when the procedure runs iteratively, it substitutes different  table name iteratively

Thanks
0
 
LVL 39

Expert Comment

by:BrandonGalderisi
ID: 22851516
correct.
0
 
LVL 39

Assisted Solution

by:BrandonGalderisi
BrandonGalderisi earned 200 total points
ID: 22851529
You may want to use this:

exec sp_msforeachtable 'print ''Rebuilding indexes for ?''; alter index all on ? rebuild'

It will provide output as to what it is doing.


0

Featured Post

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Everyone has problem when going to load data into Data warehouse (EDW). They all need to confirm that data quality is good but they don't no how to proceed. Microsoft has provided new task within SSIS 2008 called "Data Profiler Task". It solve th…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now