October 22, 2015 at 10:57 am
Why is the name of the column BABY_DOB not appear in the output ?
I am simply trying to get a listing of table_name , col_name for those tables that have a primary key ( The primary key can have more than one column )
But see the output below
IF object_id('DELIVERY_IN', 'U') IS NOT NULL DROP TABLE DELIVERY_IN
Print 'Creating table DELIVERY_IN...'
Create table DELIVERY_IN
(
[MOM_MEM_NBR] [varchar] (75)NOT NULL,
[BABY_MEM_NBR] [varchar] (75),
[MOM_DOB] [datetime]NOT NULL,
[BABY_DOB] [datetime]NOT NULL,
[EDD] [datetime],
[GEST_AGE] [float],
[FILE_ID] [float]NOT NULLDEFAULT 11000
PRIMARY KEY ([MOM_MEM_NBR],[BABY_DOB] ),
FOREIGN KEY ([FILE_ID]) REFERENCES FILE_ID_IN([FILE_ID])
) ON [PRIMARY]
GO
Select object_name(SI.object_id) as tab_name,
( Select c.name from sys.columns c where object_id = SI.object_id AND c.column_id = index_column_id ) as [index_col_name]
from
sys.indexes SI
inner join sys.index_columns SIC on ( SIC.object_id = SI.object_id and (SI.is_primary_key =1) )
where SI.is_primary_key =1
ORDER BY 1,2
OUTPUT
tab_nameindex_col_name
DELIVERY_INBABY_MEM_NBR
DELIVERY_INMOM_MEM_NBR
October 22, 2015 at 11:04 am
mw112009 (10/22/2015)
Why is the name of the column BABY_DOB not appear in the output ?I am simply trying to get a listing of table_name , col_name for those tables that have a primary key ( The primary key can have more than one column )
But see the output below
IF object_id('DELIVERY_IN', 'U') IS NOT NULL DROP TABLE DELIVERY_IN
Print 'Creating table DELIVERY_IN...'
Create table DELIVERY_IN
(
[MOM_MEM_NBR] [varchar] (75)NOT NULL,
[BABY_MEM_NBR] [varchar] (75),
[MOM_DOB] [datetime]NOT NULL,
[BABY_DOB] [datetime]NOT NULL,
[EDD] [datetime],
[GEST_AGE] [float],
[FILE_ID] [float]NOT NULLDEFAULT 11000
PRIMARY KEY ([MOM_MEM_NBR],[BABY_DOB] ),
FOREIGN KEY ([FILE_ID]) REFERENCES FILE_ID_IN([FILE_ID])
) ON [PRIMARY]
GO
Select object_name(SI.object_id) as tab_name,
( Select c.name from sys.columns c where object_id = SI.object_id AND c.column_id = index_column_id ) as [index_col_name]
from
sys.indexes SI
inner join sys.index_columns SIC on ( SIC.object_id = SI.object_id and (SI.is_primary_key =1) )
where SI.is_primary_key =1
ORDER BY 1,2
OUTPUT
tab_nameindex_col_name
DELIVERY_INBABY_MEM_NBR
DELIVERY_INMOM_MEM_NBR
Quick suggestion, alter your DDL and prefix the constraints with the constraint keyword.
😎
October 22, 2015 at 11:21 am
FOUND THE MISTAKE.. ALL Set ....
Select object_name(SI.object_id) as tab_name,
( Select c.name from sys.columns c where object_id = SI.object_id AND c.column_id = SIC.column_id ) as [index_col_name]
from
sys.indexes SI
inner join sys.index_columns SIC on ( SIC.object_id = SI.object_id and (SI.is_primary_key =1) )
where SI.is_primary_key =1
ORDER BY 1,2
October 22, 2015 at 11:26 am
October 22, 2015 at 11:33 am
mw112009 (10/22/2015)
FOUND THE MISTAKE.. ALL Set ....
Select object_name(SI.object_id) as tab_name,
( Select c.name from sys.columns c where object_id = SI.object_id AND c.column_id = SIC.column_id ) as [index_col_name]
from
sys.indexes SI
inner join sys.index_columns SIC on ( SIC.object_id = SI.object_id and (SI.is_primary_key =1) )
where SI.is_primary_key =1
ORDER BY 1,2
Going to say it again. You really should not ORDER BY using ordinal position. You should ORDER BY using column names. What happens if the columns you are sorting on change position? Also, using column names makes your queries easier to understand.
October 22, 2015 at 12:25 pm
Lynn Pettis (10/22/2015)
mw112009 (10/22/2015)
FOUND THE MISTAKE.. ALL Set ....
Select object_name(SI.object_id) as tab_name,
( Select c.name from sys.columns c where object_id = SI.object_id AND c.column_id = SIC.column_id ) as [index_col_name]
from
sys.indexes SI
inner join sys.index_columns SIC on ( SIC.object_id = SI.object_id and (SI.is_primary_key =1) )
where SI.is_primary_key =1
ORDER BY 1,2
Going to say it again. You really should not ORDER BY using ordinal position. You should ORDER BY using column names. What happens if the columns you are sorting on change position? Also, using column names makes your queries easier to understand.
Just to make it more clear, it can be column names or column alias. The query could be rewritten like this:
Select object_name(SI.object_id) as tab_name,
( Select c.name
from sys.columns c
where object_id = SI.object_id
AND c.column_id = SIC.column_id ) as index_col_name
from sys.indexes SI
inner join sys.index_columns SIC on SIC.object_id = SI.object_id and SI.is_primary_key =1
where SI.is_primary_key =1
ORDER BY tab_name, index_col_name
Although, my personal preference would be like this:
SELECT OBJECT_SCHEMA_NAME( SI.object_id) schema_name,
object_name(SI.object_id) as tab_name,
c.name as index_col_name
FROM sys.indexes SI
JOIN sys.index_columns SIC ON SIC.object_id = SI.object_id
AND SI.index_id = SIC.index_id --You're missing this join condition.
JOIN sys.columns c ON c.object_id = SI.object_id
AND c.column_id = SIC.column_id
WHERE SI.is_primary_key =1
AND SI.is_primary_key =1
ORDER BY tab_name, index_col_name
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply