Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 Invalid Column name on Select with Case

Author  Topic 

FUNCTOR
Starting Member

5 Posts

Posted - 2012-04-25 : 18:22:27
Hi,

I've got a weird scenario. I have one table, "Station" that has many columns, two of which contain the the same type of information but are not the same values (IdDocumentNumber and IdDocumentNumberDE). I tossed in a case statement to pull the information from a one field or the other depending on a value in a temporary table "p". The column in the temporary table is called "end". I also have a variable called @IdDocument of type int. I get an error on the where clause when i use the alias column names. Invalid column name 'DocNum'. I know it doesn't like the column alias, but don't know how to proceed.

Any help would be appreciated!

Thanks!


if exists(
select p.idpipe,
DocNum = case when p.[end] = 'BE'
then ps.idDocumentNumber
when @end = 'DE'
then ps.idDocumentNumberDE
else null
end
from #p p
join station ps on ps.idstation = p.idpipe
where DocNum <> @IdDocument )

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2012-04-25 : 18:51:49
The where clause gets applied before the select, so the alias doesn't yet exist. You can either duplicate the case in the where clause or use a derived table.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/

Subscribe to my blog
Go to Top of Page

vinu.vijayan
Posting Yak Master

227 Posts

Posted - 2012-04-27 : 06:17:34
People here might be able to provide you a way to get that done. But, you need to help them by providing DDL of the tables, Sample data and the expected result set of what you are trying to do.
Just by looking at the query they won't be able to provide a Solution.
Please help them in helping you.

N 28° 33' 11.93148"
E 77° 14' 33.66384"
Go to Top of Page

vijays3
Constraint Violating Yak Guru

354 Posts

Posted - 2012-05-03 : 09:22:43
quote:
Originally posted by FUNCTOR

Hi,

I've got a weird scenario. I have one table, "Station" that has many columns, two of which contain the the same type of information but are not the same values (IdDocumentNumber and IdDocumentNumberDE). I tossed in a case statement to pull the information from a one field or the other depending on a value in a temporary table "p". The column in the temporary table is called "end". I also have a variable called @IdDocument of type int. I get an error on the where clause when i use the alias column names. Invalid column name 'DocNum'. I know it doesn't like the column alias, but don't know how to proceed.

Any help would be appreciated!

Thanks!


Can you please provide the list of columns of both the tables .


First insert the all the records into temp table

select p.idpipe,
case when p.[end] = 'BE'
then ps.idDocumentNumber
when @end = 'DE'
then ps.idDocumentNumberDE
else null
end As DocNum into #temp
from #p p
join station ps on ps.idstation = p.idpipe

Now after doing that you can check the existence of records in

temp table

if exists( select 1 from temp where DocNum <> @IdDocument )

Try doing this





Go to Top of Page
   

- Advertisement -