Free Lessons
Courses
Seminars
TechHelp
Fast Tips
Templates
Topic Index
Forum
ABCD
 
Home   Courses   TechHelp   Forums   Help   Contact   Merch   Join   Order   Logon  
 
Back to SQL 3 Seminar Lessons    Comments List
Upload Images   @Reply   Bookmark    Link   Email   Next Unseen 
Cannot DROP COLUMN with SQL
Sami Shamma 
             
2 years ago
Hi Friends

Does anyone know why I cannot drop a Primary Key CLOUMN in a table?
The Column was created by SharePoint. I am moving the Backend to SQL Server and cleaning all the extra columns SharePoint placed in the table. It will not let me drop the Index either.

It does not generate and error but does not delete.
Note: I can manually Delete that column.

Thanks  

DetailsPrivate Sub Button2_Click()
    'Clean Court, Judge and User Table
    'Make a copy of the table
    'DoCmd.CopyObject , "JudgeT-OLD", acTable, "JudgeT"
    'DoCmd.CopyObject , "CourtT-OLD", acTable, "CourtT"
    'DoCmd.CopyObject , "UserT-OLD", acTable, "UserT"

    'DropField "CourtT"
    'DropField "judgeT"
    DropField "UserT1", "UserID", "UserIDSP"
    
End Sub

Private Sub DropField(TableName As String, PrimaryKey As String, OldID As String)
    
    Dim MySQL As String
    Dim MySQL2 As String
    
    MySQL = "ALTER TABLE " & TableName & " ALTER COLUMN UserIDSP INTEGER"


    MySQL2 = "ALTER TABLE " & TableName & _
            " DROP COLUMN " & OldID & "," & _
            " [Title], " & _
            " [Color Tag], " & _
            " [Compliance Asset Id], " & _
            " [Content Type], " & _
            " [App Created By], " & _
            " [App Modified By], " & _
            " [Workflow Instance ID], " & _
            " [File Type], " & _
            " [Modified], " & _
            " [Created], " & _
            " [Created By], " & _
            " [Modified By], " & _
            " [URL Path], " & _
            " [Path], " & _
            " [Item Type], " & _
            " [Encoded Absolute URL];"
    
    MsgBox MySQL
    'DoCmd.RunSQL MySQL

End Sub



In the code above, all the remaining columns are dropped.
Kevin Yip  @Reply  
     
2 years ago
To drop the primary index, you need to use:

     ALTER TABLE TableName DROP CONSTRAINT IndexName

In Access, the index name is found in table design -> indexes.
Sami Shamma OP  @Reply  
             
2 years ago
Hi Kevin

Yes, I managed to find that. My current problem is that I need to drop all Indexes before I can drop the columns. I am trying to automate this Data migration without human intervention. Different tables have different Indexes.

Is there a way in SQL to get all the indexes of a table and the loop and delete all of them?

Many Thanks
Kevin Yip  @Reply  
     
2 years ago
The index names are stored in the sys.indexes table, in the [name] field.  This SQL will list all the index items in [TableName]:

     SELECT * FROM sys.indexes WHERE object_id = OBJECT_ID('TableName');


To avoid having to dig into system tables to look up names you don't know, you may name the indices yourself when you create tables.  E.g.:

     CREATE TABLE TableName
     (
          PKFieldName int CONSTRAINT PKIndexName PRIMARY KEY IDENTITY (1,1),
          FieldName1 nvarchar(255)
     )

Sami Shamma OP  @Reply  
             
2 years ago
Thank you, Kevin.
I am naming the new index keys. It is cleaning up the mess created by SharePoint that the issue.

Appreciate your help.

This thread is now CLOSED. If you wish to comment, start a NEW discussion in SQL 3 Seminar Lessons.
 

Next Unseen

 
New Feature: Comment Live View
 
 

The following is a paid advertisement
Computer Learning Zone is not responsible for any content shown or offers made by these ads.
 

Learn
 
Access - index
Excel - index
Word - index
Windows - index
PowerPoint - index
Photoshop - index
Visual Basic - index
ASP - index
Seminars
More...
Customers
 
Login
My Account
My Courses
Lost Password
Memberships
Student Databases
Change Email
Info
 
Latest News
New Releases
User Forums
Topic Glossary
Tips & Tricks
Search The Site
Code Vault
Collapse Menus
Help
 
Customer Support
Web Site Tour
FAQs
TechHelp
Consulting Services
About
 
Background
Testimonials
Jobs
Affiliate Program
Richard Rost
Free Lessons
Mailing List
PCResale.NET
Order
 
Video Tutorials
Handbooks
Memberships
Learning Connection
Idiot's Guide to Excel
Volume Discounts
Payment Info
Shipping
Terms of Sale
Contact
 
Contact Info
Support Policy
Mailing Address
Phone Number
Fax Number
Course Survey
Email Richard
[email protected]
Blog RSS Feed    YouTube Channel

LinkedIn
Copyright 2026 by Computer Learning Zone, Amicron, and Richard Rost. All Rights Reserved. Current Time: 4/30/2026 12:24:25 PM. PLT: 0s