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
 Old Forums
 CLOSED - General SQL Server
 ALTER TABLE

Author  Topic 

ashy_16in
Starting Member

16 Posts

Posted - 2004-07-16 : 17:17:16
While using ALTER TABLE command in SQL to add a new column, it always creates the column at the end of the table schema. Is it possible to specify the exact position of the new column within the table schema without using Enterprise Manager ?. I am having some issues using Enterprise Manager since it copies all the records to a temp table first and this process takes a long time.

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-07-16 : 17:30:13
keep it in 1 forum please!
Go to Top of Page

eyechart
Master Smack Fu Yak Hacker

3575 Posts

Posted - 2004-07-16 : 17:38:55
The only way to insert a column in the middle is to use a temp table like EM does. btw, the order of columns is not important.


-ec
Go to Top of Page

kselvia
Aged Yak Warrior

526 Posts

Posted - 2004-07-17 : 04:00:01
EM Creates a new table dosen't it? You could shorten that by creating the new column and moving the old column data to the end and renameing the columns.

create table t (t1 int, t2 int)

insert t select 1, 2

select * from t
t1 t2
----------- -----------
1 2

alter table t add placeholder int

update t set placeholder = t1

update t set t1 = null

exec sp_rename 't.t1', 't3', 'COLUMN'

exec sp_rename 't.placeholder', 't1', 'COLUMN'

select * from t
t3 t2 t1
----------- ----------- -----------
NULL 2 1


--Ken
Your Kung-Fu is not strong. -- 'The Core'
Go to Top of Page

Kristen
Test

22859 Posts

Posted - 2004-07-17 : 04:19:19
Why is the column order important to you? Knowing that might enable me to think of something!

Kristen
Go to Top of Page

derrickleggett
Pointy Haired Yak DBA

4184 Posts

Posted - 2004-07-17 : 09:06:44
Yeah, like redo your application so it's not important anymore. :) You can always display it however you want. The physical order of a column shouldn't be important. If you are set on this though, create the column at the end like it always does unless you drop and recreate, then just reorder it. I really, really hate doing this though.



--Create the table to fix this awful nightmare of a situation
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.
---Because of this, you have to recreate the table to reorder, which is what EM does.
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

Kristen
Test

22859 Posts

Posted - 2004-07-17 : 11:18:45
I suppose you could set up a VIEW

CREATE VIEW MyView
AS
SELECT
MyCol1,
MyCol2,
MyNEWCol,
MyCol3,
MyCol4
FROM MyTable

Kristen
Go to Top of Page
   

- Advertisement -