473,651 Members | 2,496 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9567

"Doug Baroter" <qw********@box frog.com> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
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..Cust omers 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********@box frog.com> wrote in message
news:fc******** *************** ***@posting.goo gle.com...
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.mi ndspring.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..Cust omers 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
3062
by: SAN CAZIANO | last post by:
how can i swap column in a html table ???
7
2040
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 works with Microsoft® Access databases to make complex queries or repetitive tasks simple.
10
10035
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
2457
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! Theoretically, I should need only six of the columns, but as a means of verifying integrity, I would like to de-dup using all the columns. For instance, if there are two rows identical everywhere but some column outside the six, I would like to know about...
1
2090
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 column. Without nesting containers, here's the best cross-browser version I could come up with: http://www.singley.org/test/two_cols2.html I see what's wrong - the right column is getting offset by the left column which pushes the viewport out...
1
5961
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 along the top row and the data items below it. This would give a better appreciation of changes over time. I apologise if I'm missing something obvious. Phil
12
4197
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 6&7 and both have the same result. It works fine in FF. Thanks to Petr Stanicek for the original example. As you can see the table is 800px wide in the middle column and it's vertical position is affected by the amount of text in the left or right...
28
2839
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 apply. Can somebody tell me why? Thanks, Jess
9
5474
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 - This is an important point. I do this because I would like the main content of the site to be at the top of the HTML source (to please screen readers and search bots) But in the browser I would like to show the sidebar to the left of the...
0
8352
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8275
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8802
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8697
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8579
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6158
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5612
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4283
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1587
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.