Solved

How To Change Field (or column) order through script

Posted on 2014-03-01
5
157 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
[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
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
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.
This videos aims to give the viewer a basic demonstration of how a user can query current session information by using the SYS_CONTEXT function
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.

688 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