Solved

How To Change Field (or column) order through script

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

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

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

Suggested Solutions

Slowly Changing Dimension Transformation component in data task flow is very useful for us to manage and control how data changes in SSIS.
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
Using examples as well as descriptions, and references to Books Online, show the different Recovery Models available in SQL Server and explain, as well as show how full, differential and transaction log backups are performed
Via a live example, show how to setup several different housekeeping processes for a SQL Server.

738 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