views:

73

answers:

5

I have simplified my problem to the following select statement.

select 
   u.UserId,
   aVariable = cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit), 
from TblUser u
where aVariable = 1

aVariable is not a column of a table, but just a column that gets a value in this select statement.

Is there a way to do the above without getting the Invalid column name aVariable error?

A: 
select  
   u.UserId, 
   1 as 'aVariable'
from TblUser u 
-- where aVariable = 1 - Omit this
nonnb
Are you sure about the single quotes? I'd omit them or use double quotes.
Aaron Digulla
nope it should be `[aVariable]`
Hogan
sorry for the misunderstanding, I updated the question.
Stavros
Single quotes used to be the defacto in SQL 2000 - see herehttp://msdn.microsoft.com/en-us/library/aa259187(SQL.80).aspxBut Y, Hogan is correct, in 2005+ [] are more correct.For aVariable though '' "" [] and no decoration at all will all work. Most examples are now given as undecorated FWIW http://msdn.microsoft.com/en-us/library/ms187731.aspx
nonnb
yes, typically people only use them if there are spaces or a path to the resource (eg db name)
Hogan
+1  A: 

The SELECT must look like so:

select 
   u.UserId,
   1 as aVariable
from TblUser u
Aaron Digulla
sorry for the misunderstanding, I updated the question.
Stavros
Your question is still unclear. What do you want to achieve?
Aaron Digulla
+3  A: 
select q.* from (
select 
   u.UserId,
   cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) as aVar, 
from TblUser u
)q 
where q.aVar = 1
Alex Reitbort
sorry for the misunderstanding, I updated the question.
Stavros
updated answer.
Alex Reitbort
Incorrect syntax near the keyword 'where'.
Stavros
I have edited this so it should work now. SQL was missing a table alias.
Barry
+1  A: 

The statement you picked as correct makes no sense.

select q.* from (
select 
   u.UserId,
   cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) as aVar, 
from TblUser u
)q 
where q.aVar = 1

The statement above says select all users from the tbluser if there is one user who is disabled.

I think you want to see the users in the table who are disabled. If that is so then you want the following select statement:

SELECT userid, disabled as aVar
FROM TblUser
WHERE disabled = 1

Give this a shot.

Prior answered deleted since the question is a tad unclear.

Hogan
sorry for the misunderstanding, I updated the question.
Stavros
aVariable is not a parameter, it's a column name and I want to use it in a where clause.
Stavros
you can't change column values in a select statement -- you can only read values... if you want a new output column use `as` if you want to change the table use update.
Hogan
Invalid column name 'aVariable'. :(
Stavros
does your table have a column named avariable? if it does not have it you can't change it.
Hogan
using the @ sign as a prefix is not only for parameters, it is also used for local variables. if you want a local variable you need to prefix it with an @ and have a declare statement.
Hogan
aVariable is not a column of a table, but just a column that gets a value in this select statement.
Stavros
Forget the logic inside the sql-statement. What I needed was about syntax. It was about how to use a select-statement-column in my "where" clause.
Stavros
@stavros : maybe you mean a select statement that has a case? But there is no magic -- just include the logic in the where statement, it works find. Anything in the select statement can also be in the where clause, there is no trick.
Hogan
+1  A: 

You need to do this:

select 
   u.UserId,
   aVariable = cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit), 
from TblUser u
where cast((case when exists(select * from TblUser u where u.Disabled=1) then 1 else 0 end) as bit) = 1
Barry
I was looking for something better, because the select statement inside the case block, is more complicated and slow :(
Stavros
Maybe you could wrap the case statement in to a function - that may speed it up. It will certainly make it more readable. Are you able to actually post the real problem. Someone maybe be able to provide a different solution all together.
Barry
don't use a function!!! use a join.
Hogan