SQL Server - script to update database columns from varchar to nvarchar if not already nvarchar

前端 未结 5 1759
南笙
南笙 2020-12-16 19:02

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 applicati

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-16 19:48

    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
    

提交回复
热议问题