Showing posts with label requirement. Show all posts
Showing posts with label requirement. Show all posts

Tuesday, March 20, 2012

CASE function result with result expression values (for IN keyword)

I am trying to code a WHERE xxxx IN ('aaa','bbb','ccc') requirement but it the return values for the IN keyword changes according to another column, thus the need for a CASE function.

WHERE
GROUP.GROUP_ID = 2
AND DEPT.DEPT_ID = 'D'
AND WORK_TYPE_ID IN
(
CASE DEPT_ID
WHEN 'D' THEN 'A','B','C' <- ERROR
WHEN 'F' THEN 'C','D
ELSE 'A','B','C','D'
END
)

I kept on getting errors, like

Msg 156, Level 15, State 1, Line 44
Incorrect syntax near the keyword 'WHERE'.

which leads me to assume that the CASE ... WHEN ... THEN statement does not allow mutiple values for result expression. Is there a way to get the SQL above to work or code the same logic in a different manner in just one simple SQL, and not a procedure or T-SQL script.

AND

(

(CASE DEPT_ID = 'D' AND WORK_TYPE_ID IN ('A','B','C'))

OR
(CASE DEPT_ID = 'F' AND WORK_TYPE_ID IN ('A','B','C'))

OR
(CASE DEPT_ID != 'D' AND CASE DEPT_ID != 'F' AND

WORK_TYPE_ID IN ('A','B','C'))

)

Though this could lead to bad performance :-(

Jens K. Suessmeyer

http://www.sqlserver2005.de

|||Hi Jens,

Thanks for the reply . It works, and I agree with you that it could lead to performance degradation.

However, if the number of records involved are filtered and limited to, say under 1000 rows, it would still be managable ? Just a feeling, I know it is hard to quantify the expense of a query by just the row count alone.

Kenny

Saturday, February 25, 2012

Capturing SQL noise word exception...

Hi All,

I have a requirement where I need to find the list of noise words from a set of words in a SP.

PS: Too common words are said to be noise words and SQL maintains list of noise words on its own.

Say I have a sentence like "I am a software engineer. Here I need to get the list of noise words (I, am, a).I have written a logic where I will split the sentence into words and process word by word. I will first take one word and I have a select statement which will throw SQL noise word error exception if it is noise word.

Logic is
1. Take a word from the sentence.
2. Write a select statement like "select * from job where contains (jobdescription,' extracted word')"
3. If the word is noise word then SQL will throw a noise word exception.
4. I try to capture this error and based on that I have some logic.
5. If noise error thrown (I am using @.@.error)
do this;
do this;
else
do this;
do this;
6. Now my problem is, when the SQL throws noise exception, the execution of the SP stops immediately and the rest of the logic is not executed.
7. But some how I need to capture the exception and continue with the program flow.

I have different logic where I can achieve my requirements. (Instead of capturing SQL exception, maintain the noise words in a table and check with the table). but my question is there any way where I can capture the SQL exception and continue the program flow?

Please reply to my mail id.

TIA,
Varada.Can we do this 1 problem at a time?

Are you looking for a word in a string?

Look up CHARINDEX