Case statement to check if column exist in table

Padmanabhan, Venkatesh 246 Reputation points
2021-12-08T06:31:37.587+00:00

Hi.
Is there an option to check , if a particular column name exist in a table using a query ? If exists, then execute a sql statement else execute another sql statement ?

How to achieve this ?
Thanks

Developer technologies Transact-SQL
SQL Server Other
{count} votes

4 answers

Sort by: Most helpful
  1. Vaibhav Chaudhari 38,916 Reputation points Volunteer Moderator
    2021-12-08T06:50:05.377+00:00

    Check if below query works

    if exists(
    select 1 from INFORMATION_SCHEMA.COLUMNS
    where TABLE_NAME = 'DimEmployee' and COLUMN_NAME = 'FirstName'
    )
    begin
    select 'exists'
    end
    
    else
    
    begin
    select 'not exists'
    end
    

    Please don't forget to Accept Answer and Up-vote if the response helped -- Vaibhav


  2. Olaf Helper 47,436 Reputation points
    2021-12-08T07:10:50.743+00:00

    I am getting error as - invalid column name

    The query get's compile in once and if you have a query part which access a non-exists column (as I guess by your post) then compilation fails.
    A simple CASE condition won't work for you.
    You can try dynamic SQL, but that's not a big fun.

    0 comments No comments

  3. LiHong-MSFT 10,056 Reputation points
    2021-12-08T07:57:44.713+00:00

    Hi,@Padmanabhan, Venkatesh
    Welcome to the microsoft tsql Q&A forum!
    Please check this:

    DECLARE @column_name TABLE (Name nvarchar(50))  
    INSERT INTO @column_name      
    SELECT column_name FROM information_schema.columns   
    WHERE table_name = 'TABLE_NAME To Search'   
      
    IF EXISTS(select * from @column_name  
              where Name ='particular column name')  
    SELECT 'a sql statement'  
    ELSE   
    SELECT 'another sql statement'   
    

    Best regards,
    LiHong


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our Documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  4. Tom Phillips 17,771 Reputation points
    2021-12-08T20:07:56.85+00:00

    To avoid the compiler error, you must use "dynamic SQL'

    DECLARE @sqlcmd VARCHAR(MAX);  
      
    if exists(  
     select * from INFORMATION_SCHEMA.COLUMNS  
     where TABLE_SCHEMA = 'dbo' AND TABLE_NAME = 'Customer' and COLUMN_NAME = 'Zip'  
     )  
     begin  
     SET @sqlcmd = '  
     SELECT CustomerId, CustomerName, Addr1, City, State, Zip  
     FROM dbo.Customer';  
     end  
          
     else  
          
     begin  
     SET @sqlcmd = '  
     SELECT CustomerId, CustomerName, Addr1, City, State, ZipCode  
     FROM dbo.Customer';  
     end  
     EXEC(@sqlcmd);  
    

    However, you would be better off creating a VIEW which renames the column correctly.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.