SQL要基于参数包含或排除

时间:2016-02-09 15:11:09

标签: sql-server

如何编写SQL语句以在给定范围之间返回客户信息或排除给定范围?

这将在SQL Server 2008中的存储过程中。

declare @CustomerTypeBegin int;
declare @CustomerTypeEnd int;
declare @CustomerTypeInclusive bit;

set @CustomerTypeBegin=2;
set @CustomerTypeEnd=4;
set @CustomerTypeInclusive=1;

CREATE TABLE dbo.TmpCustType (
    Id int NOT NULL,
    Code varchar(10),
    Name varchar(100)
);

INSERT INTO dbo.TmpCustType (Id, Code, Name)
SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
FROM [CustomerTypeLookup]
WHERE
    CASE @CustomerTypeInclusive
    WHEN 1 THEN CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd
    ELSE CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd
    END;

SQL Server Management Studio仅显示“语法不正确。”

6 个答案:

答案 0 :(得分:2)

CASE不是用于控制流量,而是用于返回值。

将条件与逻辑结合起来:

WHERE
    (@CustomerTypeInclusive = 1 AND CustomerTypeId BETWEEN CustomerTypeBegin AND @CustomerTypeEnd)
    OR
    (@CustomerTypeInclusive = 2 AND CustomerTypeId NOT BETWEEN CustomerTypeBegin AND @CustomerTypeEnd)
    ....

答案 1 :(得分:1)

WHERE 
 (@CustomerTypeInclusive = 1 AND CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd)
OR 
 (@CustomerTypeInclusive = 0 AND CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd)

答案 2 :(得分:1)

CASE表达式而不是语句,因此不能用于控制执行顺序。

您可以使用标准SQL谓词来实现所需的结果:

INSERT INTO dbo.TmpCustType (Id, Code, Name)
SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
FROM [CustomerTypeLookup]
WHERE (@CustomerTypeInclusive = 1 AND 
       CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd)
      OR
      (@CustomerTypeInclusive <> 1 AND 
       CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd)

答案 3 :(得分:1)

你可以写2个插入语句:

IF ( @CustomerTypeInclusive = 1)
    INSERT INTO dbo.TmpCustType (Id, Code, Name)
    SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
    FROM [CustomerTypeLookup]
    WHERE CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd
ELSE
    INSERT INTO dbo.TmpCustType (Id, Code, Name)
    SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
    FROM [CustomerTypeLookup]
    WHERE CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd

或者您重写了where子句:

INSERT INTO dbo.TmpCustType (Id, Code, Name)
SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
FROM [CustomerTypeLookup]
WHERE CASE WHEN CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd THEN 1 END = @CustomerTypeInclusive 

答案 4 :(得分:1)

可以使用case表达式编写您想要的内容。关键是要认识到case表达式总是计算为

-- option #1
WHERE
    CASE
        WHEN @CustomerTypeInclusive = 1
             AND CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd THEN 1
        ELSE @CustomerTypeInclusive = 0
             AND CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd THEN 1 
    END = 1;

-- option #2
WHERE
    CASE @CustomerTypeInclusive
        WHEN 1 THEN
            CASE
                WHEN CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd
                THEN 1
            END
        WHEN 0 THEN
            CASE
                WHEN CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd
                THEN 1
            END
    END = 1;

答案 5 :(得分:0)

嗯,有一个答案,但是在我感谢他之前他删除了它。我想,Alex是他的UserID:

DECLARE @TmpCustType AS TABLE (
    Id int NOT NULL,
    Code varchar(10),
    Name varchar(100)
);

INSERT INTO @TmpCustType (Id, Code, Name)
SELECT CustomerTypeId, CustomerTypeCode, CustomerTypeName
FROM [CustomerTypeLookup]
WHERE
    (@CustomerTypeInclusive=1 AND CustomerTypeId BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd) OR
    (@CustomerTypeInclusive=0 AND CustomerTypeId NOT BETWEEN @CustomerTypeBegin AND @CustomerTypeEnd);

他告诉我要删除CASE。