April 12, 2006 at 8:56 am
Can someone please help me with a Stored Procedure problem.
One of our stored procedures is failing because one of the tables in database has quotes in its name, ie.'FAC treaties$'
I've pulled out the bit of code that is failing...
EXEC ('DBCC SHOWCONTIG (''[' + @TableSchema +'].['+ @TableName + ']'') WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
I tried the following but can't get this to work.
set quoted_identifier on
EXEC ('DBCC SHOWCONTIG ("'FAC treaties$'") WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
EXEC ('DBCC SHOWCONTIG (['FAC treaties$']) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
set quoted_identifier off
EXEC ('DBCC SHOWCONTIG ("'FAC treaties$'") WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
EXEC ('DBCC SHOWCONTIG (['FAC treaties$']) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
The error is :
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near 'FAC'.
The DBCC command on its own without being inside EXEC works fine either way :
DBCC SHOWCONTIG ("'FAC treaties$'") WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS
DBCC SHOWCONTIG (['FAC treaties$']) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS
Any ideas ? Please
April 12, 2006 at 9:24 am
Try using dynamic sql:
DECLARE @sql VarChar(100) DECLARE @tblName VarChar(15) SET @tblName = '["FAC Treaties$"]' SET @sql = 'DBCC SHOWCONTIG (' + @tblName + ') WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS' EXEC(@sql)
why does the db have tables with single quotes in their names ?!?!
Could you please share the reasoning behind this naming convention ?!
**ASCII stupid question, get a stupid ANSI !!!**
April 12, 2006 at 10:03 am
DECLARE @sql VarChar(100)
DECLARE @tblName VarChar(15)
SET @tblName = "'FAC Treaties$'"
SET @sql = 'DBCC SHOWCONTIG (' + @tblName + ') WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS'
EXEC(@sql)
Could not find a table or object named 'FAC Treaties$'. Check sysobjects.
DECLARE @sql VarChar(100)
DECLARE @tblName VarChar(15)
SET @tblName = "'FAC Treaties$'"
SET @sql = 'DBCC SHOWCONTIG (' + @tblName + ') WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS'
EXEC(@sql)
Invalid column name ''FAC Treaties$''.
April 12, 2006 at 10:13 am
How about using the tblName variable the way it is in my example and then running it - what does that get you ?!?!
SET @tblName = '["FAC Treaties$"]'
**ASCII stupid question, get a stupid ANSI !!!**
April 13, 2006 at 4:11 am
I will bet 25c on:
EXEC ('DBCC SHOWCONTIG ([''FAC treaties$'']) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS')
(those are two single quotes, not double quotes)
April 13, 2006 at 7:47 am
yeah I did that (forgot to copy that in my earlier reply). I got a whole bunch of other errors :
Server: Msg 105, Level 15, State 1, Line 1
Unclosed quotation mark before the character string '"FAC Treaties$) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS'.
Server: Msg 170, Level 15, State 1, Line 1
Line 1: Incorrect syntax near '"FAC Treaties$) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS'.
I also tried SET @tblName = ['FAC Treaties$']
and SET @tblName = ["FAC Treaties$"]
These gave the same errors as SET @tblName = "'FAC Treaties$'"
April 13, 2006 at 8:00 am
Try this :-
DECLARE
@sSql AS NVARCHAR(4000)
DECLARE
@StupidTableName AS NVARCHAR(1024)
SELECT
@StupidTableName = '''StupidTableName$'''
SELECT
@sSql = 'DBCC SHOWCONTIG (' + char(91) + @StupidTableName + char(93) + ')'
EXEC
(@sSql)
April 13, 2006 at 8:04 am
Do I win ?
April 13, 2006 at 2:17 pm
Only if the solution uses sp_MSforeachtable ... LOL !
RegardsRudy KomacsarSenior Database Administrator"Ave Caesar! - Morituri te salutamus."
April 19, 2006 at 10:03 am
Yes I got it to work for that stupid named table. The problem now is this is selecting tables within a cursor and not all of them have stupid names.
PS I used this in the end...
select @sSQL = 'DBCC SHOWCONTIG (['+ char(39) + @mytab + char(39) + ']) WITH ALL_INDEXES, TABLERESULTS, NO_INFOMSGS'
Thanks for all your help
April 19, 2006 at 10:07 am
Thats why I used char(91) and char(93) it wouldn't matter what the table name was then.
April 19, 2006 at 10:17 am
I did try the [ ] but still got errors..I'll see if I can work it out
Thanks
Viewing 12 posts - 1 through 11 (of 11 total)
You must be logged in to reply to this topic. Login to reply