Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4540816
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T15:08:19+00:00 2026-05-21T15:08:19+00:00

IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = ‘SiteInformation’ AND COLUMN_NAME = ‘Number_Injectors’)

  • 0
IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
        WHERE TABLE_NAME = 'SiteInformation' 
        AND  COLUMN_NAME = 'Number_Injectors')
    BEGIN
        SELECT [Number_Injectors] as Injectors
        FROM [BLEND].[dbo].[SiteInformation]
    END

ELSE
    BEGIN
        IF EXISTS( SELECT * FROM INFORMATION_SCHEMA.COLUMNS 
                WHERE TABLE_NAME = 'SiteInformation' 
                AND  COLUMN_NAME = 'Injectors')
        BEGIN
        SELECT [Injectors] as Injectors
        FROM [BLEND].[dbo].[SiteInformation]
        END
    END

The basic premise is that I have Visual Studio code that references a table called SiteInfomation from different servers to collect information concerning a certain piece of machinery. Thing is that I found out that a couple of those servers have have different column names (Injectors and Number_Injectors). The data variable Injectors from Visual Studio is looking for columns named Injectors to collect information. When it comes to a SiteInformation table that has a column named Number_Injectors instead of Injectors, the value that is returned is NULL. Number_Injectors and Injectors are both one and the same except in name.

I checked StackOverflow and found a topic on how to check if a column exists and created the code mentioned above. The if exist portion of the code works fine but I get an error if I use this query on a server that doesn’t contain one of the two column names.

Example: The SiteInformation table from Server A has the column Injectors. It would give me an error because of this code:

SELECT [Number_Injectors] as Injectors
FROM [BLEND].[dbo].[SiteInformation]

Likewise the SiteInformation table from Server B has the column Number_Injectors. It would give me an error because of this code:

SELECT [Number_Injectors] as Injectors
FROM [BLEND].[dbo].[SiteInformation]

I am a bit lost on how to correct it. It seems like both Select queries are ran at the same time despite the if-exist part. Any suggestions will be helpful.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T15:08:20+00:00Added an answer on May 21, 2026 at 3:08 pm

    The SQL compiler is going to try to validate both select statements, so you’d need to “hide” them from the compiler by embedding them in an EXEC like this:

    EXEC ('SELECT [Number_Injectors] as Injectors
           FROM [BLEND].[dbo].[SiteInformation]')
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following query used to work: select * from information_schema.columns where column_name like ...
I was suggested to do this SELECT table_schema, table_name FROM information_schema.tables WHERE table_schema =
I used to write my EXISTS checks like this: IF EXISTS (SELECT * FROM
I normally use the following code in SQL Server: IF EXISTS (SELECT * FROM
Trying to create Database as follows: USE Master GO IF NOT EXISTS(SELECT [Name] FROM
SELECT A, B, C FROM TUser UNION IF EXISTS(SELECT dataUserId FROM TUserData WHERE DataId
I learned to use "exists" instead of "in". BAD select * from table where
I'm using the following query to gather information about a table's columns: SELECT COLUMN_NAME,
I am using this condition as part of my where clause: exists (select *
What is the linq equivalent of the following statement ? IF NOT EXISTS(SELECT UserName

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.