Solved

SQL - Column doesn't exists error

Posted on 2013-05-30
5
551 Views
Last Modified: 2013-06-03
Hello experts,

I have 2 databases for the same system, but since it's different versions one has additional columns and the other one doesn't.  I'm using the same query to pull from both databases.

I am getting an error if I use the query with the extra column to pull from the database that doesn't have it.  How could I set it to null if it doesn't have the extra column?

SELECT ID, NAME, DEF_ID FROM VENDOR

The column "DEF_ID" is a new field in the current database.  It is returning an error.
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
  • 3
  • 2
5 Comments
 
LVL 41

Expert Comment

by:ralmada
ID: 39207896
try

if col_length('vendor, 'DEF_ID') is null
      SELECT ID, NAME, DEF_ID FROM VENDOR
else
      SELECT ID, NAME, null as def_id FROM VENDOR
0
 
LVL 69

Accepted Solution

by:
Scott Pletcher earned 400 total points
ID: 39208358
Even without the minor syntax problem, the above code won't work.

Assuming permissions aren't an issue, you can use dynamic SQL:

DECLARE @sql varchar(8000)

SET @sql = 'SELECT ID, NAME'
IF COL_LENGTH('vendor', 'DEF_ID') > 0
    SET @sql = @sql + ', DEF_ID'
SET @sql = @sql + ' FROM DBO.VENDOR'
EXEC(@sql)
0
 
LVL 41

Assisted Solution

by:ralmada
ralmada earned 100 total points
ID: 39208480
I guess you're referring to this syntax issue:

if col_length('vendor, 'DEF_ID') >0
      SELECT ID, NAME, DEF_ID FROM VENDOR
else
      SELECT ID, NAME, null as def_id FROM VENDOR

I too was considering going dynamic but I think this is what the asker is looking for:

set @sql = 	'select id, name,' + 
		case when col_length('vendor', 'def_id') > 0 then 'def_id' else 'null as def_id' end + 
		' from vendor'
exec(@sql)

Open in new window

0
 
LVL 69

Expert Comment

by:Scott Pletcher
ID: 39209163
The missing quote after vendor is the syntax issue.

The code you wrote won't work on a database where the vendor table doesn't contain a "DEF_ID" column, because SQL can't generate a plan for that query -- you have to remember that SQL has to pre-generate a plan for the query before the code runs and thus before the IF gets evaluated at run time.  Try the code and you'll see what I mean.
0
 
LVL 41

Expert Comment

by:ralmada
ID: 39209190
Correct, the IF won't work, but the dynamic SQL alternative with the null in case the column doesn't exists will (http:#a39208480). And I think that's what the asker is looking for.
0

Featured Post

Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

Question has a verified solution.

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

PL/SQL can be a very powerful tool for working directly with database tables. Being able to loop will allow you to perform more complex operations, but can be a little tricky to write correctly. This article will provide examples of basic loops alon…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

691 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