SQL Server - 用于将数据库列从varchar更新为nvarchar的脚本(如果尚未nvarchar)

时间:2008-10-07 19:34:54

标签: sql-server alter-table

我遇到的情况是必须使用脚本将现有数据库结构从varchar更新为nvarchar。由于每次运行配置应用程序时都会运行此脚本,因此我宁愿确定列是否已更改为nvarchar而不是对表执行alter。我必须支持的数据库是SQL Server 2000,2005和2008.

5 个答案:

答案 0 :(得分:26)

您可以运行以下脚本,该脚本将为您提供一组ALTER命令:

SELECT 'ALTER TABLE ' + isnull(schema_name(syo.id), 'dbo') + '.' +  syo.name 
    + ' ALTER COLUMN ' + syc.name + ' NVARCHAR(' + case syc.length when -1 then 'MAX' 
        ELSE convert(nvarchar(10),syc.length) end + ');'
   FROM sysobjects syo
   JOIN syscolumns syc ON
     syc.id = syo.id
   JOIN systypes syt ON
     syt.xtype = syc.xtype
   WHERE 
     syt.name = 'varchar' 
    and syo.xtype='U'

然而,您有几个快速的警告

  1. 这只会做表格。您需要扫描所有的sprocs和函数,以确保它们也被更改为NVARCHAR
  2. 如果您有VARCHAR> 4000您需要将其修改为NVARCHAR(MAX)
  3. 但是这些模板应该很容易实现。

    如果您希望自动运行,可以在WHILE子句中设置它。

答案 1 :(得分:4)

Josef的答案是,在执行查询后,它会将NOT NULL字段更改为NULL。以下操作修复了它:

SELECT cmd = 'alter table [' + c.table_schema + '].[' + c.table_name 
 + '] alter column [' + c.column_name + '] nvarchar('
 +CASE WHEN CHARACTER_MAXIMUM_LENGTH<=4000
       THEN CAST(CHARACTER_MAXIMUM_LENGTH as varchar(10)) ELSE 'max' END+')' 
 + CASE WHEN IS_NULLABLE='NO' THEN ' NOT NULL' ELSE '' END,*
FROM information_schema.columns c
WHERE c.data_type='varchar' 
ORDER BY CHARACTER_MAXIMUM_LENGTH desc
  

Igor's answer的信用

答案 2 :(得分:2)

以下查询可以满足您的需求:

IF EXISTS 
  (SELECT *
   FROM sysobjects syo
   JOIN syscolumns syc ON
     syc.id = syo.id
   JOIN systypes syt ON
     syt.xtype = syc.xtype
   WHERE 
     syt.name = 'nvarchar' AND
     syo.name = 'MY TABLE NAME' AND
     syc.name = 'MY COLUMN NAME')
BEGIN
   ALTER ...
END

答案 3 :(得分:2)

修复了空间问题并添加了架构

SELECT 'ALTER TABLE [' + isnull(schema_name(syo.object_id), sysc.name) + '].[' +  syo.name 
    + '] ALTER COLUMN ' + syc.name + ' NVARCHAR(' + case syc.max_length when -1 then 'MAX' 
        ELSE convert(nvarchar(10),syc.max_length) end + ');'
   FROM sys.objects syo
   JOIN sys.columns syc ON
     syc.object_id= syo.object_id
   JOIN sys.types syt ON
     syt.system_type_id = syc.system_type_id
    JOIN sys.schemas sysc ON
    syo.schema_id=sysc.schema_id
   WHERE 
     syt.name = 'varchar' 
    and syo.type='U'

答案 4 :(得分:0)

进一步更新以修复MAX被替换为-1。

SELECT cmd = 'ALTER TABLE [' + c.table_schema + '].[' + c.table_name 
 + '] ALTER COLUMN [' + c.column_name + '] NVARCHAR('
 +CASE WHEN CHARACTER_MAXIMUM_LENGTH<=4000 THEN 
 CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN
 'MAX' ELSE CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) END ELSE 'MAX' END+')' 
 + CASE WHEN IS_NULLABLE='NO' THEN ' NOT NULL' ELSE '' END,*
FROM information_schema.columns c
WHERE c.data_type='VARCHAR' 
ORDER BY CHARACTER_MAXIMUM_LENGTH DESC

贷记Nezam's Answer

另一个用于管理默认值的

SELECT cmd = 
CASE WHEN name IS NOT NULL THEN
    'ALTER TABLE ' + c.table_name + ' DROP CONSTRAINT ' + d.name + '; ' +
    'ALTER TABLE [' + c.table_schema + '].[' + c.table_name + '] ALTER COLUMN [' + c.column_name + '] ' + 
    'NVARCHAR(' +
    CASE WHEN CHARACTER_MAXIMUM_LENGTH <= 4000 THEN 
        CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN
            'MAX' 
        ELSE 
            CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) 
        END 
     ELSE 
        'MAX' 
    END 
    + ')' +
    CASE WHEN IS_NULLABLE='NO' THEN ' NOT NULL' ELSE '' END + '; ' + 
    'ALTER TABLE '+ c.table_name + ' ADD CONSTRAINT ' + d.name +' DEFAULT '+ c.column_default + ' FOR ' + c.column_name + ';'
ELSE
    'ALTER TABLE [' + c.table_schema + '].[' + c.table_name + '] ALTER COLUMN [' + c.column_name + '] ' +
    'NVARCHAR(' +
    CASE WHEN CHARACTER_MAXIMUM_LENGTH<=4000 THEN
        CASE WHEN CHARACTER_MAXIMUM_LENGTH = -1 THEN
            'MAX' 
        ELSE 
            CAST(CHARACTER_MAXIMUM_LENGTH AS VARCHAR(10)) 
        END 
    ELSE 
        'MAX' 
    END
    + ')' +
    CASE WHEN IS_NULLABLE='NO' THEN ' NOT NULL' ELSE '' END 
END,d.name, c.*
FROM information_schema.columns c
LEFT OUTER JOIN sys.default_constraints d ON d.parent_object_id = object_id(c.table_name)
AND d.parent_column_id = columnproperty(object_id(c.table_name), c.column_name, 'ColumnId')
WHERE c.data_type='VARCHAR' 
ORDER BY CHARACTER_MAXIMUM_LENGTH DESC