0

Can something like this can be done?

"SELECT * FROM account WHERE if(@dropdownsearch) = 'ID' then id=@id else name=@name"

because I can't if else outside the query string maybe because of this:

cmd.Parameters.AddRange(queryParams.ToArray());

please help

2 Answers 2

4

use AND/OR logic

SELECT * 
FROM account 
WHERE (@dropdownsearch = 'ID' and id=@id) 
   or name=@name
Sign up to request clarification or add additional context in comments.

4 Comments

I think the second condition should be (@dropdownsearch != 'ID' and name=@name)
No, your query will return the redundant rows (name = @name) when @dropdownsearch = 'ID'
@TriV As per my assumption @id and @name will never have values at same time
@Pரதீப் yea, they will never have values at the same time
0

If I understand your dilemma correctly, you have a dropdown where a user may select whether to search on ID or Name. Now you need to pass one or the other in, not both, correct? I have done this before, with multiple dropdown options, using the following logic:

SELECT * FROM account 
WHERE
  (CASE WHEN @dropdownsearch = 'ID' then @id else ID END) = ID,
  (CASE WHEN @dropdownsearch = 'Name' then @name else name END) = name

So what happens here is if they have selected ID then it will compare the value in your @id parameter to that of ID. At the same breath it will compare name to itself. (Name)Hanzou = (Name)Hanzou is always true, so every value in name will pass and @name will be ignored. The reverse works if the user selected 'Name' instead of 'ID'

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.