ورود

View Full Version : پیدا کردن کلید اصلی Table ها



Accidentboy
سه شنبه 15 اردیبهشت 1388, 15:45 عصر
با سلام
من حدود 1000 تا Table دارم که می خوام بدونم که کدومشون کلید اصلی دارند
آبا میشه با یه Query اون رو پیدا کرد


با تشکر

hassanf
سه شنبه 15 اردیبهشت 1388, 18:00 عصر
سلام


Select Table_Name
From Information_Schema.table_Constraints
Where Constraint_Type = 'Primary Key'

Accidentboy
چهارشنبه 16 اردیبهشت 1388, 12:30 عصر
با سلام

این تیکه کد رو هم از اینترنت گرفتم


select name from sys.objects
where type_desc='user_table'
and name not in (
select distinct tbl.name
from sysobjects tbl join sysconstraints constr
on tbl.id = constr.id and tbl.xtype = 'U'
and constr.status & 0x0001 = 0x0001
join sysobjects constrId on
constrId.parent_obj = tbl.id and constrId.xtype = 'PK'
join sysindexes ix on constrId.name = ix.name and
ix.id = tbl.id join sysindexkeys sik on
sik.id = tbl.id and sik.indid = ix.indid
join syscolumns col on col.id = tbl.id and sik.colid = col.colid
)
order by name