Invalid column name on sql server update after column create

后端 未结 4 1455
暗喜
暗喜 2020-12-06 09:34

Does anyone see what\'s wrong with this code for SQL Server?

IF NOT EXISTS(SELECT *
              FROM   sys.columns
              WHERE  Name = \'OPT_LOCK\'         


        
4条回答
  •  粉色の甜心
    2020-12-06 10:01

    For your information,you can replace the IF NOT EXISTS with the COL_LENGTH function. It takes two parameters,

    1. Table Name and

    2. Column you are searching for

    If the Column is found then it returns the range of the datatype of the column Ex: Int (4 bytes), when not found then it returns a NULL.

    So, you could use this as follows and also combine 3 Statements into one.

    IF (SELECT COL_LENGTH('REP_DSGN_SEC_GRP_LNK','OPT_LOCK')) IS NULL
    
    BEGIN
    
        ALTER TABLE REP_DSGN_SEC_GRP_LNK
        ADD OPT_LOCK NUMERIC(10, 0) NOT NULL DEFAULT 0
    
    END;
    

    Makes it simpler.

提交回复
热议问题