Is it possible to set/read variables from within the query?
pseudo code:
SELECT animal_name,
@tallest_animal = (select top 1 height from animal order by height desc) as tallest,
@smallest_animal = (select top 1 height from animal order by height asc) as smallest
FROM animals
WHERE height BETWEEN @smallest_animal AND @tallest_animal
I know the result can be achieved by making the query different, my question's real use is too difficult to explain.
It's Microsoft SQL Server in question. :)
It is not possible for a select statement to assign values to variables and return a resultset in the same SELECT statement - this is a restriction of SQL Server. Wouldn't it be great if it were possible!
Why do you wish to use variables here, if you're requiring a single statement? Would the following not work for you?
If you wish to use the variables later on in a stored procedure, then your only option is to use two select statements: One for the assignment and one for the select:
Note that when using ADO, you can use compound queries in an ADO command. In other words, your command component can include more than one statement, so both of the above solutions would work.