Solved

Query to drop all primary keys

Posted on 2004-08-11
12
1,599 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 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
Free Webinar: AWS Backup & DR

Join our upcoming webinar with experts from AWS, CloudBerry Lab, and the Town of Edgartown IT to discuss best practices for simplifying online backup management and cutting costs.

 
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

Independent Software Vendors: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL query for highest sequence 4 63
Need help with a query 3 39
Find special characters using tSQL 6 19
sql server major issue  need help 2 32
Introduction In my previous article (http://www.experts-exchange.com/Microsoft/Development/MS-SQL-Server/SSIS/A_9150-Loading-XML-Using-SSIS.html) I showed you how the XML Source component can be used to load XML files into a SQL Server database, us…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Familiarize people with the process of utilizing SQL Server functions from within Microsoft Access. Microsoft Access is a very powerful client/server development tool. One of the SQL Server objects that you can interact with from within Microsoft Ac…
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

749 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