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


Update statistics for all tables in any DB


Update statistics for all tables in any DB

Author
Message
SanjayAttray
SanjayAttray
SSCarpal Tunnel
SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)SSCarpal Tunnel (4.4K reputation)

Group: General Forum Members
Points: 4419 Visits: 1619
Comments posted to this topic are about the item Update statistics for all tables in any DB

SQL DBA.
Jason Crider
Jason Crider
SSChasing Mays
SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)

Group: General Forum Members
Points: 605 Visits: 2232
you can modify the following line to handle more naming possibilities on tables
SET @Statement = 'UPDATE STATISTICS ' + '[' + @tablename + ']' + ' WITH FULLSCAN'

MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
David Williams-471773
David Williams-471773
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 44
How would I modify the script to pass the owner.tablename?
Jason Crider
Jason Crider
SSChasing Mays
SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)

Group: General Forum Members
Points: 605 Visits: 2232
If you do a SELECT * FROM information_schema.tables you'll see that there's a column there called TABLE_SCHEMA. That's where you'll find your dbo. I'm assuming you are wanting to handle other schemas that aren't dbo.

Just modify the script so that you can pass TABLE_SCHEMA + TABLE_NAME.

MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
David Williams-471773
David Williams-471773
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 44
Thanks for the prompt reply. I am new with the coding, this is what I have but I am hitting a road block.
USE pubs
GO
SET NOCOUNT ON
GO
DECLARE updatestats CURSOR FOR
SELECT table_schema, table_name FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
OPEN updatestats

DECLARE @schema NVARCHAR(128)
DECLARE @tablename NVARCHAR(128)
DECLARE @Statement NVARCHAR(300)

FETCH NEXT FROM updatestats INTO @tablename
WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'UPDATING STATISTICS ' + @tablename
SET @Statement = 'UPDATE STATISTICS ' + @tablename + ' WITH FULLSCAN'
EXEC sp_executesql @Statement
FETCH NEXT FROM updatestats INTO @tablename
END

CLOSE updatestats
DEALLOCATE updatestats
GO
SET NO

Msg 16924, Level 16, State 1, Line 10
Cursorfetch: The number of variables declared in the INTO list must match that of selected columns.
Jason Crider
Jason Crider
SSChasing Mays
SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)SSChasing Mays (605 reputation)

Group: General Forum Members
Points: 605 Visits: 2232
I normally do this sort of thing without a cursor while still using a loop but I don't have time to play with it right now.

Since you mentioned being new, It helps when you post code on here to put the IFCode Shortcuts from the left of the messagebox around your code to format it.

Doing it the same way you could do the following:

SET NOCOUNT ON
GO

DECLARE updatestats CURSOR FOR
SELECT table_schema, table_name
FROM information_schema.tables
where TABLE_TYPE = 'BASE TABLE'
OPEN updatestats

DECLARE @tableSchema NVARCHAR(128)
DECLARE @tableName NVARCHAR(128)
DECLARE @Statement NVARCHAR(300)

FETCH NEXT FROM updatestats INTO @tableSchema, @tableName

WHILE (@@FETCH_STATUS = 0)
BEGIN
PRINT N'UPDATING STATISTICS ' + '[' + @tableSchema + ']' + '.' + '[' + @tableName + ']'
SET @Statement = 'UPDATE STATISTICS ' + '[' + @tableSchema + ']' + '.' + '[' + @tableName + ']' + ' WITH FULLSCAN'
--PRINT @Statement
EXEC sp_executesql @Statement
FETCH NEXT FROM updatestats INTO @tableSchema, @tableName
END

CLOSE updatestats
DEALLOCATE updatestats
GO
SET NOCOUNT OFF
GO



Also, have a look at this page for some of the normal maintenance procedures including another way of doing this.

MCITP, Database Administrator
A hodgepodge of Information Technology and Life
LinkedIn Profile
My Twitter
David Williams-471773
David Williams-471773
Valued Member
Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)Valued Member (51 reputation)

Group: General Forum Members
Points: 51 Visits: 44
That makes sense..Thanks! I appreciate your help.
David
rja.carnegie
rja.carnegie
SSC-Enthusiastic
SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)SSC-Enthusiastic (125 reputation)

Group: General Forum Members
Points: 125 Visits: 169
Technically the QUOTENAME() function should be considered for quoting schema and object names, such as

EXEC ( '
CREATE TABLE [#stupid[reallystupid]]tablename] (i int NOT NULL)
SELECT * FROM [#stupid[reallystupid]]tablename]
SELECT * FROM tempdb.INFORMATION_SCHEMA.TABLES WHERE ( TABLE_NAME LIKE N''%stupid%'' )
' )



I also prefer cursor variables in scripts because they go away by themselves - if the script halts without explicitly deallocating, you don't have to fuss to get it to run again. Also table variables. But my example here, a temporary table declared inside dynamic SQL which runs as a separate batch, is a lazy way to get the same benefit - table goes away when execution halts for any reason.

It also isn't an example to do with statistics, just a demonstration that objects can be given stupid names. This can happen if you have perversely motivated co-workers. The guy who mounts our server hardware likes to demonstrate his multilingual and multidisciplinary skill (exotic minerals? mythological characters in light opera? minor bodies of the outer solar system? some of which categories overlap, but thank god we closed our office in India), and our major databases, hundreds per server, all begin with a digit 0-9. We wear out more [ ] keys...
Russel Bell
Russel Bell
SSC Rookie
SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)SSC Rookie (45 reputation)

Group: General Forum Members
Points: 45 Visits: 264
Thanks also for the hint about the IFCode Shortcuts on the left side of the editing pane. Had not noticed those but wondered how to people displayed the code nicely. I will make use of those in my future posts.
SQL-DBA-01
SQL-DBA-01
Hall of Fame
Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)Hall of Fame (3.7K reputation)

Group: General Forum Members
Points: 3699 Visits: 3295
Hi Jason,

For you have a similar one to update stats of all Dbs?

Thanks.
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