猿问

sql语句怎么实现动态条件

select * from T where operator = (case when @type = 0 then @o1 else is not null end)

 

主要是想根据@type 用不同的条件

当@type = 0 时 operator = @o 否则 operator is not null

 

请问各位大侠SQL语句怎么写,最好不要用字符串拼接


MMTTMM
浏览 2718回答 3
3回答

MMMHUHU

select * from T where (operator = @o1 and&nbsp;@type = 0 ) or (&nbsp;@type <> 0 and operator&nbsp;is not null )

湖上湖

定义一个临时变量,然后用if else去判断,然后把值放到sql语句里面执行

元芳怎么了

(case when @type=0 then (case when&nbsp;operator=@o then 1 else 0 end) else (case when&nbsp;operator&nbsp;is not null then 1 else 0 end) end )=1
随时随地看视频慕课网APP
我要回答