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)
 changing a columns name or datatype via TSQL

Author  Topic 

AskSQLTeam
Ask SQLTeam Question

0 Posts

Posted - 2002-01-31 : 09:39:40
G. writes "Anyway to change a column's datatype and/or the name of the column through a stored proc?
i.e.:
start with: fName char(10)
change to: firstname varchar(50)


Dropping the table and recreating it isnt a simple solution since the data would have to be exported then re-imported.

Thanks,
G."

nr
SQLTeam MVY

12543 Posts

Posted - 2002-01-31 : 09:59:52
Changing the datatype you should be able to do by an alter table statement.
The name I don't think you can do like this though.
you could update syscolumns (after setting the system table updates flag) but I wouldn't advise this.

Probably best to rename the table, create a new table, copy all the data across then drop the old table.


==========================================
Cursors are useful if you don't know sql.
Beer is not cold and it isn't fizzy.
Go to Top of Page

lfmn
Posting Yak Master

141 Posts

Posted - 2002-01-31 : 13:15:00
you can change a column name with the following code:

create table test (test int)

select * from test

exec sp_rename 'test.[test]', 'test2', 'column'

drop table test

(see bol - sp_rename)

cursors are like hammers - sometimes you have to use them, but watch your thumb!
Go to Top of Page
   

- Advertisement -