Solved

SQL - Column doesn't exists error

Posted on 2013-05-30
5
546 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
  • 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

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…
In this article we will get to know that how can we recover deleted data if it happens accidently. We really can recover deleted rows if we know the time when data is deleted by using the transaction log.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 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