如何在一个SQL查询中使用两个运算符

时间:2018-01-12 20:22:40

标签: sql-server

我的SQL语句中有问题。我只想在我的一个SQL语句中使用运算符之间的多个。我的代码如下所示 - 代码中有什么问题?请告诉我

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[GetProductByCustomization]
    @Size1 NVARCHAR(MAX),
    @Size2 NVARCHAR(MAX),
    @Size3 NVARCHAR(MAX),
    @Size4 NVARCHAR(MAX),
    @Size5 NVARCHAR(MAX),
    @Size6 NVARCHAR(MAX),
    @Size7 NVARCHAR(MAX),
    @Size8 NVARCHAR(MAX),
    @Size9 NVARCHAR(MAX),
    @Size10 NVARCHAR(MAX),
    @Size11 NVARCHAR(MAX),
    @Size12 NVARCHAR(MAX),
    @Size13 NVARCHAR(MAX),
    @Size14 NVARCHAR(MAX),
    @Size15 NVARCHAR(MAX),
    @Size16 NVARCHAR(MAX),
    @Size17 NVARCHAR(MAX),
    @Size18 NVARCHAR(MAX),
    @Size19 NVARCHAR(MAX),
    @Size20 NVARCHAR(MAX),
    @CategoryId NVARCHAR(255),
    @Price1 decimal(18,0),
    @Price2 decimal(18,0),
    @Color1 NVARCHAR(MAX),
    @Color2 NVARCHAR(MAX),
    @Color3 NVARCHAR(MAX),
    @Color4 NVARCHAR(MAX),
    @Color5 NVARCHAR(MAX),
    @Color6 NVARCHAR(MAX),
    @Color7 NVARCHAR(MAX),
    @Color8 NVARCHAR(MAX),
    @Color9 NVARCHAR(MAX),
    @Color10 NVARCHAR(MAX),
    @Color11 NVARCHAR(MAX),
    @Color12 NVARCHAR(MAX),
    @Color13 NVARCHAR(MAX),
    @Color14 NVARCHAR(MAX),
    @Color15 NVARCHAR(MAX),
    @Color16 NVARCHAR(MAX),
    @Color17 NVARCHAR(MAX),
    @Color18 NVARCHAR(MAX),
    @Color19 NVARCHAR(MAX),
    @Color20 NVARCHAR(MAX),
    @DiscountPercentage1 tinyint,
    @DiscountPercentage2 tinyint
AS
BEGIN
    SELECT * 
    FROM tblProduct 
    WHERE (PrdPrice BETWEEN @Price1 AND @Price2) 
      AND (PrdOffPercentage BETWEEN @DiscountPercentage1 AND @DiscountPercentage2)
      AND PrdColor IN (@Color1, @Color2, @Color3, @Color4, @Color5, @Color6, @Color7, @Color8, @Color9, @Color10, @Color11, @Color12, @Color13, @Color14, @Color15, @Color16, @Color17, @Color18, @Color19, @Color20)
      AND PrdSize IN (@Size1, @Size2, @Size3, @Size4, @Size5, @Size6, @Size7, @Size8, @Size9, @Size10, @Size11, @Size12, @Size13, @Size14, @Size15, @Size16, @Size17, @Size18, @Size19, @Size20) 
      AND PrdCategoryId = @CategoryId
END

1 个答案:

答案 0 :(得分:0)

假设所有参数值都正确。尝试将您的选择查询放入sp_executesql

    USE [MakaAnOrderDB]
    GO
    /****** Object:  StoredProcedure [dbo].[GetProductByCustomization]    Script Date: 1/13/2018 1:51:07 AM ******/
    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    ALTER PROCEDURE [dbo].[GetProductByCustomization]
        @Size1 NVARCHAR(MAX) ,
        @Size2 NVARCHAR(MAX) ,
        @Size3 NVARCHAR(MAX) ,
        @Size4 NVARCHAR(MAX) ,
        @Size5 NVARCHAR(MAX) ,
        @Size6 NVARCHAR(MAX) ,
        @Size7 NVARCHAR(MAX) ,
        @Size8 NVARCHAR(MAX) ,
        @Size9 NVARCHAR(MAX) ,
        @Size10 NVARCHAR(MAX) ,
        @Size11 NVARCHAR(MAX) ,
        @Size12 NVARCHAR(MAX) ,
        @Size13 NVARCHAR(MAX) ,
        @Size14 NVARCHAR(MAX) ,
        @Size15 NVARCHAR(MAX) ,
        @Size16 NVARCHAR(MAX) ,
        @Size17 NVARCHAR(MAX) ,
        @Size18 NVARCHAR(MAX) ,
        @Size19 NVARCHAR(MAX) ,
        @Size20 NVARCHAR(MAX) ,
        @CategoryId NVARCHAR(255) ,
        @Price1 DECIMAL(18, 0) ,
        @Price2 DECIMAL(18, 0) ,
        @Color1 NVARCHAR(MAX) ,
        @Color2 NVARCHAR(MAX) ,
        @Color3 NVARCHAR(MAX) ,
        @Color4 NVARCHAR(MAX) ,
        @Color5 NVARCHAR(MAX) ,
        @Color6 NVARCHAR(MAX) ,
        @Color7 NVARCHAR(MAX) ,
        @Color8 NVARCHAR(MAX) ,
        @Color9 NVARCHAR(MAX) ,
        @Color10 NVARCHAR(MAX) ,
        @Color11 NVARCHAR(MAX) ,
        @Color12 NVARCHAR(MAX) ,
        @Color13 NVARCHAR(MAX) ,
        @Color14 NVARCHAR(MAX) ,
        @Color15 NVARCHAR(MAX) ,
        @Color16 NVARCHAR(MAX) ,
        @Color17 NVARCHAR(MAX) ,
        @Color18 NVARCHAR(MAX) ,
        @Color19 NVARCHAR(MAX) ,
        @Color20 NVARCHAR(MAX) ,
        @DiscountPercentage1 TINYINT ,
        @DiscountPercentage2 TINYINT
    AS
        BEGIN

            DECLARE @SQL AS NVARCHAR(MAX) = N'
        SELECT  *
        FROM    tblProduct
        WHERE   ( PrdPrice BETWEEN @Price1 AND @Price2 )
                AND 
                ( PrdOffPercentage BETWEEN @DiscountPercentage1 AND @DiscountPercentage2 )

                AND PrdColor IN ( @Color1, @Color2, @Color3, @Color4, @Color5,
                                  @Color6, @Color7, @Color8, @Color9, @Color10,
                                  @Color11, @Color12, @Color13, @Color14,
                                  @Color15, @Color16, @Color17, @Color18,
                                  @Color19, @Color20 )
                AND PrdSize IN ( @Size1, @Size2, @Size3, @Size4, @Size5,
                                 @Size6, @Size7, @Size8, @Size9, @Size10,
                                 @Size11, @Size12, @Size13, @Size14, @Size15,
                                 @Size16, @Size17, @Size18, @Size19, @Size20 )
                AND PrdCategoryId = @CategoryId'


            EXEC sp_executesql @SQL, N'@Size1 NVARCHAR(MAX) ,
                        @Size2 NVARCHAR(MAX) ,
                        @Size3 NVARCHAR(MAX) ,
                        @Size4 NVARCHAR(MAX) ,
                        @Size5 NVARCHAR(MAX) ,
                        @Size6 NVARCHAR(MAX) ,
                        @Size7 NVARCHAR(MAX) ,
                        @Size8 NVARCHAR(MAX) ,
                        @Size9 NVARCHAR(MAX) ,
                        @Size10 NVARCHAR(MAX) ,
                        @Size11 NVARCHAR(MAX) ,
                        @Size12 NVARCHAR(MAX) ,
                        @Size13 NVARCHAR(MAX) ,
                        @Size14 NVARCHAR(MAX) ,
                        @Size15 NVARCHAR(MAX) ,
                        @Size16 NVARCHAR(MAX) ,
                        @Size17 NVARCHAR(MAX) ,
                        @Size18 NVARCHAR(MAX) ,
                        @Size19 NVARCHAR(MAX) ,
                        @Size20 NVARCHAR(MAX) ,
                        @Color1 NVARCHAR(MAX) ,
                        @Color2 NVARCHAR(MAX) ,
                        @Color3 NVARCHAR(MAX) ,
                        @Color4 NVARCHAR(MAX) ,
                        @Color5 NVARCHAR(MAX) ,
                        @Color6 NVARCHAR(MAX) ,
                        @Color7 NVARCHAR(MAX) ,
                        @Color8 NVARCHAR(MAX) ,
                        @Color9 NVARCHAR(MAX) ,
                        @Color10 NVARCHAR(MAX) ,
                        @Color11 NVARCHAR(MAX) ,
                        @Color12 NVARCHAR(MAX) ,
                        @Color13 NVARCHAR(MAX) ,
                        @Color14 NVARCHAR(MAX) ,
                        @Color15 NVARCHAR(MAX) ,
                        @Color16 NVARCHAR(MAX) ,
                        @Color17 NVARCHAR(MAX) ,
                        @Color18 NVARCHAR(MAX) ,
                        @Color19 NVARCHAR(MAX) ,
                        @Color20 NVARCHAR(MAX) ,
                        @CategoryId NVARCHAR(255) ,
                        @Price1 DECIMAL(18, 0) ,
                        @Price2 DECIMAL(18, 0) ,
                        @DiscountPercentage1 TINYINT ,
                        @DiscountPercentage2 TINYINT', @Size1 = @Size1,
                @Size2 = @Size2, @Size3 = @Size3, @Size4 = @Size4,
                @Size5 = @Size5, @Size6 = @Size6, @Size7 = @Size7,
                @Size8 = @Size8, @Size9 = @Size9, @Color1 = @Color1,
                @Color2 = @Color2, @Color3 = @Color3, @Color4 = @Color4,
                @Color5 = @Color5, @Color6 = @Color6, @Color7 = @Color7,
                @Color8 = @Color8, @Color9 = @Color9, @Color10 = @Color10,
                @Color11 = @Color11, @Color12 = @Color12,
                @Color13 = @Color13, @Color14 = @Color14,
                @Color15 = @Color15, @Color16 = @Color16,
                @Color17 = @Color17, @Color18 = @Color18,
                @Color19 = @Color19, @Color20 = @Color20,
                @CategoryId = @CategoryId, @Price1 = @Price1,
                @Price2 = @Price2,
                @DiscountPercentage1 = @DiscountPercentage1,
                @DiscountPercentage2 = @DiscountPercentage2

        END