Solved

SQL - If Field Exists Or not

Posted on 2013-11-06
5
213 Views
Last Modified: 2013-11-21
I have 2 databases, one is a newer version with additional fields and the other doesn't have these new fields.

I just want to create one query for use with both database since it's same report.  I do not want to have 2 sets of query to toggle between one or the other.  Anyway I can check if field exists and if it does, use that field else put a "Null"?

Example:

New Database table (Employee)

SELECT ID, NAME, DEPT, PHONE, EMAIL, MOBILE FROM EMPLOYEE

Older Database table (Employee)

SELECT ID, NAME, DEPT, PHONE FROM EMPLOYEE

I am missing Email and Mobile from the older database.  When I query the older database, it would throw me an error stating that Email and Mobile doesn't exists.  How could I set it with one select statement to use a condition to check if field exists or not?

I found a few examples online to toggle, but would like to not have to toggle.  Can something like this be done?

SELECT ID, NAME, DEPT, PHONE, CASE WHEN COL_LENGTH('EMPLOYEE', 'EMAIL') IS NOT NULL THEN 'EMAIL' ELSE 'NULL' END AS EMAIL,  CASE WHEN COL_LENGTH('EMPLOYEE', 'MOBILE') IS NOT NULL THEN 'MOBILE' ELSE 'NULL' END AS MOBILE
FROM EMPLOYEE
0
Comment
Question by:holemania
5 Comments
 

Expert Comment

by:sergiomn
Comment Utility
The simplest solution would be just to add those 2 missing columns to the old database.

If you're using java, you could also use getMetaData() to get the column count, names and more, so you could have a function to retrieve results from any table, even if you don't know what columns are in it.

Also, if you're executing this query inside any sort of script, you could use a parameter so you can know if execute one or another query.

There are many other aproaches to this, it depends on your needs.
0
 

Author Comment

by:holemania
Comment Utility
Adding the 2 missing columns is a no no for us since when upgrading, it might create issue since the upgrade to the database will attempt to add those 2 fields and may fail.

Probably might have to create 2 queries and just toggle between the 2 depending on which database to connect to.
0
 
LVL 32

Expert Comment

by:bhess1
Comment Utility
Much depends on how you are running this.  However, it is fairly straightforward to get the number of columns for a table.  Here is one example:

DECLARE @tableName sysname
SET @tableName = 'MyTableName'

DECLARE @cols int

SELECT @cols = COUNT(*)
FROM sys.columns AS c
INNER JOIN sys.tables AS t
	ON c.object_id = t.object_id
WHERE t.object_id = OBJECT_ID(@tablename)

Open in new window

If this is a stored procedure, you can check the table, find the number of columns, and see if your version works when the COL_LENGTH check is replaced by  CASE @cols WHEN 6 THEN EMAIL END as email (for example)
0
 
LVL 40

Accepted Solution

by:
Sharath earned 500 total points
Comment Utility
You can try dynamical query like this.
declare @sql nvarchar(200)
select @sql = 'SELECT ID, NAME, DEPT, PHONE' +
              case db_name() when 'OldDB' then ''
                             when 'NewDB' then 'EMAIL, MOBILE' end +
              ' FROM EMPLOYEE'
exec @sql
 

Open in new window

0
 

Author Closing Comment

by:holemania
Comment Utility
Thanks that helped.
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now