简体   繁体   English

检查查询中传递的参数中是否存在某个值(SQL SERVER)

[英]Check if a certain value exist in the passed parameter in query (SQL SERVER)

So I need to determine if a certain value exist in the passed parameter to perform a certain condition. 因此,我需要确定传递的参数中是否存在某个值才能执行某种条件。 Here's the query below: 这是下面的查询:

DECLARE @PCStatusas varchar(50);

SELECT * FROM 
Employee emp 
WHERE (emp.PCStatus IN (@PCStatus) OR @PCStatus = '') 
OR 

('ON-HOLD' IN (@PCStatus) ('ON-HOLD'IN(@PCStatus)

AND CAST(emp.EffectiveDate AS DATE) <= CAST(GETDATE() AS DATE)))

so what i need is to check if the parameter sent contain the value 'ON-HOLD' to perform the EffectiveDate condition being equal or less than today. 因此,我需要检查发送的参数是否包含值“ ON-HOLD”,以执行等于或小于今天的EffectiveDate条件。

You can do like (@PCStatus like '%ON-HOLD%') 您可以这样做(@PCStatus like '%ON-HOLD%')

DECLARE @PCStatusas varchar(50);

SELECT * FROM 
Employee emp 
WHERE (emp.PCStatus IN (@PCStatus) OR @PCStatus = '') 
OR 
(@PCStatus like '%ON-HOLD%')

AND CAST(emp.EffectiveDate AS DATE) <= CAST(GETDATE() AS DATE)))

If you have mix AND and OR conditions, it is best to use parenthesis ( ) on it to clearly define the intended conditions. 如果您具有混合ANDOR条件,则最好在其上使用括号( )清楚地定义预期条件。

Also avoid applying function on the column as it will prohibit the use of index on the column. 还要避免在列上应用函数,因为这将禁止在列上使用索引。 If the EffectiveDate does not contain time, you may simply use equal emp.EffectiveDate = CAST(GETDATE() AS DATE) else you should use >= today and < tomorrow 如果EffectiveDate不包含时间,则可以简单地使用等于emp.EffectiveDate = CAST(GETDATE() AS DATE)否则应使用>= today< tomorrow

DECLARE @PCStatus as varchar(50);

SELECT * 
FROM   Employee emp 
WHERE  
(
       emp.PCStatus       = @PCStatus
)
OR     
(
       @PCStatus          = ''
)
OR 
(
       @PCStatus          LIKE '%ON-HOLD%'
AND    emp.EffectiveDate >= CAST(GETDATE() AS DATE)
AND    emp.EffectiveDate <  CAST(DATEADD(DAY, 1, GETDATE()) AS DATE)
)

EDIT : change to LIKE '%ON-HOLD%' 编辑:更改为LIKE '%ON-HOLD%'

EDIT 2 : @PCStatus is CSV here, the query is using STRING_SPLIT() if you are using earlier version of SQL Server, use the DelimitedSplit8K that i posted in the comments 编辑2:@PCStatus在此处为CSV,如果您使用的是SQL Server的早期版本,则查询使用的是STRING_SPLIT() ,请使用我在评论中发布的DelimitedSplit8K

DECLARE @PCStatus as varchar(50) = 'ACTIVE,ON-HOLD';

SELECT * 
FROM   Employee emp 
WHERE  
(
    @PCStatus          = ''
OR  EXISTS
    (
        select  *
        from    STRING_SPLIT(@PCStatus, ',') x
        where   x.value     = emp.PCStatus
    )
)
AND 
(
    emp.PCStatus      <> 'ON-HOLD'
OR  emp.EffectiveDate <= CAST(GETDATE() AS DATE)
)

So with the help of my colleague, we created a function that splits the csv into a temporary table and there I can simply check if a certain value is in the temporary table and return a Boolean for it. 因此,在同事的帮助下,我们创建了一个将csv拆分为临时表的函数,在那里我可以简单地检查临时表中是否有某个值并为其返回布尔值。 Thanks for the help guys. 感谢您的帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 SQL Server-传入的参数数组中的值是? - SQL Server - is the value in the passed in parameter array? SQL需要从传递给查询的日期参数中减去某些时间 - SQL need to subtract certain time from date parameter passed into query SQL查询中的条件语句检查作为参数传递的多个值(列表) - Conditional Statements in SQL query to check multiple values (list) passed as parameter ADO.NET,SQL Server,C#:在数据库中执行插入之前如何检查某个参数是否不存在 - ADO.NET, SQL Server, C#: how to check if a certain parameter does not exist before performing an Insert in a database SQL Server查询,传递给LEFT或SUBSTRING函数的无效长度参数 - SQL Server query, Invalid length parameter passed to the LEFT or SUBSTRING function 如何检查参数不是空值的sql server - How to check parameter is not null value sql server SQL 根据传递的参数查询 - SQL query according to parameter passed 多值字段SQL查询,检查值是否存在 - Multi value field sql query, check if value exist 如果列中存在值,则SQL Server查询返回1,否则返回0 - SQL Server query to return 1 if value exist in a column else return 0 SQL Server存储过程 - 读取现有的十进制值,并从传入的参数中添加另一个十进制值 - SQL Server Stored Procedures - Read an existing decimal value and add another decimal value from a passed in parameter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM