Skip to content

Commit 1a2f8db

Browse files
authored
Merge pull request #3754 from VladDBA/3753
quick fix for #3753
2 parents d70e8f3 + 050eaa8 commit 1a2f8db

File tree

1 file changed

+19
-4
lines changed

1 file changed

+19
-4
lines changed

sp_BlitzIndex.sql

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -141,10 +141,25 @@ SET @DatabaseName = COALESCE(@DatabaseName, PARSENAME(@ObjectName, 3)) /* 3 = Da
141141
SET @SchemaName = COALESCE(@SchemaName, PARSENAME(@ObjectName, 2)) /* 2 = Schema name */
142142
SET @TableName = COALESCE(@TableName, PARSENAME(@ObjectName, 1)) /* 1 = Table name */
143143

144-
/* Handle already quoted input if it wasn't fully qualified*/
145-
SET @DatabaseName = PARSENAME(@DatabaseName,1);
146-
SET @SchemaName = ISNULL(PARSENAME(@SchemaName,1),PARSENAME(@TableName,2));
147-
SET @TableName = PARSENAME(@TableName,1);
144+
/* Handle already quoted input if it wasn't fully qualified - only if @ObjectName is null*/
145+
IF (@ObjectName IS NULL)
146+
BEGIN
147+
SELECT @DatabaseName = CASE WHEN @DatabaseName LIKE N'\[%\]' ESCAPE N'\' THEN PARSENAME(@DatabaseName,1) ELSE @DatabaseName
148+
END,
149+
@SchemaName = ISNULL(
150+
CASE /*only apply parsename if the schema is actually quoted*/
151+
WHEN @SchemaName LIKE N'\[%\]' ESCAPE N'\' THEN PARSENAME(@SchemaName,1) ELSE @SchemaName
152+
END,
153+
CASE /*if we already have @TableName in the form of [some.schema].[some.table]*/
154+
WHEN @TableName LIKE N'\[%\].\[%\]' ESCAPE N'\' THEN PARSENAME(@TableName,2)
155+
/*I'm making an assumption here that people who use . in their naming conventions would have one in each object name*/
156+
WHEN LEN(@TableName)- LEN(REPLACE(@TableName,'.','')) = 1 THEN PARSENAME(@TableName,2) ELSE NULL
157+
END),
158+
@TableName = CASE
159+
WHEN @TableName LIKE N'\[%\].\[%\]' ESCAPE N'\' OR @TableName LIKE N'\[%\]' ESCAPE N'\' THEN PARSENAME(@TableName,1)
160+
WHEN LEN(@TableName)- LEN(REPLACE(@TableName,'.','')) = 1 THEN PARSENAME(@TableName,1) ELSE @TableName
161+
END;
162+
END;
148163

149164
/* If we're on Azure SQL DB let's cut people some slack */
150165
IF (@TableName IS NOT NULL AND @AzureSQLDB = 1 AND @DatabaseName IS NULL)

0 commit comments

Comments
 (0)