混合SQL过程,根据传入的参数添加动态where CLU?

混合SQL过程,根据传入的参数添加动态where CLU?,sql,sql-server,stored-procedures,dynamic-sql,Sql,Sql Server,Stored Procedures,Dynamic Sql,我想构建一个存储过程,但是where clu变得很棘手,因为它根据传入的内容而变化 在前端,我有3个复选框可供选择。这些代表公司的规模。 我的数据库中有7个公司规模。 如果选中“小”,则需要设置我的Where类: 例如: 小支票 WHERE Size = Unclasified AND Size = VerySmal AND Size = Small 中号支票 WHERE Size = Medium AND Size = MediumLarge 大支票 WHERE Size = Large

我想构建一个存储过程,但是where clu变得很棘手,因为它根据传入的内容而变化

在前端,我有3个复选框可供选择。这些代表公司的规模。 我的数据库中有7个公司规模。 如果选中“小”,则需要设置我的Where类:

例如:

小支票

WHERE Size = Unclasified AND Size = VerySmal AND Size = Small
中号支票

WHERE Size = Medium AND Size = MediumLarge
大支票

WHERE Size = Large AND Size = Huge
现在,由于这些都是复选框,所以它们称为全部被选中,或者只选中一个或两个

因此,我想先构建我的where clus,然后再添加它:

DECLARE @SizeCatWhereClus varchar(MAX)
SET @SizeCatWhereClus = 'cm.sizecat=' + 'H: UNCLASSIFIED' + 'AND cm.sizecat= ...'



WHERE @SizeCatWhereClus 

这可能吗?如果没有,有什么建议我可以遵循吗

不幸的是,sql Server中的动态sql具有“要么全有,要么全无”的特性。如果要动态生成WHERE子句,必须在变量中生成整个查询,然后执行该变量。出于安全和性能方面的原因,这在Sql Server中通常是一个坏主意

幸运的是,对于您的特定问题,实际上不需要动态sql,因为您的
WHERE
子句可以使用逻辑运算符组合以获得所需的结果

DECLARE @SmallChecked BIT, @MediumChecked BIT, @LargeChecked BIT
-- set the values of the @...Checked variables to the values of your checkboxes
SELECT * FROM [MyTable] 
 WHERE (@SmallChecked = 1 AND Size IN ('Unclassified', 'VerySmall', 'Small'))
    OR (@MediumChecked = 1 AND Size IN ('Medium', 'MediumLarge'))
    OR (@LargeChecked = 1 AND Size IN ('Large', 'Huge'))

不幸的是,SQLServer中的动态sql具有“要么全有,要么全无”的特性。如果要动态生成WHERE子句,必须在变量中生成整个查询,然后执行该变量。出于安全和性能方面的原因,这在Sql Server中通常是一个坏主意

幸运的是,对于您的特定问题,实际上不需要动态sql,因为您的
WHERE
子句可以使用逻辑运算符组合以获得所需的结果

DECLARE @SmallChecked BIT, @MediumChecked BIT, @LargeChecked BIT
-- set the values of the @...Checked variables to the values of your checkboxes
SELECT * FROM [MyTable] 
 WHERE (@SmallChecked = 1 AND Size IN ('Unclassified', 'VerySmall', 'Small'))
    OR (@MediumChecked = 1 AND Size IN ('Medium', 'MediumLarge'))
    OR (@LargeChecked = 1 AND Size IN ('Large', 'Huge'))

不幸的是,SQLServer中的动态sql具有“要么全有,要么全无”的特性。如果要动态生成WHERE子句,必须在变量中生成整个查询,然后执行该变量。出于安全和性能方面的原因,这在Sql Server中通常是一个坏主意

幸运的是,对于您的特定问题,实际上不需要动态sql,因为您的
WHERE
子句可以使用逻辑运算符组合以获得所需的结果

DECLARE @SmallChecked BIT, @MediumChecked BIT, @LargeChecked BIT
-- set the values of the @...Checked variables to the values of your checkboxes
SELECT * FROM [MyTable] 
 WHERE (@SmallChecked = 1 AND Size IN ('Unclassified', 'VerySmall', 'Small'))
    OR (@MediumChecked = 1 AND Size IN ('Medium', 'MediumLarge'))
    OR (@LargeChecked = 1 AND Size IN ('Large', 'Huge'))

不幸的是,SQLServer中的动态sql具有“要么全有,要么全无”的特性。如果要动态生成WHERE子句,必须在变量中生成整个查询,然后执行该变量。出于安全和性能方面的原因,这在Sql Server中通常是一个坏主意

幸运的是,对于您的特定问题,实际上不需要动态sql,因为您的
WHERE
子句可以使用逻辑运算符组合以获得所需的结果

DECLARE @SmallChecked BIT, @MediumChecked BIT, @LargeChecked BIT
-- set the values of the @...Checked variables to the values of your checkboxes
SELECT * FROM [MyTable] 
 WHERE (@SmallChecked = 1 AND Size IN ('Unclassified', 'VerySmall', 'Small'))
    OR (@MediumChecked = 1 AND Size IN ('Medium', 'MediumLarge'))
    OR (@LargeChecked = 1 AND Size IN ('Large', 'Huge'))