Sql server 使用IF的SQL where条件

Sql server 使用IF的SQL where条件,sql-server,stored-procedures,Sql Server,Stored Procedures,我有一个像这样的桌子 |name|level|type|| |a | 1 | a1 | |b | 1 | a2 | |c | 2 | a1 | |d | 1 | a1 | |e | 3 | a3 | |a | 1 | a1 | |a | 1 | a1 | SELECT * FROM LEVEL WHERE ((@LEVEL IS NULL) OR (RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND

我有一个像这样的桌子

|name|level|type||
|a   | 1   | a1  |
|b   | 1   | a2  |
|c   | 2   | a1  |
|d   | 1   | a1  |
|e   | 3   | a3  |
|a   | 1   | a1  |
|a   | 1   | a1  |
SELECT * 
FROM LEVEL
WHERE
((@LEVEL IS NULL) OR 
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '1' AND NAME = @NAME) OR
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '2' AND TYPE = @TYPE) OR
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '3' AND TYPE = @TYPE))
我需要一个基于规则的存储过程

if I send @name = a and @level = 1
- select all from table level where name = @name
if I send @name = b and @level = 2
- select all from table level where type = @type
if I send @name = b and @level = 3
- select all from table level where type = @type
我创建这样的过程

|name|level|type||
|a   | 1   | a1  |
|b   | 1   | a2  |
|c   | 2   | a1  |
|d   | 1   | a1  |
|e   | 3   | a3  |
|a   | 1   | a1  |
|a   | 1   | a1  |
SELECT * 
FROM LEVEL
WHERE
((@LEVEL IS NULL) OR 
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '1' AND NAME = @NAME) OR
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '2' AND TYPE = @TYPE) OR
(RIGHT(LEVEL,1) = RIGHT(@LEVEL,1) AND RIGHT(@LEVEL,1) = '3' AND TYPE = @TYPE))
但是当我发送
@level=1、@name=a和@type=a1
时,仍然返回全部


我认为我的WHERE子句是错误的,但不知道在哪里以及为什么?

使用
联合所有人

select *
from level
where name = @name and 
@level = 1 and @name = 'a'
--above select query return any result if  @name = a and @level = 1

union all

select *
from level
where type = @type and 
@name = 'b' and @level in (2, 3)
--above select query return any result 
--if @name = b and @level = 2 or @name = b and @level = 3

使用
联合所有

select *
from level
where name = @name and 
@level = 1 and @name = 'a'
--above select query return any result if  @name = a and @level = 1

union all

select *
from level
where type = @type and 
@name = 'b' and @level in (2, 3)
--above select query return any result 
--if @name = b and @level = 2 or @name = b and @level = 3

你可以检查一下。该条件的数据不匹配,因此我更改了参数值

Declare @table table (name varchar(5), level int, type varchar(5))

insert into @table values ('a', 1,'a1'),('b', 1,'a2'),('c', 2,'a1'),('d', 1,'a1'),('e', 3,'a3'),('a', 1,'a1'),('a', 1,'a1')

--uncomment for other parameter value
--declare @name varchar(5) = 'a', @level int = 1, @type varchar(5) = 'a1'
declare @name varchar(5) = 'b', @level int = 1, @type varchar(5) = 'a2' -- There is no rows, for @name = 'b' and @level (2, 3) and @type = 'a1'

select * from @table where  
(   name = @name and level = @level and 
    ( 
        (@name = 'a' and @level = 1 )
        or
    ( 
        (@name = 'b' and ( @level in (1, 2 ,3) )) and type = @type ) -- show only row 1 
    )
)
输出将来自第一个注释参数列表

name    level   type
a        1       a1
a        1       a1
a        1       a1
name    level   type
b        1       a2
输出将来自第二个取消注释参数列表

name    level   type
a        1       a1
a        1       a1
a        1       a1
name    level   type
b        1       a2

你可以检查一下。该条件的数据不匹配,因此我更改了参数值

Declare @table table (name varchar(5), level int, type varchar(5))

insert into @table values ('a', 1,'a1'),('b', 1,'a2'),('c', 2,'a1'),('d', 1,'a1'),('e', 3,'a3'),('a', 1,'a1'),('a', 1,'a1')

--uncomment for other parameter value
--declare @name varchar(5) = 'a', @level int = 1, @type varchar(5) = 'a1'
declare @name varchar(5) = 'b', @level int = 1, @type varchar(5) = 'a2' -- There is no rows, for @name = 'b' and @level (2, 3) and @type = 'a1'

select * from @table where  
(   name = @name and level = @level and 
    ( 
        (@name = 'a' and @level = 1 )
        or
    ( 
        (@name = 'b' and ( @level in (1, 2 ,3) )) and type = @type ) -- show only row 1 
    )
)
输出将来自第一个注释参数列表

name    level   type
a        1       a1
a        1       a1
a        1       a1
name    level   type
b        1       a2
输出将来自第二个取消注释参数列表

name    level   type
a        1       a1
a        1       a1
a        1       a1
name    level   type
b        1       a2

一个
查询
或一个
存储过程
?为什么您被赋予了权利?请看我的答案是否对您有帮助。因为从旧的数据级别来看,不总是1而是01:),这将成为存储过程a
查询
存储过程
?为什么您被赋予了正确的权限?请看我的答案是否对您有帮助。因为从旧的数据级别不总是1,而是01:),这将成为存储过程,所以我需要从中选择*并使用级别对其进行过滤?如果confusingEntire
select
查询对这三种情况都有效,那么很抱歉。如果正确的条件匹配,那么只有each
select
query返回结果,所以我需要选择*from(即union all)并用level对其进行过滤?如果confusingEntire
select
查询对这三种情况都有效,那么很抱歉。如果正确的条件匹配,则只有each
select
查询返回结果@和参数名称之间没有间隙。只要删除并尝试,就会得到结果。还有为什么在列中只有一个值时使用RIGHT函数@和参数名之间没有间隙。只要删除并尝试,就会得到结果。还有为什么在列中有一个值时使用RIGHT函数