Welcome to dbFreaks.com!
FAQFAQ    SearchSearch      ProfileProfile    Private MessagesPrivate Messages   Log inLog in

possible problems altering column order in a table via code?

 
   Database Help (Home) -> Programming RSS
Next:  nested hirerarchical data from sql to xml  
Author Message
Keith G Hicks

External


Since: Mar 18, 2004
Posts: 117



(Msg. 1) Posted: Wed Oct 18, 2006 9:19 am
Post subject: possible problems altering column order in a table via code?
Archived from groups: microsoft>public>sqlserver>programming (more info?)

Right off the bat, I'm not interested in anyone giving me grief regarding
this issue. I have (IMHO) valid reasons for this. And I also will refer you
to Erland Sommarskog's response to a similar post a couple years ago:
http://groups.google.com/group/comp.databases.ms-sqlserver/browse_thre...thread/

Having got that out of the way, here's my question. I know that the column
order is stored in syscolumns.colorder. Does anyone know of any problems
that can occur by changing that value after adding a new column to a table?
For example, I have the following table:

Customers:
CustID
CustName
CustPhone

I added a new column as follows:

ALTER TABLE Customer ADD CustSex so that I end up with this as viewed from
EM:

Customers:
CustID
CustName
CustPhone
CustSex

However I want this:

Customers:
CustID
CustSex
CustName
CustPhone

It seems that I could simply run the following:

UPDATE tcols SET ColOrder = ColOrder + 1 FROM syscolumns tcols INNER JOIN
sysobjects tojbs ON syscolumns.id = sysobjects.id WHERE tobjs.name =
'Customer' AND ColOrder >= 2

UPDATE tcols SET ColOrder = 2 FROM syscolumns tcols INNER JOIN sysobjects
tojbs ON syscolumns.id = sysobjects.id WHERE tobjs.name = 'Customer' AND
tcols.name = 'CustSex'

Thanks,

Keith

 >> Stay informed about: possible problems altering column order in a table via code? 
Back to top
Login to vote
Aaron Bertrand [SQL Serve

External


Since: Jan 10, 2008
Posts: 2166



(Msg. 2) Posted: Wed Oct 18, 2006 9:26 am
Post subject: Re: possible problems altering column order in a table via code? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

> Does anyone know of any problems
> that can occur by changing that value after adding a new column to a
> table?

YES! Do not modify system tables. Period.

A

 >> Stay informed about: possible problems altering column order in a table via code? 
Back to top
Login to vote
Keith G Hicks

External


Since: Mar 18, 2004
Posts: 117



(Msg. 3) Posted: Wed Oct 18, 2006 1:24 pm
Post subject: Re: possible problems altering column order in a table via code? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Back to top
Login to vote
Michael Epprecht [MSFT]

External


Since: Feb 15, 2007
Posts: 5



(Msg. 4) Posted: Wed Oct 18, 2006 3:34 pm
Post subject: Re: possible problems altering column order in a table via code? [Login to view extended thread Info.]
Archived from groups: per prev. post (more info?)

Hi

To add to Aaron's comments.

The code will not work in SQL Server 2005 as you can't modify system tables
and what you are doing is not supported by Microsoft for earlier versions of
SQL Server..

Regards
--
Mike

This posting is provided "AS IS" with no warranties, and confers no rights.


"Aaron Bertrand [SQL Server MVP]" wrote in message

>> Does anyone know of any problems
>> that can occur by changing that value after adding a new column to a
>> table?
>
> YES! Do not modify system tables. Period.
>
> A
>
 >> Stay informed about: possible problems altering column order in a table via code? 
Back to top
Login to vote
Display posts from previous:   
Related Topics:
Altering a table's column to set it Identity Seed - Hi , I have an application which was intially not using the autonumbering feature in SQL2k5 and the primary key values were populated via a manually generated sequence numbering scheme. A lot of data went into these tables in this way. However this....

Altering column width - Not a lot of SQL experience here. Trying to alter a column width (character). Normal ANSII SQL ALTER statements do not seem to apply. How is this accomplished in MSoft SQL? thx

How to create a checkbox datatype column in a table in VB .. - I am creating a new boolean column in a MSAccess .mdb database like this: Dim Cmd As New OleDb.OleDbCommand("ALTER TABLE m_table ADD boolColumn YesNo", objConn) Cmd.ExecuteNonQuery() , which works fine. But... when the .mdb file is opened in ...

Column Order, left to right and using underscores with col.. - Back in the day, we were told to be careful ordering our columns. Grouping columns and columns you deem as searchable should be to the left. Is that still an issue? I mean optimizers have become so robust and intelligent, does it matter any more? ..

ORDER BY time of a column - Is it possible to order by time eg the column contains 01/02/2007 09:00 03/02/2007 10:00 05/02/2007 08:00 06/02/2007 07:00 results would be 06/02/2007 07:00 05/02/2007 08:00 01/02/2007 09:00 03/02/2007 10:00 cheers mark
   Database Help (Home) -> Programming All times are: Pacific Time (US & Canada)
Page 1 of 1

 
You can post new topics in this forum
You can reply to topics in this forum
You can edit your posts in this forum
You can delete your posts in this forum
You can vote in polls in this forum



[ Contact us | Terms of Service/Privacy Policy ]