Tuesday, 31 July 2007

SQL Server weirdness: Alter column datatype from ntext to nvarchar

Came across this SQL Server weirdness at work...if anyone is designing a database with column datatype of 'ntext'....then think twice! : O

You see i was writing up a SQL Server DB upgrade script and I wanted to alter a table column of datatype 'ntext' to 'nvarchar'. After I have written the SQL statement, executing it resulted in an error:
Cannot alter column 'xxx' because it is 'ntext'
Thankfully my beloved friend Google was able to help me out on this! Apparently this works in Enterprise Manager (hmm I wonder if it works for SQL Server 2005...but I would believe so) because EM does some magic to ensure it gets its job done for their user:
"You will see that it (EM) is actually copying the old table, creating a
new one, copying the data from old to new and dropping the old. That is
why it works in EM and not in the query tool!"
Right...so EM creates a new table, copy the data and then drop the old table, so that's why EM has no problem with it. But you see I am writing a script, so I am forced to 'write the query' myself! So all I need to do is to write a few more statements to manually emulate the EM behaviour in my script. (Not a hard task when table is small...but could become hideous if table is a big one)

I only have one complain about all this....ahha why can't SQL Server display more 'informative' error message! -.-" Thank goodness my friend Google is around! hehe : P

References (thanks to the following pages):
http://forums.devx.com/archive/index.php/t-48982.html
http://www.dbforums.com/archive/index.php/t-318199.html

1 comment: