sql server 2012 - Where to declare a SET in SQL? -


i have been attempting figure out can set variable in sqlquery. have 2 different select statements have different clause depending on answer if statement, have run query make set variable once, , since running code find particular table, i'd in first select statement.

the variable trying set called regtype

select s.subid, c.ckey comp c join jdata j on j.pkey = c.primary join sub s on  j.fkey = s.pkey ( //this need variable// ) 

i want create variable because have second select uses same clause same conditions on it.

where in can set variable within select statement can use in where?

it makes no sense "set" variable in clause - maybe mean in select?

select      s.subid,      c.ckey,     @regtype = ??? comp c join jdata j on j.pkey = c.primary join sub s on  j.fkey = s.pkey ( ... ) 

note variable not accessible in where clause of query since where clause evaluated before select. can either repeat expression you're using in set or set variable before execute query.


Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -