Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Query to drop all primary keys

Posted on 2004-08-11
12
Medium Priority
?
1,627 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 8

Accepted Solution

by:
Dishan Fernando earned 2000 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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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.
In this article we will learn how to fix  “Cannot install SQL Server 2014 Service Pack 2: Unable to install windows installer msi file” error ?
This video shows, step by step, how to configure Oracle Heterogeneous Services via the Generic Gateway Agent in order to make a connection from an Oracle session and access a remote SQL Server database table.
Via a live example, show how to shrink a transaction log file down to a reasonable size.

636 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