2

I'm using the IF statement like this:

ALTER proc spGetUserLevelCode 
( 
    @strLoginID VARCHAR(20) = '', @outdata int  output
) AS

if(select level_code 
    from org_person with(nolock) 
    where person_code = @strLoginID) = 'CA40'

as you can see, I extend CA50, CA60 like this adding behind 'CA40' or 'CA50' or 'CA60' but there is an error

How can I use additional condition in IF condition?

1

3 Answers 3

4

Use

IN ('CA40','CA50','CA60' )

Or

 LIKE 'CA[4-6]0'

Not = 'CA40' or 'CA50' or 'CA60'

Sign up to request clarification or add additional context in comments.

2 Comments

+1 for the SQL LIKE pattern - IN is more readable, but the LIKE demonstrates a neat additional way of looking at the problem, plus a useful demo of a feature of SQL Server's string matching :-)
thanks for your reply.even though It's beginners' question :) Finally I made it
1

I think I'd prefer to use an exists in this case

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

Comments

0

You can use either way as below:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.