Solved

How To Change Field (or column) order through script

Posted on 2014-03-01
5
143 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 82

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 82

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
For both online and offline retail, the cross-channel business is the most recent pattern in the B2C trade space.
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Viewers will learn how the fundamental information of how to create a table.

758 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now