When querying metadata for a specific table, I can either
- use the
OBJECT_ID
function or - join the relevant system tables (
sys.tables
andsys.schemas
).
For example, both of those queries will return all DML triggers for table myTable
:
SELECT *
FROM sys.triggers AS t
WHERE t.parent_id = OBJECT_ID('[dbo].[myTable]', 'U')
SELECT *
FROM sys.triggers AS tr
INNER JOIN sys.tables AS tab ON tr.parent_id = tab.object_id
INNER JOIN sys.schemas AS s ON tab.schema_id = s.schema_id
WHERE s.name = 'dbo' AND tab.name = 'myTable';
Is it purely a matter of taste, or is there a good reason to prefer one over the other (if both will solve your problem)?
My preliminary analysis is that:
- Option 1 is shorter (obviously).
- With Option 2 I don't need to worry about quoting/escaping the table/schema name (if it contains special characters).
- The query plans are different (but unlikely to make a huge difference, since this is not an operation I plan to do frequently).
Did I miss anything important?