Solved

How To Change Field (or column) order through script

Posted on 2014-03-01
5
148 Views
Last Modified: 2014-05-06
Hello Experts.
I have to go through a huge database and change the Primary Key field to always be the first field in the table (the database designers before me didn't know it was important to be that way)
I am wondering - is there any way I can Alter the table and change the field order without losing data?
What would such script be?
I know I can do it manually in Management Studio, but that is not what I am looking for.
0
Comment
Question by:vituxa
5 Comments
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39897767
I don't know that it is important to be that way.  Why?
0
 
LVL 1

Author Comment

by:vituxa
ID: 39897781
I have tested it and when selecting large amounts of data, and having the primary key being the first field the result comes back significantly faster that way.

So, do you know of a way I can change the order by using script?
0
 
LVL 83

Assisted Solution

by:Dave Baldwin
Dave Baldwin earned 250 total points
ID: 39897782
No, I'm afraid I don't.
0
 
LVL 34

Assisted Solution

by:Brian Crowe
Brian Crowe earned 125 total points
ID: 39897808
The order of the columns has no effect on the speed with which the are retrieved.  Please post the table schema and the query you claim is dependent on column order.  Please include any indexes defined on the table.
0
 
LVL 75

Accepted Solution

by:
Anthony Perkins earned 125 total points
ID: 39897962
I have tested it and when selecting large amounts of data, and having the primary key being the first field the result comes back significantly faster that way.
I am afraid, all things being equal that is a perception problem on your part.

So, do you know of a way I can change the order by using script?
You cannot do that without rebuilding the table.  This involves dropping any FOREIGN KEYS and then recreating them.  Incidentally, this is how the SSMS table designer does it.
0

Featured Post

Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
SQL LINE CONTINUATION ISSUE 12 32
SYbase 4 27
SQL - Update field defined as Text 6 16
Alternative of IN Clause in SQL Server 3 18
Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
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 set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.

786 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