473,387 Members | 1,844 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Swap column position

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
phone 4
email 5
city 6
state 7
....

Now, I need to make city as 2 and state as 3. BTW, the tblXYZ table
has data there. Copy/select all the data into a new table with
desired column position would require constraints re-mapping etc.,
which seems quite a bit hassle.

Thanks.
Jul 20 '05 #1
6 9556

"Doug Baroter" <qw********@boxfrog.com> wrote in message
news:fc**************************@posting.google.c om...
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
phone 4
email 5
city 6
state 7
...

Now, I need to make city as 2 and state as 3. BTW, the tblXYZ table
has data there. Copy/select all the data into a new table with
desired column position would require constraints re-mapping etc.,
which seems quite a bit hassle.
I'm not sure what you mean. It might help if you can explain why the
physical order of the columns is relevant. (hint, generally it should not
be.)

I mean normally you'd simply do:

select xyzUUID, city, state, fn, ln, phone, email from tblXZY


Thanks.

Jul 20 '05 #2
Doug,

Ideally, very little should depend on the order in which
the columns are arranged, and every query should contain
a column list so that this won't matter.

The only thing I can think of that does depend on it offhand is
the COLUMNS_UPDATED() bitmask available to triggers,
and unfortunately, I've seen bugs where the column id values
on which it depends can end up with gaps.

I think you can do this in the Enterprise Manager design GUI,
by selecting, then dragging, the column definitions. Behind
the scenes it will copy all the data into a new table, I believe.

-- Steve Kass
-- Drew University
-- Ref: D64B08D4-9E4A-4F29-8847-5E9AAA89B900


Doug Baroter wrote:
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
phone 4
email 5
city 6
state 7
...

Now, I need to make city as 2 and state as 3. BTW, the tblXYZ table
has data there. Copy/select all the data into a new table with
desired column position would require constraints re-mapping etc.,
which seems quite a bit hassle.

Thanks.


Jul 20 '05 #3
Steve Kass (sk***@drew.edu) writes:
Ideally, very little should depend on the order in which
the columns are arranged, and every query should contain
a column list so that this won't matter.


When I add new columns to tables in our database, I very rarely put them
last, but I put them where they fit in logically. Say that I'm working
with Northwind..Customers and there is a requirement to add a column
Address2, because some companies have complex addresses. I would add
this column between Address and City, not after Fax.

This has nothing to do with how the code works in the database, but is for
the benefit for the people who work with the data model.

And while SELECT * is a no-no in code, I often use it debug situations.
When you have some 70 columns, it helps if you know where a column is,
and it certainly helps if two related columns are close to each other.

It is also an advantage if you have more than instance of the same database
that the column order is the same both databases, in case you need to
move data with bulk copy.

--
Erland Sommarskog, SQL Server MVP, so****@algonet.se

Books Online for SQL Server SP3 at
http://www.microsoft.com/sql/techinf...2000/books.asp
Jul 20 '05 #4
Hi

To do this you will have to look at creating a new table with the columns in
the order you require (you may want to use the "create SQL script" option to
do this)

Then INSERT the data using INSERT INT NewTable ( Col1... ) SELECT Col1,....
FROM OldTable.

Then drop the old table, and rename (using sp_rename) the new table.

This is basically what the code behind enterprise manager will do if you
force it by changing one of the existing columns in some way (e.g. change of
datatype). EM will just create a script that adds the columns onto the end
if you don't force it to re-create the table. To see the script generated by
the table designer choose the button with the scroll on it.

HTH

John

"Doug Baroter" <qw********@boxfrog.com> wrote in message
news:fc**************************@posting.google.c om...
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
phone 4
email 5
city 6
state 7
...

Now, I need to make city as 2 and state as 3. BTW, the tblXYZ table
has data there. Copy/select all the data into a new table with
desired column position would require constraints re-mapping etc.,
which seems quite a bit hassle.

Thanks.

Jul 20 '05 #5
Steve Kass <sk***@drew.edu> wrote in message news:<bg**********@slb2.atl.mindspring.net>...
Doug,

Ideally, very little should depend on the order in which
the columns are arranged, and every query should contain
a column list so that this won't matter. Agree 100%.
The only thing I can think of that does depend on it offhand is
the COLUMNS_UPDATED() bitmask available to triggers,
and unfortunately, I've seen bugs where the column id values
on which it depends can end up with gaps. Can't help any help (further information) on COLUMNS_UPDATED()
function from BOL.
I think you can do this in the Enterprise Manager design GUI,
by selecting, then dragging, the column definitions. Behind
the scenes it will copy all the data into a new table, I believe. This is what I was looking for. I should have tried it first myself.
Don't know why sometime I hesititate to experiment nowadays.

Thanks.

Li, Chunshen (Don)
P.S. "Doug Baroter" is not my name, was being mischievious, and not
Tom Sawyer :)
-- Steve Kass
-- Drew University
-- Ref: D64B08D4-9E4A-4F29-8847-5E9AAA89B900


Doug Baroter wrote:
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
phone 4
email 5
city 6
state 7
...

Now, I need to make city as 2 and state as 3. BTW, the tblXYZ table
has data there. Copy/select all the data into a new table with
desired column position would require constraints re-mapping etc.,
which seems quite a bit hassle.

Thanks.

Jul 20 '05 #6
Thanks anyway. I should have said the rationale for column position
swap is that they've already been defined, however, as you pointed
out, columns should be logically positioned. So, when they are not,
they need swap.

Erland Sommarskog <so****@algonet.se> wrote in message news:<Xn**********************@127.0.0.1>...
Steve Kass (sk***@drew.edu) writes:
Ideally, very little should depend on the order in which
the columns are arranged, and every query should contain
a column list so that this won't matter.


When I add new columns to tables in our database, I very rarely put them
last, but I put them where they fit in logically. Say that I'm working
with Northwind..Customers and there is a requirement to add a column
Address2, because some companies have complex addresses. I would add
this column between Address and City, not after Fax.

This has nothing to do with how the code works in the database, but is for
the benefit for the people who work with the data model.

And while SELECT * is a no-no in code, I often use it debug situations.
When you have some 70 columns, it helps if you know where a column is,
and it certainly helps if two related columns are close to each other.

It is also an advantage if you have more than instance of the same database
that the column order is the same both databases, in case you need to
move data with bulk copy.

Jul 20 '05 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: SAN CAZIANO | last post by:
how can i swap column in a html table ???
7
by: mark | last post by:
Please excuse this blatant attempt at advertising. I hope you find this software both interesting and useful. http://www.caddproductivity.com/dataswap.htm Data Swap The Data Swap program...
10
by: Atlas | last post by:
To swap to nodes in list, changing pointers is enough. But the stl swap method seems to copy their values, doesn't it?
0
by: Kevin Bartz | last post by:
-----Original Message----- From: Kevin Bartz Sent: Monday, August 09, 2004 10:37 AM To: 'mike@thegodshalls.com' Subject: RE: Out of swap space & memory Thanks for your reply, Mike!...
1
by: ChitownE | last post by:
Here's what I'm looking to do: fixed left column and a right column that fills the remainder of the space. I need the ability to position other elements relative to the borders of the right...
1
by: Phil | last post by:
Is it possible to swap rows and columns in select query output so that each record's data is displayed in a column? I want to collect data each day and display it in a query with each day's date...
12
by: slartybartfast | last post by:
I'm new(ish) to css, comfortable using tables, but trying real hard to move away. Please see http://84.9.125.31/developer/css_test/test5.html NB This issue is with IE & Opera - I've tried IE...
28
by: Jess | last post by:
Hello, It is said that if I implement a "swap" member function, then it should never throw any exception. However, if I implement "swap" non- member function, then the restriction doesn't...
9
by: Thomas Watson | last post by:
I have a two column layout - this is the regular story about a sidebar and a column containing the main content. In the HTML source I've put the main content div first and the sidebar div second...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.