SQL Server - 将数据库列从 varchar 更新为 nvarchar(如果还不是 nvarchar)的脚本

2021-11-27 00:00:00 sql-server alter-table

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

I am in a situation where I must update an existing database structure from varchar to nvarchar using a script. Since this script is run everytime a configuration application is run, I would rather determine if a column has already been changed to nvarchar and not perform an alter on the table. The databases which I must support are SQL Server 2000, 2005 and 2008.

推荐答案

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

The following query should get you what you need:

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

相关文章