HOW TO DROPS SPECIAL CHARACTERS OBJECT_ID IN WHERE CLAUSE

Samuel Fernandes

Total Post:159

Points:1117
Posted by  Samuel Fernandes
 794  View(s)
Ratings:
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

  1. lois waisbrooker

    Post:70

    Points:494
    Re: How to drops special characters OBJECT_ID in WHERE clause

    QUOTENAME:

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

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

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!