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


Update Stats with full scan on all tables in all databases


Update Stats with full scan on all tables in all databases

Author
Message
sunshine-587009
sunshine-587009
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 1273
I run the following code which worked sql2k but does not work in sql2k5 and get the following error. I tried with and without semicolon, chaning quotations. Any help appreciated!

--Update Statistics on all tables in all DBs
Set Nocount on
Declare db Cursor For
Select name from master.dbo.sysdatabases where dbid>=5 --Doesnt include system dbs

Declare @dbname varchar(60)
Declare @execmd nvarchar(150)

Open db
Fetch Next from db into @dbname
While @@Fetch_status=0
begin
if @dbname is null
Begin
Print 'null Value'
end
else
Begin
PRINT '###########################################################################'
PRINT 'Update Statistics in ' + @dbname
SELECT @execmd = 'USE ' + @dbname + ''Exec sp_MSForEachTable 'Update Statistics ''?'' with FULLSCAN'''
EXEC(@execmd)
PRINT ''
End
Fetch Next from db into @dbname
end
Close db
Deallocate db
GO

Error:
Msg 102, Level 15, State 1, Line 1
Incorrect syntax near '[Production].[ProductProductPhoto]'.
Msg 319, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'with'. If this statement is a common table expression or an xmlnamespaces clause, the previous statement must be terminated with a semicolon.
Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark after the character string ''.

¤ §unshine ¤
Perry Whittle
Perry Whittle
SSC-Insane
SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)SSC-Insane (20K reputation)

Group: General Forum Members
Points: 20004 Visits: 17243
sunshine (9/15/2008)

the previous statement must be terminated with a semicolon.


this message is fairly descriptive!! The USE statement and exec must be separated by a semi colon like so


SELECT @execmd = 'USE ' + @dbname + '';Exec sp_MSForEachTable 'Update Statistics ''?'' with FULLSCAN'''



-----------------------------------------------------------------------------------------------------------

"Ya can't make an omelette without breaking just a few eggs" ;-)
GilaMonster
GilaMonster
SSC Guru
SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)SSC Guru (87K reputation)

Group: General Forum Members
Points: 87499 Visits: 45272
Perry Whittle (9/16/2008)
sunshine (9/15/2008)

the previous statement must be terminated with a semicolon.


this message is fairly descriptive!! The USE statement and exec must be separated by a semi colon like so


That message is just saying that if the with is starting a CTE (which in this case it is not) the previous command needs to be terminated with a ;
It's a generic message that is given any time there's a syntax error and the keywork 'with' is anywhere nearby. In this case, it's misleading as the problem has nothing to do with the 'with'

In this case, the problem is with the single quotes. Change the select to the following and it works (in my 2008 test instance)

SELECT @execmd = 'USE ' + @dbname + '  Exec sp_MSForEachTable ''Update Statistics ? with FULLSCAN'''



Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability

We walk in the dark places no others will enter
We stand on the bridge and no one may pass


sunshine-587009
sunshine-587009
SSC Eights!
SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)SSC Eights! (815 reputation)

Group: General Forum Members
Points: 815 Visits: 1273
I had originally tried the semi-colon and it did not work. Gail, your fix helped. Thank you so much for your help as always!

¤ §unshine ¤
vijay_shanthi23400
vijay_shanthi23400
Grasshopper
Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)Grasshopper (13 reputation)

Group: General Forum Members
Points: 13 Visits: 316
I am having job running SSIS package,which is having 4 steps as below
1.Database Integrity -- success
2.Clean Up History --success
3.Update Statistics --- failed
4.Reorganize Index--success

-->update statistics got failed due to below error..

FailedSad-1073548784) Executing the query "UPDATE STATISTICS [dbo].[PDE_RFEQP_MVMNT_EVENT_DN] WITH FULLSCAN " failed with the following error: "A severe error occurred on the current command. The results, if any, should be discarded. A severe error occurred on the current command. The results, if any, should be discarded.". Possible failure reasons: Problems with the query, "ResultSet" property not set correctly, parameters not set correctly, or connection not established correctly.

Can any one help me pls...
Aaron Street
Aaron Street
Valued Member
Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)Valued Member (55 reputation)

Group: General Forum Members
Points: 55 Visits: 854
How could you use the following code using sample percent using variable to pass percentage?



MANU-J.
MANU-J.
SSCrazy
SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)SSCrazy (2.9K reputation)

Group: General Forum Members
Points: 2934 Visits: 8766
See what message you receive when you try to run it under management studio...

MJ
sharma.arpita1
sharma.arpita1
Forum Newbie
Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)Forum Newbie (7 reputation)

Group: General Forum Members
Points: 7 Visits: 125
sp_msforeachtable 'update statistics ? with all'

it will work fine
TheSQLGuru
TheSQLGuru
SSChampion
SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)SSChampion (12K reputation)

Group: General Forum Members
Points: 12483 Visits: 8553
whenever I use sp_msforeach... I always SET QUOTED_IDENTIFIER OFF first. Then I can use DOUBLE quotes for the outer (executed) strings and then single quotes for inner string creation. Works like a champ, and I never have to worry with is it 1, 2, 3, 4, etc single quotes I have to put together to get the desired effect!! Cool

Best,
Kevin G. Boles
SQL Server Consultant
SQL MVP 2007-2012
TheSQLGuru on googles mail service
saqlainkhan
saqlainkhan
SSC Journeyman
SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)SSC Journeyman (80 reputation)

Group: General Forum Members
Points: 80 Visits: 422
Set Nocount on
Declare db Cursor For
Select name from master.dbo.sysdatabases where dbid>=11 --Doesnt include system dbs

Declare @dbname varchar(60)
Declare @execmd nvarchar(150)

Open db
Fetch Next from db into @dbname
While @@Fetch_status=0
begin
if @dbname is null
Begin
Print 'null Value'
end
else
Begin
PRINT '###########################################################################'
PRINT 'Update Statistics in ' + @dbname
SELECT @execmd = 'USE ' + @dbname + ' Exec sp_msforeachtable ''Update Statistics ? with FULLSCAN'''
EXEC(@execmd)
PRINT ''
End
Fetch Next from db into @dbname
end
Close db
Deallocate db
GO




All,

i tried the above code and made a moification where the number of database being pulled for testing purpose is 2
When i run it, i get the following error message:

###########################################################################
Update Statistics in xxx91B
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'sp_msforeachtable'.

###########################################################################
Update Statistics in xxx91C
Msg 2812, Level 16, State 62, Line 1
Could not find stored procedure 'sp_msforeachtable'.



When i just run the following:
it runs just fine
use xxx91C
exec sp_MSforeachtable 'update statistics ? with fullscan'
go




Any help would be appreciated.(SQL 08R2 RTM)

Noli Timere
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