Sql server sql server 2014中where clase中的案例说明

Sql server sql server 2014中where clase中的案例说明,sql-server,tsql,Sql Server,Tsql,我正在创建一个存储过程,我在其中遇到了问题。我想根据条件查询两列。若参数为数值,则查询一列;若参数为非数值,则查询另一列。以下是程序 $ declare @result AS varchar(50) DECLARE @peopleId AS varchar(50) if('232332' NOT LIKE '%[^0-9]%') BEGIN SET @result='Numeric' PRINT @result END ELSE BEGIN set @result='nonNumeric'

我正在创建一个存储过程,我在其中遇到了问题。我想根据条件查询两列。若参数为数值,则查询一列;若参数为非数值,则查询另一列。以下是程序

   $
declare @result AS varchar(50)
DECLARE @peopleId AS varchar(50)
if('232332' NOT LIKE '%[^0-9]%')
BEGIN
SET @result='Numeric'
PRINT @result
END
ELSE
BEGIN
set @result='nonNumeric'
print @result
END


select isnull(class.grade,'') as grade,ISNULL(class.room,'') as room,student.prefix as prefix,student.student_id as student_id,(person.first_name+' '+person.last_name) as name,
person.dob as dob,person.people_id as people_id,quit_on,
case when student.student_status='30' then 
N'พักการเรียน'
when student.student_status='31' then
N'น.ร.ไปเรียนโครงการฯ'
else ''
end
as quit_reason from school_student student
inner join people_person person on student.person_id=person.id
left join school_classroom_students classStudent on classStudent.student_id=student.id
left join school_classroom class on class.id =classStudent.classroom_id
where student.student_status in('30','31') and student.system_status = 'DC' and student.school_id=@schoolId 
AND case 
WHEN
@result='nonNumeric' then-- this should execure
 person.people_id=@peopleId
 else---- this should work
 person.first_name+' '+ person.last_name LIKE '%'+@peopleId+'%'
请帮我解决这个问题,就这样做吧

DECLARE @IsNumeric INT = NULL
DECLARE @IsNotNumeric INT = NULL
DECLARE @peopleId Varchar(50)
SET @peopleId = '123'
IF ISNUMERIC(@peopleId) = 1
BEGIN
    SET @IsNumeric = 1
END
ELSE 
BEGIN
SET @IsNotNumeric =  1
END
在什么情况下只要检查一下

AND (@IsNumeric IS NULL OR  CONVERT(VARCHAR(500),person.people_id)=@peopleId)
AND (@IsNotNumeric IS NULL OR person.first_name+' '+ person.last_name LIKE  '%'+@peopleId+'%')

由于SQL Server不将大小写表达式的结果视为布尔值,因此必须添加额外的比较。方法如下:

WHERE 1 = CASE WHEN x THEN 1 WHEN y THEN 0 ELSE 1 END
AND 1 = case
WHEN
@result='nonNumeric' then case when person.person_id = @peopleId then 1 else 0 end
 else when person.first_name+' '+person.last_name LIKE '%'+@peopleId+'%' then 1 else 0 end
end
导致结果中包含行的条件的计算结果必须为1,而不包含行的条件的计算结果必须为1以外的值(如0)。因此,整个CASE表达式返回0或1,并将其与1进行比较

在您的代码中,它将如下所示:

WHERE 1 = CASE WHEN x THEN 1 WHEN y THEN 0 ELSE 1 END
AND 1 = case
WHEN
@result='nonNumeric' then case when person.person_id = @peopleId then 1 else 0 end
 else when person.first_name+' '+person.last_name LIKE '%'+@peopleId+'%' then 1 else 0 end
end

我添加了结尾。

为什么要使用单独的变量?你可以做:

WHEN (person.people_id = try_convert(int, @peopleId) or
      try_convert(int, @peopleId) is null and 
 person.first_name + ' ' + person.last_name LIKE '%' + @peopleId + '%'
     )
我质疑为什么要传递一个用于字符串和数字比较的值。如果我使用变量,我会:

declare @personid int;
declare @personname varchar(255);

if @peopleid not like '%[^0-9]%'
    set @personname = @peopleid;
else
    set @personid = convert(int, @peopleid);

where (personid = @personid or
       person.first_name + ' ' + person.last_name LIKE '%' + @personname + '%'
      )

代码似乎更容易理解。

因为peopleId可以包含数值和非数值。因为在网站中,我有一个文本框,用户可以在其中输入bot值。因此,我需要用户输入数字值,然后@result='nonNumeric',然后--这应该是执行person.people\id=@peopleId,如果用户输入的不是数字,那么这应该是工作人员。first_name+'+person.last_name像'%+@peopleId+'%应该工作这会导致[like]和[when]出现语法错误cases@VirenderThakur:您可能键入错误或将其包含在WHERE子句的错误部分。如果没有完整的代码,我只能猜测问题到底是什么。我希望一次只执行一个条件。如果用户输入数字,则第一个1应起作用,否则第二个条件应起作用。检查条件!!一次只有一个条件有效,您将得到完美的结果。@MuhammadAzim:这不起作用,因为从语义上讲,WHERE子句中and和OR中的所有条件都被检查。SQL中没有短路。如果
@peopleId
不是数字,无论
@IsNumeric
变量的状态如何,第一个OR子句都将失败。@siride,谢谢您的关注。我忘了计算列值。我更新我的答案。现在这会很好。注意:强制转换的长度必须是输入参数的长度。