Solved

Query to drop all primary keys

Posted on 2004-08-11
12
1,593 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
ID: 11770614
can you post the exact error it gives?

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

Expert Comment

by:dishanf
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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 8

Accepted Solution

by:
dishanf 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:dishanf
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:dishanf
ID: 11770806
0

Featured Post

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Some sers suddenly getting error popup msg 28 88
script to send html report 3 32
I Need a T-SQL Query (Union or Intersect, or...?) 6 39
Query Syntax 17 34
When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
The Delta outage: 650 cancelled flights, more than 1200 delayed flights, thousands of frustrated customers, tens of millions of dollars in damages – plus untold reputational damage to one of the world’s most trusted airlines. All due to a catastroph…
Via a live example combined with referencing Books Online, show some of the information that can be extracted from the Catalog Views in SQL Server.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

776 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