Assigning a variable conditionally inside a SELECT statement on SQL SERVER -
i attempting write select
statement on sql server displays items based on condition, provide in subsequent where
clause.
here simplified version of attempting write.
declare @tmpvar varchar(5) .... select @tmpvar, .... some_table .... , @tmpvar = case when (some condition here) 'yes' else 'no' end
the code above executes successfully, when check value of @tmpvar
, remains unassigned.
i'd know if there mistake in syntax using. if so, correct way of assigning variable inside select
statement using given condition? prior apologies if question redundant.
you can't assign in where
clause. can in select
clause:
declare @tmpvar varchar(5) .... select @tmpvar = case when (some condition here) 'yes' else 'no' end, .... some_table ....
but if you're not attempting data retrieval @ same time. also, it's pretty poorly defined row(s) influence result if there's more 1 row in result set, unless have order by
clause:
if select statement returns more 1 value, variable assigned last value returned.
as may have noticed, sql uses =
assignment, comparison , introducing aliases. 1 determined position =
appears in. in where
clause, =
can comparison.
Comments
Post a Comment