Solved

SQL - Column doesn't exists error

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

Secure Your Active Directory - April 20, 2017

Active Directory plays a critical role in your company’s IT infrastructure and keeping it secure in today’s hacker-infested world is a must.
Microsoft published 300+ pages of guidance, but who has the time, money, and resources to implement? Register now to find an easier way.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
length of the password hash sha1:64000 to set sql field property. 13 83
Query for timesheet application 3 24
What is this datetime? 1 25
SQL Query Syntax Assistance 2 34
'Between' is such a common word we rarely think about it but in SQL it has a very specific definition we should be aware of. While most database vendors will have their own unique phrases to describe it (see references at end) the concept in common …
If you find yourself in this situation “I have used SELECT DISTINCT but I’m getting duplicates” then I'm sorry to say you are using the wrong SQL technique as it only does one thing which is: produces whole rows that are unique. If the results you a…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

685 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