[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

SQL - Column doesn't exists error

Posted on 2013-05-30
5
Medium Priority
?
557 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 70

Accepted Solution

by:
Scott Pletcher earned 1600 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 400 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 70

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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

I'm trying, I really am. But I've seen so many wrong approaches involving date(time) boundaries I despair about my inability to explain it. I've seen quite a few recently that define a non-leap year as 364 days, or 366 days and the list goes on. …
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Want to learn how to record your desktop screen without having to use an outside camera. Click on this video and learn how to use the cool google extension called "Screencastify"! Step 1: Open a new google tab Step 2: Go to the left hand upper corn…

650 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