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
Anonymous User
21-Jan-2015QUOTENAME:
WHERE OBJECT_ID (QUOTENAME([name]) + N'.[dbo].[tblInfo]', N'U') IS NOT NULL
(Ditto for schema/table names that contain reserved characters)