Solved

Query to drop all primary keys

Posted on 2004-08-11
12
1,596 Views
Last Modified: 2009-12-16
I have the following TSQL...

Declare @SQL VarChar(1000)
Declare DropFKs Cursor
For
Select 'Alter Table [' + Table_Name + '] Drop Constraint [' + Constraint_Name + ']'
From Information_Schema.Table_Constraints
Where Constraint_Type = 'Primary Key'
Open DropFKs
Fetch Next From DropFKs
Into @SQL
WHILE @@FETCH_STATUS = 0
Begin
      Exec(@SQL)
      Fetch Next From DropFKs
      INTO @SQL
End
Close DropFKs
DeAllocate DropFKs
Go    

When I run this I get an error saying I can't alter the table... because the table doesn't exist.  How do I end up with non-existant tables in this list and how to I exclude them?
0
Comment
Question by:billy21
  • 6
  • 5
12 Comments
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 11770614
can you post the exact error it gives?

You cant drop all PKs, before droping FK.
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 11770628
0
 
LVL 6

Author Comment

by:billy21
ID: 11770633
The exact error is...

Cannot alter 'FinancialYearPeriods' because it is not a table.


And now I think about it, that is the name of a UDF.
0
Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

 
LVL 8

Accepted Solution

by:
Dishan Fernando earned 500 total points
ID: 11770642
SELECT 'Alter Table [' + A.TABLE_NAME + '] Drop Constraint [' + A.Constraint_Name + ']'
FROM INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE A JOIN
INFORMATION_SCHEMA.TABLE_CONSTRAINTS B
 ON A.CONSTRAINT_NAME = B.CONSTRAINT_NAME AND A.TABLE_NAME =
B.TABLE_NAME
WHERE B.CONSTRAINT_TYPE = 'PRIMARY KEY' AND
    OBJECTPROPERTY(OBJECT_ID
        (A.TABLE_SCHEMA +
         N'.' +
         A.TABLE_NAME), 'IsMSShipped') = 0
0
 
LVL 11

Expert Comment

by:ram2098
ID: 11770648
I think the error that you were getting might be due to the pervious errors that you encountered when preparing the script.

First try to close and deallocate the cursor and run your script.

But, as dishanf said you may not be able to drop the primary alone..but again this depends on your database schema (possible if you don't have any foriegn keys :)

0
 
LVL 6

Author Comment

by:billy21
ID: 11770661
I'm dropping foreign keys first.  I already have a script for that.  I'm running into trouble trying to drop indices because it's telling me there's a unique constraint on one of the tables.  Sounds like I have to do this in the following order...

Drop FKs
Drop Unique Constraints
Drop Indices
Drop PKs

Would that sound about right?
0
 
LVL 6

Author Comment

by:billy21
ID: 11770680
When I attempt to deallocate the cursor alone I get this...

Server: Msg 16916, Level 16, State 1, Line 1
A cursor with the name 'DropFKs' does not exist.
Server: Msg 16916, Level 16, State 1, Line 2
A cursor with the name 'DropFKs' does not exist.

Which tells me the cursor was already deallocated.  As I said it is interpreting the UDF as a table.  I have a number of Inline and table valued UDFs in the system.  These need to be ignored.
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 11770714
did you try out my sql.
because ur one select following line and it will cause error1

Alter Table [dtproperties] Drop Constraint [pk_dtproperties]
0
 
LVL 6

Author Comment

by:billy21
ID: 11770716
Just trying it now dishanf.  Had to restore my db first ;-)
0
 
LVL 6

Author Comment

by:billy21
ID: 11770726
dishanf,

When I run the cursor against your query I get this error...

Server: Msg 3728, Level 16, State 1, Line 1
'PK_AdoptionStepParentChildren' is not a constraint.
0
 
LVL 6

Author Comment

by:billy21
ID: 11770740
It's ok.  that was caused by duplicate rows.  I threw in a distinct and it appears to work =)
0
 
LVL 8

Expert Comment

by:Dishan Fernando
ID: 11770806
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
This article shows gives you an overview on SQL Server 2016 row level security. You will also get to know the usages of row-level-security and how it works
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question