Home > DeveloperSection > Forums > How to drops special characters OBJECT_ID in WHERE clause
Samuel Fernandes
Samuel Fernandes

Total Post:154

Points:1082
Posted on    January-21-2015 10:17 PM

 MSSQL Server SQL Server  SQL 
Ratings:


 1 Reply(s)
 484  View(s)
Rate this:

I'm using the query below to identify databases for a drop-down list; it works fine, as long as the DatabaseName does not have any special characters (such as periods, commas, etc), but of course, we do want to include those. Now, when I run the Select statement by itself, it returns all of the tables correctly, but when I add in the WHERE clause, it loses the tables with punctuation in the name. I'm sure that I have a mistake in OBJECT_ID, but I just can't figure it out.

SELECT [name] AS DatabaseName FROM sys.databases

WHERE OBJECT_ID ([name] + N'.[dbo].[tblInfo]', N'U') IS NOT NULL

ORDER BY DatabaseName ASC



lois waisbrooker
lois waisbrooker

Total Post:70

Points:494
Supported
Posted on    January-21-2015 10:54 PM

QUOTENAME:

WHERE OBJECT_ID (QUOTENAME([name]) + N'.[dbo].[tblInfo]', N'U') IS NOT NULL

(Ditto for schema/table names that contain reserved characters)


Don't want to miss updates? Please click the below button!

Follow MindStick