Click here to Skip to main content
15,889,424 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi Experts,


i want to use CASE clause within where clause of update statement

SQL
IF not exist (select listid from list where Listname=@listname and ListType=@ListType and AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))

BEGIN

--some other statement

END



List table structure:list(listid,listname,listname_en,listname_DE,...)
I want use CASE clause something like this...Based upon @languagecode parameter value i want to check @listname value with different columns(listname_en,listname_de...) of list table


SQL
IF not exist (select listid from list where  
case when @languagecode='en' then ListName_en =@listname 
case when @languagecode='DE' then ListName_DE =@listname and ListType=@ListType and AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))

BEGIN

--some other statement

END




the above statement is not working please give me the solution .

I will appreciate you.
Posted
Updated 23-Dec-13 6:53am
v2
Comments
Mike Meinz 23-Dec-13 12:49pm    
You don't need a CASE statement.


IF NOT EXIST (SELECT listid FROM list WHERE
(
(@languagecode='en' AND ListName_en = @listname)
OR
(@languagecode='DE' AND ListName_DE = @listname)
)
AND ListType = @ListType AND AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))


BEGIN

--some other statement

END
argeraju 24-Dec-13 10:58am    
yes it works.but how can i accept your solution.provide the solution option.

Try something like this:

IF NOT EXIST (SELECT listid FROM list WHERE @languagecode='en' AND ListName_en =@listname AND ListType=@ListType AND AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))
 
BEGIN
 
--some other statement

END


You do not need to use a case statement in this situation. You have multiple values you are checking against and a case statement will give you one result for each case statement so is not appropriate for your query.

[UPDATE]

Try this, it will get some values you can check before the query runs.

DECLARE @listNameChoice varchar(25)

SET @listNameChoice = (Case WHEN @languagecode = 'en' THEN 'ListName_en' ELSE 'ListName_DE' END)

IF NOT EXIST (SELECT listid FROM list WHERE @listNameChoice = @listname AND ListType=@ListType AND AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))
 
BEGIN
 
--some other statement

END
 
Share this answer
 
v3
Comments
argeraju 23-Dec-13 12:59pm    
Hi Richard,
I appreciated. Meanwhile improving my question you answered .sorry for inconvenience.
Now i updated question completely.you are right but i want to check something like that .Can you help me out with that.I hope so i will expect the answer from you.
argeraju 23-Dec-13 12:59pm    
Is there any other way to check like that..
Richard C Bishop 23-Dec-13 13:13pm    
See my updated solution. I think that is what you might be looking for.
argeraju 23-Dec-13 13:26pm    
DECLARE @langCode varchar(25)

SET @langCode = (Case WHEN @languagecode = 'en' THEN ListName_en ELSE ListName_DE END)

IF NOT EXIST (SELECT listid FROM list WHERE @listname= @langCode AND ListType=@ListType AND AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))

BEGIN

--some other statement

END
argeraju 23-Dec-13 13:29pm    
Thanks Richard..i appreciated..It will work..i will this code ..

I also got some solution for my question...i added the solution but it is not working..can you check my solution once.
SQL
IF not exist (select listid from list where  
case @languagecode when 'en' then ListName_en
case @languagecode  when 'DE' then ListName_DE End =@listname and ListType=@ListType and AccessType=@AccessType AND (CreatedBy = @EmplpyeeID OR @CreatedBy = '0' OR @AccessType = 'Global'))
 
BEGIN
 
--some other statement

END
 
Share this answer
 
Comments
Mike Meinz 23-Dec-13 14:10pm    
See my comment above. It contains sample SQL that solves your problem.
Nelek 23-Dec-13 19:54pm    
Please don't post solutions to chat with people asking or answering. The messages are not always sorted by date, so it can be a bit difficult to follow them correctly.
The best option is to use the "Have a question or comment?" (or the tiny "reply" on another comment). Another advantage is, that the person you write to will get a notification, otherwise it could be that he/she doesn't see your additional message.
argeraju 24-Dec-13 10:50am    
Hi Mike, From next time onwards i wont do that mistake..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900