473,503 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

changing the position of column by writing a query

8 New Member
is there any way to change the position of a column in a table by writing sql query?

for eg;
Table 'demo' contains 3 fields:
companyName
EmpName
id

I need to move the 'id' from last position to first by writing a query along with the data..

can anybody help me pls...
May 26 '08 #1
2 9566
deepuv04
227 Recognized Expert New Member
is there any way to change the position of a column in a table by writing sql query?

for eg;
Table 'demo' contains 3 fields:
companyName
EmpName
id

I need to move the 'id' from last position to first by writing a query along with the data..

can anybody help me pls...
Hi,
You can do in two ways.
1. ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE
then update the table as
update Table_Name set New_Column = Old_Column
then delete the old column
Alter table Table_Name drop column Old_Column
( Create new columns so that they must be in the order you want)

2. Create a new table as
SELECT Id,CompanyName,EmpName into New_Table From Table_Name
then drop the old table
then rename the original table


why do you want to? As per relational database theory the order of the
columns in a table is not important. If you want to see the columns in certain order specify the column names in some specific order in select query

thanks
May 26 '08 #2
ck9663
2,878 Recognized Expert Specialist
Hi,
You can do in two ways.
1. ALTER TABLE TABLE_NAME ADD COLUMN_NAME DATATYPE
then update the table as
update Table_Name set New_Column = Old_Column
then delete the old column
Alter table Table_Name drop column Old_Column
( Create new columns so that they must be in the order you want)

2. Create a new table as
SELECT Id,CompanyName,EmpName into New_Table From Table_Name
then drop the old table
then rename the original table


why do you want to? As per relational database theory the order of the
columns in a table is not important. If you want to see the columns in certain order specify the column names in some specific order in select query

thanks

I would agree with you.

Mary, If you need this columns re-arrange so that you can BULK INSERT into it, I would suggest you just create a temp table that you can use for that and check the quality of the records that you will be inserting to your table. This way your don't just insert data without knowing it's quality.

If this is for display purposes, just do something l ike:

Expand|Select|Wrap|Line Numbers
  1. select id. companyName, EmpName from YourTable
May 26 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

6
9557
by: Doug Baroter | last post by:
What is a good method/mechanism to swap the position of multiple columns? For instance, tblXZY has the followings columns and respective positions: tblXZY ====== xyzUUID 1 fn 2 ln 3...
1
6457
by: iporter | last post by:
Hi - I am changing a field from type nvarchar to type text, given that I need to store strings longer than 255 characters. To do this I change the data type in SQL Server, then I change the...
6
4578
by: Brett Maton | last post by:
Hi NG, How do I find out the size of a column ? I am retrieving large objects from the pg_largeobject table and creating a files, I would like to know the length of the data column so that I...
20
2734
by: Steve Jorgensen | last post by:
Hi all, I've just finished almost all of what has turned out to be a real bear of a project. It has to import data from a monthly spreadsheet export from another program, and convert that into...
1
8820
by: Vinny Vinn | last post by:
I would like to insert a new column in DataTable at a specified position at runtime. Unfortunatly, the DataColumnCollection does not expose any method to do this (like AddAt),Any ideas would be...
0
926
by: Gary | last post by:
Hi all, By default when we create a Template Column, the position of the Template Column will be 1. Is it possible to change its position at Design Time or Runtime i.e if I want a Template...
4
2969
by: Scot L. Harris | last post by:
Currently using Postgresql 7.2.4-5.80 with php 4.2.2.-8.0.8 on a redhat 8.0 system. I am writing some php scripts where I want to generate a list of the column names in a particular table that...
3
4665
by: Aziz | last post by:
1. I have a shopping basket DataGrid with a list of products. What I want to do is when the user clicks on a row, a button will become visible/be created that allows user to edit the quantity. The...
4
4662
by: Billy | last post by:
Hi all, I'm building a text file from a database table using the ASP Write Method and would like to position the cursor in a specific column position before writing the fields. As I loop through...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7074
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7451
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5572
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5000
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3161
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3150
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
731
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
374
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.