3
votes

Why is the following syntax generates the error "An expression of non-boolean type specified in a context where a condition is expected, near '@orderwhere'"

use orders
declare @orderwhere varchar(5000)
set @orderwhere = 'order_status.step = 1'
select order_status.order_id
from order_status
where @orderwhere
2
You want to use dynamic SQL. Then you have to exec() itjuergen d

2 Answers

3
votes

You can't have parts of your query being dynamic. It has to be all or nothing. Then use EXEC() to run your dynamic query

exec('select order_status.order_id
      from order_status
      where ' + @orderwhere)
1
votes

Your where clause is a string. You should use dynamic SQL or something like this:

use orders
declare @orderwhere varchar(5000)
set @orderwhere = '1'
select order_status.order_id
from order_status
where order_status.step = @orderwhere