?
Solved

SQL - If Field Exists Or not

Posted on 2013-11-06
5
Medium Priority
?
220 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

Percona Live Europe 2017 | Sep 25 - 27, 2017

The Percona Live Open Source Database Conference Europe 2017 is the premier event for the diverse and active European open source database community, as well as businesses that develop and use open source database software.

Question has a verified solution.

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

Occasionally there is a need to clean table columns, especially if you have inherited legacy data. There are obviously many ways to accomplish that, including elaborate UPDATE queries with anywhere from one to numerous REPLACE functions (even within…
This article describes how to use the timestamp of existing data in a database to allow Tableau to calculate the prior work day instead of relying on case statements or if statements to calculate the days of the week.
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses

764 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