I need a stored procedure that will reorder the columns for me, unfortunately, nothing else will do!
Main Topics
Browse All TopicsCan someone provide me with a stored procedure that will reorder table columns?
example: sp_ReorderTableColumns "tblPeople", "1,2,3,4,7,5,6"
The seventh column would now appear in the fifth position and the 5th and 6th columns would be bumpted to 6th and 7th.
This Question has been solved and asker verified All Experts Exchange premium technology solutions are available to subscription members.
Experts Exchange has been collecting answers to technology questions since 1996…3 million and counting! If you have a question, chances are we already have your answer.
If you can't find the exact answer you're looking for, ask our exclusive community of 50,000 experts. You’ll get a personalized answer from a trusted professional.
Thousands of free tech tips, tricks, how-to’s and tutorials are available in our peer reviewed articles section. See for yourself how smart our experts are, no login required.
Access the answers to your technology questions today.
30-day free trial. Register in 60 seconds.
Members of the expert community talk about why the experience at Experts Exchange is different than what you will find anywhere else.

Try it out and discover for yourself.
30-day free trial. Register in 60 seconds.
Join the community of experts here and help other tech pros by answering question in your area of expertise. You can earn FREE access to all Experts Exchange's premium features and resources.
I understand what you've suggested vis-a-vis the views. It does not address the problem I am trying to solve. I need a stored procedure that will physically reorder the fields in a table.
In the example I gave, the number refer to the existing column by their physical order. The new order that is supplied should be used to change that physical order.
example: sp_ReorderTableColumns "tblPeople", "1,3,2"
tblPeople
-Name
-Address
-Phone
would now be
tblPeople
-Name
-Phone
-Address
You will probably need to create a new table with the columns in the desired order, recreate all the keys and indexes, then copy the data from old table to new table, drop old table and rename temp table to original name.
Seems like an awful lot of work for not much return. What sort of problem could you be having where the order of the columns was significant?
WHY do you need to PHYSICALLY re-order the columns in the table? You can select/sort/etc. them in any order and/or combination that you need. So why do they have to be PHYSICALLY in a certain order? Besides, the columns are NOT necessarily physically stored in the same order as they are defined. All fixed-length columns are stored first, followed by all variable length columns.
Create procedure Reorder @Table varchar(50), @order varchar(50)
As
Declare @TableID as int, @Counter as smallint, @Query as varchar(200)
Select @TableID = id from sysobjects where name = @Table
if @TableID IS NOT NULL
BEGIN
if exists (select * from dbo.sysobjects where id = object_id(N'[tempTable]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
drop table [tempTable]
Set @Query = 'SELECT '
Set @Counter=1
IF RIGHT(@Order,1)!=',' Set @Order=@Order+','
WHILE CHARINDEX(',',@order,@COUN
BEGIN
Select @Query = @Query
+ Name + ', '
from syscolumns where id = @TableID and
ColOrder =cast( SUBSTRING(@ORDER,@COUNTER,
Select @Counter = CHARINDEX(',',@order,@COUN
END
Set @Query = LEFT(@Query, LEN(@Query)-1 ) + ' INTO tempTable FROM ' + @Table
Execute (@Query)
exec ('drop table '+@Table)
exec sp_rename 'tempTable', @Table
END
go
Then you can do:
reorder 'Clients','2,3,4,5,6,7,8,9
select * from Clients
It doesn't works if the table has foreign keys.
If there is any PK, it will dissapears after reordering...
basically this sp generates and executes this code for you:
SELECT Field1, Field2, FieldN, ... INTO tempTable FROM Clients
drop table tempTable
exec sp_rename 'tempTable', 'Clients'
I hope this help, (anyway, i'll never use this SP... your trying to do something really weird, and probably focusing the problem from a wrong point of view)
Saludos!
Gerardo Grignoli
i agree with the above comments that this is a very odd thing to do
and will in most cases have no bearing on the physical storage/retrieval of the information....
and may even lead to some of your existing queries failing (e.g. Inserts without a column list)
but if you know the order of the columns you want....
why don't you just generate the new table as gerardo basically suggests , (but directly using the column name)
SELECT Field1, Field2, FieldN, ... INTO tempTable FROM Clients
drop table clients
exec sp_rename 'tempTable', 'Clients'
hth
If it is a one time ordering, then you could just change the order in Enterprise Manager?
1. Highlight the row of the column you want to move.
2. Press CTRL+X
3. Find the new place for the column you are moving and highlight the row of the column after this new place.
4. Press CTRL+V
This will preserve your PK's, FK's, and indexes.
I can't imagine why the problem would require a stored proc because I don't see why you would be reordering the columns on a regular basis?
alaplume:
You don't say why you want or need to change the order. It's hard to find a comprehensive solution that meets your need if we don't know what those needs are. It may be that there is a better solution than changing the order, but until you tell us what the problem is, it's difficult to help, so please tell us.
The syscolumns table has a colorder column which *appears* to be the order in which the columns are shown. You could try copying the Northwind database and playing around with colorder, but take care, as it's marked 'for internal use only', so it could be disastrous.
>> I can't believe that noyone has ever written a stored procedure to do this. Gerardo_Grignoli's sp is a good start but is doesn't address constaints at all!
People don't write this sort of thing because people who know how to do it also know what a relational database is and so would realise it would not be a useful procedure.
Try addressing your underlying problem rather than trying to do something dangerous like this.
alaplume:
This old question needs to be finalized -- accept an answer, split points, or get a refund. For information on your options, please click here-> http:/help/closing.jsp#1
EXPERTS:
Post your closing recommendations! No comment means you don't care.
Business Accounts
Answer for Membership
by: nigelrivettPosted on 2003-05-28 at 14:08:19ID: 8601201
You can create a view that will present columns in the order you wish but it should really not make any difference to you unless you use bulk insert.
Another way is to update colid in syscolumns BUT DON"T TRY THAT.
Rename the table, create a new one then copy the data between the two.
That's what enterprise manager will do if you insert a column.