Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
| Author |
Topic |
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-02-20 : 02:59:30
|
| Hi All,Can any of you help me in getting info about, weather the col is primay key and weather the col is identity.I have to insert these info into my own data dict tableusing information schema i got most of the info except primay keyand identity property of the col.insert into mydictvaluesselect column_name, table_name, data_type, --primay ( 1 if primary else 0 if not primary) --idenity( 1 if identiy else 0 if not identity) ordinal_positionfrom information_schema.columnsThanks,aak |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-20 : 03:19:32
|
| [code]select column_name,table_name,data_type,case when i.name is not null then 1 else 0 end as is_identity,case when k.name is not null then 1 else 0 end as is_pk from information_schema.columns cleft join sys.identity_columns ion i.name=c.column_nameleft join sys.key_constraints kon k.name=c.column_nameand k.type='pk'[/code] |
 |
|
|
aakcse
Aged Yak Warrior
570 Posts |
Posted - 2009-02-20 : 05:49:11
|
Thank you so much Visakh, I was looking for this code only.quote: Originally posted by visakh16
select column_name,table_name,data_type,case when i.name is not null then 1 else 0 end as is_identity,case when k.name is not null then 1 else 0 end as is_pk from information_schema.columns cleft join sys.identity_columns ion i.name=c.column_nameleft join sys.key_constraints kon k.name=c.column_nameand k.type='pk'
|
 |
|
|
raky
Aged Yak Warrior
767 Posts |
Posted - 2009-02-20 : 07:03:18
|
| when i tested visakh query it is not giving correct results for primary key columnsTry this alsoselect distinct c.column_name,c.table_name,data_type,case when i.name is not null then 1 else 0 end as is_identity,case when k.column_name is not null then 1 else 0 end as is_pk from information_schema.columns cleft join sys.identity_columns ion i.name=c.column_nameleft join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE kon k.column_name=c.column_nameand k.constraint_name like '%pk_%' |
 |
|
|
sakets_2000
Master Smack Fu Yak Hacker
1472 Posts |
Posted - 2009-02-20 : 07:51:41
|
quote: Originally posted by raky when i tested visakh query it is not giving correct results for primary key columnsTry this alsoselect distinct c.column_name,c.table_name,data_type,case when i.name is not null then 1 else 0 end as is_identity,case when k.column_name is not null then 1 else 0 end as is_pk from information_schema.columns cleft join sys.identity_columns ion i.name=c.column_nameleft join INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE kon k.column_name=c.column_nameand k.constraint_name like '%pk_%'
This is not right. You can have a constraint named like '%pk_%' and it might not be the primary key.For PK, You could use this,SELECT i.name AS IndexName,OBJECT_NAME(ic.OBJECT_ID) AS TableName,COL_NAME(ic.OBJECT_ID,ic.column_id) AS ColumnNameFROM sys.indexes AS iINNER JOIN sys.index_columns AS icON i.OBJECT_ID = ic.OBJECT_IDAND i.index_id = ic.index_idWHERE i.is_primary_key = 1 |
 |
|
|
visakh16
Very Important crosS Applying yaK Herder
52326 Posts |
Posted - 2009-02-20 : 09:32:24
|
| [code]select column_name,c.table_name,c.data_type,case when i.name is not null then 1 else 0 end as is_identity,case when ccu.column_name is not null then 1 else 0 end as is_pk from information_schema.columns cleft join sys.identity_columns ion i.name=c.column_nameleft join information_schema.table_constraints tcon tc.table_name=c.table_nameand tc.constraint_type='PRIMARY KEY'left join information_schema.constraint_column_usage ccuon ccu.column_name=c.column_name[/code] |
 |
|
|
|
|
|
|
|