Solved

SQL - If Field Exists Or not

Posted on 2013-11-06
5
218 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:bhess1
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 500 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

Suggested Solutions

In database programming, custom sort order seems to be necessary quite often, at least in my experience and time here at EE. Within the realm of custom sorting is the sorting of numbers and text independently (i.e., treating the numbers as number…
If you have heard of RFC822 date formats, they can be quite a challenge in SQL Server. RFC822 is an Internet standard format for email message headers, including all dates within those headers. The RFC822 protocols are available in detail at:   ht…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
Suggested Courses

737 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