Solved

syntax for sp_msforeachtable sql server 2005

Posted on 2008-10-31
9
1,031 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
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 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
 
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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Let's review the features of new SQL Server 2012 (Denali CTP3). It listed as below: PERCENT_RANK(): PERCENT_RANK() function will returns the percentage value of rank of the values among its group. PERCENT_RANK() function value always in be…
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Viewers will learn how to use the SELECT statement in SQL to return specific rows and columns, with various degrees of sorting and limits in place.

809 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