Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

SQL - If Field Exists Or not

Posted on 2013-11-06
5
Medium Priority
?
221 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 

Expert Comment

by:sergiomn
ID: 39628803
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
ID: 39630176
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:Brendt Hess
ID: 39634794
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 41

Accepted Solution

by:
Sharath earned 2000 total points
ID: 39638058
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
ID: 39666882
Thanks that helped.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As they say in love and is true in SQL: you can sum some Data some of the time, but you can't always aggregate all Data all the time! Introduction: By the end of this Article it is my intention to bring the meaning and value of the above quote to…
'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 …
This course is ideal for IT System Administrators working with VMware vSphere and its associated products in their company infrastructure. This course teaches you how to install and maintain this virtualization technology to store data, prevent vuln…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

618 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