我正在使用这样的IF语句:
ALTER proc spGetUserLevelCode
(
@strLoginID VARCHAR(20) = '', @outdata int output
) AS
if(select level_code
from org_person with(nolock)
where person_code = @strLoginID) = 'CA40'
正如您所看到的,我延伸CA50
,CA60
就像这样添加'CA40' or 'CA50' or 'CA60'
但是有一个错误
如何在IF条件下使用附加条件?
答案 0 :(得分:4)
使用
IN ('CA40','CA50','CA60' )
或者
LIKE 'CA[4-6]0'
不是= 'CA40' or 'CA50' or 'CA60'
答案 1 :(得分:1)
我认为我更喜欢在这种情况下使用存在
ALTER proc spGetUserLevelCode
(
@strLoginID VARCHAR(20) = '', @outdata int output
) AS
if exists(select *
from org_person with(nolock)
where person_code = @strLoginID
AND level_code IN ('CA40','CA50','CA60') )
BEGIN
--DO SOMETHING
END
答案 2 :(得分:0)
您可以使用以下任一方式:
IF EXISTS(SELECT 1
FROM org_person WITH(NOLOCK)
WHERE person_code = @strLoginID
AND level_code IN ('CA40','CA50','CA60') )
BEGIN
-- Other statement goes here
END
OR
IF EXISTS(SELECT 1
FROM org_person WITH(NOLOCK)
WHERE person_code = @strLoginID
AND level_code LIKE 'CA[4-6]0' )
BEGIN
-- Other statement goes here
END