79167206

Date: 2024-11-07 15:55:00
Score: 1.5
Natty:
Report link

The reply by @MartinSmith was on the right track. The schema (and optionally database) should have been passed to the object_id function.

object_id(TABLE_SCHEMA+'.'+TABLE_NAME)

grabs the correct results.

The original, which only uses the TABLE_NAME string, always returns the table in the user's default schema - which in this case did not include a table by that name, while the schema being processed did.

To get info on a table in another database, the catalog can be added as well:

object_id(TABLE_CATALOG+','+TABLE_SCHEMA+'.'+TABLE_NAME)

Reasons:
  • Long answer (-0.5):
  • Has code block (-0.5):
  • User mentioned (1): @MartinSmith
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Paul H