retrieve primary key of table ????/
-
i want to retrieve of a table whose schema and other constraints i do not know basically , i had given user opurtunity to browse the database (and select any table) and based on that i will display the priary keys(if it does not had primey keys then a msgbox will be displayed) is there any method to know wheteher table has primary keys and to bind that column to the combobox plz help a code snippet will be more helpfull....
-
i want to retrieve of a table whose schema and other constraints i do not know basically , i had given user opurtunity to browse the database (and select any table) and based on that i will display the priary keys(if it does not had primey keys then a msgbox will be displayed) is there any method to know wheteher table has primary keys and to bind that column to the combobox plz help a code snippet will be more helpfull....
You can use the following SQL Statement to get the data out of the database that you want:
SELECT COLUMN_NAME FROM INFORMATION_SCHEMA.TABLE_CONSTRAINTS AS tc
INNER JOIN INFORMATION_SCHEMA.KEY_COLUMN_USAGE AS kcu
ON tc.CONSTRAINT_CATALOG = kcu.CONSTRAINT_CATALOG
AND tc.CONSTRAINT_SCHEMA = kcu.CONSTRAINT_SCHEMA
AND tc.CONSTRAINT_NAME = kcu.CONSTRAINT_NAME
WHERE tc.CONSTRAINT_TYPE = 'PRIMARY KEY'
AND tc.TABLE_NAME = @TableNameIf this SQL Statement returns rows then they each represent one of the columns of the primary key (maximum of 16). If it returns no rows then there is no primary key. Does this help?
My: Blog | Photos | Next SQL Presentation WDevs.com - Open Source Code Hosting, Blogs, FTP, Mail and More