यहाँ एक शुरुआत (SQL 2005 और ऊपर) है।
लेकिन यह केवल प्राथमिक कुंजी का नाम सूचीबद्ध करता है। क्या आपको कुंजी में कॉलम की सूची चाहिए?
- जोड़ा ----------------
यहाँ एक उपयोगिता मैं एक बार लिखा था का एक हिस्सा है। हैकिंग कि, निम्नलिखित केवल एक स्तंभ के प्राथमिक कुंजी के साथ सभी (और केवल) टेबल सूची जाएगा
SELECT ta.name TableName, ind.name IndexName, indcol.key_ordinal IndexColumnOrder, col.name ColumnName
from sys.tables ta
left outer join sys.indexes ind
on ind.object_id = ta.object_id
and ind.is_primary_key = 1
left outer join sys.index_columns indcol
on indcol.object_id = ta.object_id
and indcol.index_id = ind.index_id
left outer join sys.columns col
on col.object_id = ta.object_id
and col.column_id = indcol.column_id
order by
ta.Name
,indcol.key_ordinal
:
SELECT ta.name TableName, max(col.name) ColumnName
from sys.tables ta
inner join sys.indexes ind
on ind.object_id = ta.object_id
and ind.is_primary_key = 1
inner join sys.index_columns indcol
on indcol.object_id = ta.object_id
and indcol.index_id = ind.index_id
inner join sys.columns col
on col.object_id = ta.object_id
and col.column_id = indcol.column_id
group by ta.name
having count(*) = 1
order by ta.Name
यह सभी मौजूदा प्राथमिक कुंजी के क्रम में सभी तालिकाओं और कॉलम सूची जाएगा
इसे C# (मेरे फोर्टे नहीं) में बदलने के लिए, आपको या तो इसे संग्रहीत प्रक्रिया बनाने में सक्षम होना चाहिए, या केवल अपने कोड के भीतर से क्वेरी बनाना और सबमिट करना चाहिए।
select
t.Table_Name,
tc.Constraint_Name,
ccu.Column_Name
from
information_schema.tables t
left join information_schema.table_constraints tc
on tc.Table_Catalog = t.Table_Catalog
and tc.Table_Name = t.Table_Name
and tc.Constraint_Type = 'PRIMARY KEY'
left join information_schema.constraint_column_usage ccu
on ccu.Table_Catalog = tc.Table_Catalog
and ccu.Table_Name = tc.Table_Name
and ccu.Constraint_Schema = tc.Constraint_Schema
and ccu.Constraint_Name = tc.Constraint_Name
order by
t.Table_Name,
tc.Constraint_Name,
ccu.Column_Name
हो जाएगा ताकि सूची टेबल, उनके प्राथमिक कुंजी की कमी है, और उन बाधाओं में कॉलम:
स्रोत
2010-08-23 15:14:09
संभावित डुप्लिकेट [मैं TSQL का उपयोग कर डेटाबेस में सभी तालिकाओं की सूची कैसे प्राप्त करूं?] (Http://stackoverflow.com/questions/175415/how-do-i-get-list-of-all-tables -इन-ए-डेटाबेस-उपयोग-tsql) –
डुप्लिकेट नहीं। मुझे सी # कोड चाहिए, टीएसक्यूएल नहीं। एक बड़ा अंतर है। –
@ सर्जीओ: सी # में ऐसा करने का तरीका एक टीएसक्यूएल स्क्रिप्ट निष्पादित करना होगा (उदा। एसपी)। – Brian