all groups > sql server data warehouse > november 2005 >
You're in the

sql server data warehouse

group:

Rearrange fields alphabetically



Rearrange fields alphabetically zknezic
11/1/2005 9:16:01 AM
sql server data warehouse: Hi,
I am trying to alter a large table (with 100+ fields) so the fields are
ordered alphabetically. Is there a quicker and smarter way of doing this,
other then manual one-by-one?
Thanks,

Re: Rearrange fields alphabetically arbert
11/5/2005 12:05:42 PM

[quoted text, click to view]

Not really. Either way, it's going to involve a lot of data movement
on the backend.

Is it possible for you to just put a view on the front-end and order
the columns there and just allow your end users to use the view?



--
arbert
------------------------------------------------------------------------
Posted via http://www.codecomments.com
------------------------------------------------------------------------
Re: Rearrange fields alphabetically Adam Machanic
11/5/2005 2:48:16 PM
There is no supported method in SQL Server of moving columns in a table
except by dropping and re-creating the column. But you certainly don't have
to do it one-by-one...

Let's say we had the following table:

CREATE TABLE out_of_order
(
ColZ INT,
ColY INT,
ColX INT
)


We could create a new table with all of the same columns, in the right
order:

CREATE TABLE in_order
(
ColX INT,
ColY INT,
ColZ INT
)

.... and then INSERT all of the data from the other table:

INSERT in_order (ColX, ColY, ColZ)
SELECT ColX, ColY, ColZ
FROM out_of_order

.... and then it's a simple matter of dropping the old table and re-naming
the new one:

DROP TABLE out_of_order

sp_rename 'in_order', 'out_of_order', 'table'


--
Adam Machanic
Pro SQL Server 2005, available now
www.apress.com/book/bookDisplay.html?bID=457
--


[quoted text, click to view]

Re: Rearrange fields alphabetically zknezic
11/7/2005 9:51:04 AM
Thanks Adam,
At some point I started contemplating the idea of updating syscolumns table
(changing colorder). Firstly, modifying this table was not allowed by
default. Secondly, it appears that this could be a messy job. I'll stick with
your suggestion, clean and simple...

Thank you,

zknezic

[quoted text, click to view]
Re: Rearrange fields alphabetically zknezic
11/8/2005 8:32:03 AM
That is a possibility, it is just a matter of breaking a habit of those who
are using the table regularly. As they say "If there is a will, there is a
way"...

[quoted text, click to view]
AddThis Social Bookmark Button