Solved

Query to drop all primary keys

Posted on 2004-08-11
12
1,588 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:dishanf
Comment Utility
can you post the exact error it gives?

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

Expert Comment

by:dishanf
Comment Utility
0
 
LVL 6

Author Comment

by:billy21
Comment Utility
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
 
LVL 8

Accepted Solution

by:
dishanf earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 6

Author Comment

by:billy21
Comment Utility
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:dishanf
Comment Utility
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
Comment Utility
Just trying it now dishanf.  Had to restore my db first ;-)
0
 
LVL 6

Author Comment

by:billy21
Comment Utility
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
Comment Utility
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:dishanf
Comment Utility
0

Featured Post

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
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.
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

772 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now