Solved

How To Change Field (or column) order through script

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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Why is this different from all of the other step by step guides?  Because I make a living as a DBA and not as a writer and I lived through this experience. Defining the name: When I talk to people they say different names on this subject stuff l…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
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.
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties

820 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