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

  • Home
  • SEARCH
  • 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 521211
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:13:36+00:00 2026-05-13T08:13:36+00:00

Say I am not sure if table t in database d has a column

  • 0

Say I am not sure if table t in database d has a column x or not. Perhaps this is because some of the databases the application uses have been upgraded and some have not, so some have t.x and some don’t.

Now say I just need a simple query to get a row from d.t and I want the value of d.t.x if the column exists and some default if not.

I know how to do it with two queries in which the first looks to see if column x exists and the app adjusts the second query accordingly. But can I move this logic into the SQL and do it in one query? If there were a COLUMN_EXISTS() function then the query would be:

SELECT s, t, IF(COLUMN_EXISTS(x), x, 'NO_COL_x') AS x FROM d.t ...;
  • 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-13T08:13:36+00:00Added an answer on May 13, 2026 at 8:13 am

    Here’s the query you would be interested in:

    IF NOT EXISTS (SELECT * FROM INFORMATION_SCHEMA.COLUMNS
    WHERE TABLE_NAME = ‘tablename’ AND COLUMN_NAME = ‘column’)
    

    You could probably wrap that up in a function that takes the table name and column name as parameters, then use that in your query.

    Still not sure if that would work though, since the query might still error out due to the reference to the non-existant column in your query (even though the IF would always return false if the column didn’t exist)

    Another option would be to use that function to dynamically build your query rather than trying to use it in the query itself. But that would of course get a bit more complicated.

    Ultimately, the best way to do it would be to use a stored procedure rather than putting the query in your code. That way, when the database gets upgraded the sproc would get “upgraded” as well to match the new schema and you wouldn’t have to worry (as much) about whether the code version matched the database version or not.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 318k
  • Answers 318k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer In my company we made test in local via a… May 13, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer You cannot call helpers from controllers. Your best bet is… May 13, 2026 at 11:56 pm
  • Editorial Team
    Editorial Team added an answer Contains is used on text fields that have a 'CONTEXT… May 13, 2026 at 11:56 pm

Related Questions

I am not sure if I am missing something grotesquely obvious or what, but
What I am asking, is what is the best way (i.e. the way that
I am trying to learn Asp.net Mvc so I am trying out this Tutorial.
I am working on a certain application. Its basic structure is as follows: a
I'm starting on a project that pulls data from and writes back to a

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.