August 6, 2015 at 9:00 am
When I run below query I get 3 columns, but when I try to add table name
ind.object_name (object_id) it's giving me an error "Ambiguous column name 'object_id"
How do I add tables name with 3 columns?
SELECT ind.name, ic.index_id , ind.type_desc from sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
August 6, 2015 at 9:02 am
Tac11 (8/6/2015)
When I run below query I get 3 columns, but when I try to add table nameind.object_name (object_id) it's giving me an error "Ambiguous column name 'object_id"
How do I add tables name with 3 columns?
SELECT ind.name, ic.index_id , ind.type_desc from sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
Add the alias to the object_id column reference
😎
SELECT OBJECT_NAME(ind.object_id) AS OBJNAME,ind.name, ic.index_id , ind.type_desc from sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
August 6, 2015 at 9:05 am
Because you have multiple instances of object_id and you didn't specify which one to use. Just add the alias and it should work.
August 6, 2015 at 9:07 am
Thank you!!!
August 6, 2015 at 9:57 am
Tac11 (8/6/2015)
When I run below query I get 3 columns, but when I try to add table nameind.object_name (object_id) it's giving me an error "Ambiguous column name 'object_id"
How do I add tables name with 3 columns?
SELECT ind.name, ic.index_id , ind.type_desc from sys.indexes ind
INNER JOIN
sys.index_columns ic ON ind.object_id = ic.object_id and ind.index_id = ic.index_id
INNER JOIN
sys.columns col ON ic.object_id = col.object_id and ic.column_id = col.column_id
INNER JOIN
sys.tables t ON ind.object_id = t.object_id
Your query:
SELECT
ind.name,
ic.index_id,
ind.type_desc
from
sys.indexes ind
INNER JOIN
sys.index_columns ic
ON ind.object_id = ic.object_id and
ind.index_id = ic.index_id
INNER JOIN
sys.columns col
ON ic.object_id = col.object_id and
ic.column_id = col.column_id
INNER JOIN
sys.tables t
ON ind.object_id = t.object_id;
Adding the table name to the query:
SELECT
t.name TableName, -- No need for the object_name function
ind.name,
ic.index_id,
ind.type_desc
from
sys.indexes ind
INNER JOIN
sys.index_columns ic
ON ind.object_id = ic.object_id and
ind.index_id = ic.index_id
INNER JOIN
sys.columns col
ON ic.object_id = col.object_id and
ic.column_id = col.column_id
INNER JOIN
sys.tables t
ON ind.object_id = t.object_id;
Viewing 5 posts - 1 through 4 (of 4 total)
You must be logged in to reply to this topic. Login to reply