Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Dear,,

I am Viswanathan.M...... I have a doubt in SQL sever 2005.. Now i need to rearrange the columns in a table by using query.The most important thing is after execute the query the table should be originally affect. whenever i open the table it ll show the affected table only not original..

This is my table,

col1 col2 col3

now i need this as in this format,

col1 col3 col2


This 2nd one should be my original table.


With expectation,
Viswanathan.M
Posted
Updated 6-Apr-11 23:00pm
v2
Comments
Om Prakash Pant 7-Apr-11 2:50am    
your question is not clear. do you want to rearrange the columns of existing table?
Viswanthan.M. 7-Apr-11 5:03am    
This is my table,

col1 col2 col3

now i need this as in this format,

col1 col3 col2

After execute the query
This 2nd one should be my original table.

Om Prakash Pant 7-Apr-11 7:34am    
The position of columns does not matter. By using the select statelemt you can have it at the place you want.
SELECT col1,col3,col2 from table_name

If you want to rearrange then you have to use SELECT ... INTO .. which will create new table with the order you want.
Viswanthan.M. 8-Apr-11 6:35am    
Thank you Mr. It is working..
Sandeep Mewara 7-Apr-11 2:53am    
Kindly elaborate on what you mean by 'reorder by query'?

Are you trying to change the way it looks? Change the physical ordering of the data?

The easiest way (assuming you are using databinding of some sort) is to ask for the columns in the order you want:
SQL
SELECT col1, col2 FROM table
will have "col1" on the left of your table
SQL
SELECT col2, col1 FROM table
will have "col1" on the right.
 
Share this answer
 
Try this
SQL
--- rename table 
EXEC sp_rename 'myTable','myTable_OLD'
--- import and create table from old table
select col1, col3, col2
into  myTable
from  myTable_OLD
--- drop old table
drop table myTable_OLD
 
Share this answer
 
v2
Comments
Viswanthan.M. 8-Apr-11 6:36am    
Thank you for your valuable reply.. it is working.
Costica U 14-Apr-11 10:12am    
You are welcome! Please mark it as accepted solution!
try this

create new Table then

insert into NewTable select Column1,Column3,Column2 from OldTable
 
Share this answer
 
v2
Comments
Viswanthan.M. 7-Apr-11 6:07am    
Thank you, but my question is, i need to rearrange the columns in a same table..ok?
Mahendra.p25 7-Apr-11 6:51am    
First You Can rename your old table then create new one with the same Name and then delete the old table as shown below by Costica U atleast this much you can do it by Own.
Viswanthan.M. 8-Apr-11 0:41am    
Thank you for your help.. I got it...
To change the column order

1. In Object Explorer, right-click the table with columns you want to reorder and click Design.
2. Select the box to the left of the column name that you want to reorder.
3. Drag the column to another location within the table.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900