Using variable for UPDATE() the checker if the column is Updated or not

前端 未结 2 506
温柔的废话
温柔的废话 2021-01-29 06:03

Is it possible to use a variable in UPDATE() which check if a column is updated or not?

here is my sample code:

DECLARE @ColumnCount int
DECLARE @ColumnC         


        
2条回答
  •  情书的邮戳
    2021-01-29 06:15

    A few things: You may want to handle the first time through the loop because the name would not have changed the first time through. You may want to handle when @ColumnName is null because that would mean your query did not return a row although your query below should always return a value.

    DECLARE @ColumnCount INT 
    DECLARE @ColumnCounter INT 
    DECLARE @ColumnName NVARCHAR(max) 
    DECLARE @temp varchar(max)
    
    SET @ColumnCounter = 0 
    
    SELECT @ColumnCount = Count(c.column_name) 
    FROM   information_schema.columns c 
    WHERE  c.table_name = 'Province' 
    
    WHILE @ColumnCount >= @ColumnCounter 
      BEGIN 
          SET @ColumnName = NULL 
    
          SELECT @ColumnName = c.column_name 
          FROM   information_schema.columns c 
          WHERE  c.table_name = 'Province' 
                 AND c.ordinal_position = @ColumnCounter 
    
          IF ( @ColumnName != @temp ) 
            BEGIN 
            --do something 
            END
          SET @temp = @ColumnName
          SET @ColumnCounter = @ColumnCounter + 1 
      END 
    

提交回复
热议问题