Change datatype varchar to nvarchar in existing SQL Server 2005 database. Any issues?

前端 未结 5 682
梦如初夏
梦如初夏 2021-01-03 18:14

I need to change column datatypes in a database table from varchar to nvarchar in order to support Chinese characters (currently, the varchar

相关标签:
5条回答
  • 2021-01-03 18:51

    Note that this change is a size-of-data update, see SQL Server table columns under the hood. The change will add a new NVARCHAR column, it will update each row copying the dta from the old VARCHAR to the new NVARCHAR column, and then it will mark the old VARCHAR column as dropped. IF the table is large, this will generate a large log, so be prepared for it. After the update, run DBCC CLEANTABLE to reclaim the space used by the former VARCHAR column. If you can afford it , better run ALTER TABLE ... REBUILD, which will not only reclaim the space it will also completely remove physical deleted VARCHAR column. The linked article at the beginning has more details.

    You may also be interested in enabling Unicode Compression for your table.

    0 讨论(0)
  • 2021-01-03 18:57

    Check all the dependencies for this table as stored procs, functions, temp tables based on this table and variables used for inserts/updates etc may also need to be updated to NVARCHAR. Also check if the table is in replication! That could cause you a new set of problems!

    0 讨论(0)
  • 2021-01-03 18:59

    You can do on non primary key fields:

    ALTER TABLE [TableName]
    ALTER COLUMN [ColumnName] nvarchar(N) null
    

    On the primary key fields it will not work - you will have to recreate the table

    0 讨论(0)
  • 2021-01-03 19:14

    Make sure that the length doesn't exceed 4000 since the maximum for VARCHAR is 8000 while NVARCHAR is only 4K.

    0 讨论(0)
  • 2021-01-03 19:15

    The table will get bigger. Each character in the column will take twice the space to store. You might not notice unless the table is really big.

    Stored procedures/views/queries that work with the column data might need to be modified to deal with the nvarchar.

    0 讨论(0)
提交回复
热议问题