Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Alter Table -- column order?

Author  Topic 

SamC
White Water Yakist

3467 Posts

Posted - 2005-03-04 : 16:09:12
Is there a way to alter the order of columns in a table without copying and replacing the table

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-04 : 16:12:09
No.
And the columnorder is purely cosmetic (at least logically).

rockmoose
Go to Top of Page

SamC
White Water Yakist

3467 Posts

Posted - 2005-03-04 : 16:16:34
I thought not, but thanks for confirming...

Cosmetic has benefits at times.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-04 : 16:20:49
>> Cosmetic has benefits at times

Apply a layer of makeup or create a nice view of the object.

rockmoose
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-03-05 : 12:17:28
You can alter it in syscolumns. That's a REALLY, REALLY bad idea. But, it does work. And.....I just happen to have a script just for this (which I never use on my stuff):


--Create the table to fix this problem, that's not really a problem. It's just people not getting databases.
CREATE TABLE djl_test_colid(
int1 INT,
int2 INT,
value VARCHAR(55),
int3 INT)

INSERT djl_test_colid(int1, int2, value, int3)
SELECT 1,2,'Here''s the problem',4

SELECT * FROM djl_test_colid

--Notice that the colid is what "physically" orders the table. It doesn't really, but hey that's another issue.
---Because of this, you would have to recreate the table to reorder, which is what EM does because EM is stupid.
SELECT
so.name,
sc.name AS column_name,
sc.colid
FROM
sysobjects so
INNER JOIN syscolumns sc ON so.id = sc.id
WHERE
so.name = 'djl_test_colid'

--The other option is to muck around with the system tables, which I highly recommend against.
UPDATE sc
SET colid =
CASE
WHEN colid = 3 THEN 4
WHEN colid = 4 THEN 3
ELSE colid
END
FROM
sysobjects so
INNER JOIN syscolumns sc ON so.id = sc.id
WHERE
so.name = 'djl_test_colid'


SELECT * FROM djl_test_colid

--Notice now that everything is right in the order of orderdom.
SELECT
so.name,
sc.name AS column_name,
sc.colid
FROM
sysobjects so
INNER JOIN syscolumns sc ON so.id = sc.id
WHERE
so.name = 'djl_test_colid'

--Clean up after yourself. gheesh
DROP TABLE djl_test_colid


MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page

rockmoose
SQL Natt Alfen

3279 Posts

Posted - 2005-03-05 : 13:32:06
>> That's a REALLY, REALLY bad idea...
Ok, Why?

rockmoose
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2005-03-05 : 13:46:58
Because you're messing with the system tables for no good reason. That should be reason enough. There are also some really weird issues with this. Until you clear out the cache, any stored procedures, inline code, etc that are relying on the order for some reason will not be corrected until the dynamic and procedure caches are completely flushed of those plans. The diagrams do change immediately and reflect the new order.

To me, it's a cosmetic issue. So, you deal with it and educate people on it. You avoid coding in a manner that would be dependent on this order being correct. If you want to change the system tables though, feel free to do so.

MeanOldDBA
derrickleggett@hotmail.com

When life gives you a lemon, fire the DBA.
Go to Top of Page
   

- Advertisement -