简体   繁体   English

如何在SQL查询中使用IF-THEN-ELSE逻辑包含可选参数?

[英]How do I include optional parameters using IF-THEN-ELSE logic in a SQL query?

Here is the create statement for a stored procedure: 这是存储过程的create语句:

Create Procedure SearchCreatedAssignments
(@LessonName Varchar(50), @DateFrom date, @DateTo Date, @LocationCode Varchar(10))
As 
BEGIN 

Basically, I want to write a query that searches the database based on the values of the parameters. 基本上,我想编写一个查询,根据参数的值搜索数据库。 For example: 例如:

Select *
from dbo.test

where (LessonName = @LessonName)
AND (StartDate = @DateFrom)
AND (EndDate = @DateTo) 
AND (LocationCode = @LocationCode)

Fairly simple, right? 相当简单吧? However, if any of these parameters are null (or contain an empty string), I would like to omit them from the search, and search by only the parameters that are not null. 但是,如果这些参数中的任何一个为null(或包含空字符串),我想从搜索中省略它们,并仅搜索非null的参数。 I was thinking something like this: 我在想这样的事情:

--if @LocationCode is null OR @LocationCode = '' -> omit @LocationCode from the search

This is obviously pseudo code. 这显然是伪代码。 How can I do this? 我怎样才能做到这一点? Forgive me if this is a simple task; 如果这是一项简单的任务,请原谅我; I am new to SQL. 我是SQL的新手。

Consider the following. 考虑以下。 If a parameter is NULL or empty, the default value will be the field in question 如果参数为NULL或为空,则默认值为相关字段

Select *
from dbo.test
where LessonName   = IsNull(NullIf(@LessonName,''),LessonName) 
 AND  StartDate    = IsNull(NullIf(@DateFrom,''),StartDate) 
 AND  EndDate      = IsNull(NullIf(@DateTo,''),EndDate)
 AND  LocationCode = IsNull(NullIf(@LocationCode,''),LocationCode)

You can either write a dynamic SQL statement and execute it using sp_ExecuteSQL in your procedure, or you can get tricky with the SQL like: 您可以编写动态SQL语句并在过程中使用sp_ExecuteSQL执行它,或者您可能sp_ExecuteSQL SQL感到棘手:

Select *
from dbo.test    
where (LessonName = @LessonName)
AND (StartDate = @DateFrom)
AND (EndDate = @DateTo) 
AND (LocationCode = @LocationCode or @LocationCode IS NULL or @LocationCode = '')

You can use the COALESCE function to do so in this way: 您可以使用COALESCE函数以这种方式执行此操作:

where LessonName = coalesce(@LessonName, LessonName)
  AND StartDate = coalesce(@DateFrom, StartDate)
  AND EndDate = coalesce(@DateTo, EndDate) 
  AND LocationCode = coaleasce(@LocationCode, LocationCode)

Although I'm not sure about the empty strings. 虽然我不确定空字符串。 It will work for null values, in other databases coalesce also handle the empty strings. 它适用于空值,在其他数据库中,coalesce也可以处理空字符串。 If it do not work you can use case in the same manner: 如果它不起作用,您可以以相同的方式使用case

LessonName = case when @LessonName is not null and @LessonName != ''
                  then @LessonName else LessonName end

And just use the same logic for the other parameters. 只需对其他参数使用相同的逻辑。

INHO on this case a good way is using a dynamic query. 在这种情况下INHO一个好方法是使用动态查询。

DECLARE @cmd VARCHAR(MAX);

SET @CMD = 'SELECT * FROM dbo.Text WHERE @Param1 = 'x''; --at least on parameter
IF @PARAM2 IS NOT NULL
BEGIN
    SET @CMD = @CMD + ' AND Param2 = @Param2'
END 
IF @PARAM3 IS NOT NULL
BEGIN
    SET @CMD = @CMD + ' AND Param3 = @Param3'
END 

EXECUTE (@CMD);

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM