OFGemini
asked on
Delete Primary key constraint without knowing the name. SQL 2008
Using a single SQL 2008 query, I'm trying to delete a primary key constraint from a table. but the catch is I don't know the name of the primary key. In this case, each table has a random number attached to the end of the pk....like, pk_WorkFlowDocumentsTable_ 234123. So I just need a query that will delete the pk constraint, no matter what the name is.
I'm very close...
This query returns the name of the key i need to delete:
select a.CONSTRAINT_NAME from INFORMATION_SCHEMA.KEY_COL UMN_USAGE a inner join INFORMATION_SCHEMA.TABLE_C ONSTRAINTS b on a.CONSTRAINT_NAME = b.CONSTRAINT_NAME where a.table_name = 'test2_WorkFlowDocumentsTa ble' and constraint_type = 'Primary key'
This query will delete the pk constraint:
ALTER TABLE test2_WorkFlowDocumentsTab le DROP CONSTRAINT [name]
But I just can't figure out how to put the two together?
I'm very close...
This query returns the name of the key i need to delete:
select a.CONSTRAINT_NAME from INFORMATION_SCHEMA.KEY_COL
This query will delete the pk constraint:
ALTER TABLE test2_WorkFlowDocumentsTab
But I just can't figure out how to put the two together?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER