From: Muhammad Bilal on
Hi.

I want to remove the primary key on table on any column if exists otherwise
do nothing.


Regards,
Muhammad Bilal

From: Erland Sommarskog on
Muhammad Bilal (MuhammadBilal(a)discussions.microsoft.com) writes:
> I want to remove the primary key on table on any column if exists
> otherwise do nothing.

SELECT 'ALTER TABLE ' + quotename(p.name) +
' DROP CONSTRAINT ' + quotename(o.name)
FROM sysobjects o
JOIN sysobjects p ON o.parent_obj = p.id
WHERE o.xtype = 'PK'

Copy result and execute it.

Note that if there are referencing foreign keys, it will not be possible
to drop the PK.

--
Erland Sommarskog, SQL Server MVP, esquel(a)sommarskog.se

Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx