首页 新闻 会员 周边

sql语句怎么实现动态条件

0
悬赏园豆:60 [已解决问题] 解决于 2012-11-12 14:30

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语句怎么写,最好不要用字符串拼接

Jxj的主页 Jxj | 初学一级 | 园豆:21
提问于:2012-11-09 10:31
< >
分享
最佳答案
1

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

收获园豆:60
Fanr_Zh | 菜鸟二级 |园豆:368 | 2012-11-10 01:41
其他回答(7)
0

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

az235 | 园豆:8483 (大侠五级) | 2012-11-09 10:36
0

你这个是写了一个sql语句啊,并没有拼接sql

chenping2008 | 园豆:9836 (大侠五级) | 2012-11-09 10:39

我写的那句有问题,所以来请教

支持(0) 反对(0) Jxj | 园豆:21 (初学一级) | 2012-11-09 10:46

@Jxj: 有什么问题,直接保存还是,参数没有传入对?

支持(0) 反对(0) chenping2008 | 园豆:9836 (大侠五级) | 2012-11-09 10:46
0

(case when @type=0 then (case when operator=@o then 1 else 0 end) else (case when operator is not null then 1 else 0 end) end )=1

飞来飞去 | 园豆:2057 (老鸟四级) | 2012-11-09 10:52
0

DECLARE int @a
SET @a=case when @type = 0 then @o1 else is not null end

select * from T where operator =@a

田林九村 | 园豆:2367 (老鸟四级) | 2012-11-09 11:05
0

if(@type = 0)
 begin
    select * from T where operator=@o1
end
else
begin
    select * from T where operator is not null
end

分开来写.

/╱× 丫 丶 | 园豆:202 (菜鸟二级) | 2012-11-09 11:18
0

楼上正解

轻狂の书生 | 园豆:1042 (小虾三级) | 2012-11-09 12:52
0
Insus.NET | 园豆:932 (小虾三级) | 2012-11-09 13:44
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册