0

I got stuck trying to find the solution to this one, maybe someone can help?

Based on a int parameter sent from C#, the parameter used in our query should change. Pseudo code:

DECLARE @parameterFromC# int = 1

DECLARE @city1 nvarchar = 'London'
DECLARE @city2 nvarchar = 'Paris'
DECLARE @city3 nvarchar = 'New York'

DECLARE @mainParameter nvarchar

SELECT * from Customers
WHERE City = @mainParameter

When @parameterFromC# is 1, @mainParameter should be = @city1,
when @parameterFromC# is 2, @mainParameter should be = @city2 etc

My solution was to make @parameterFromC# nvarchar, send the number as string and then concatenate it with '@city'. In pseudo code it was something like this:

@mainParameter = '@city' + @parameterFromC#

I was told this is unsafe and to find some other way.

7
  • Why not have C# just pass the value for @mainParameter and bin @parameterFromC# and @City1, etc? Commented Mar 25, 2019 at 17:10
  • the problem is actually more complex, this is a part of it Commented Mar 25, 2019 at 17:12
  • 1
    To simplify you should create a table with a city name and an Id column. Then when you are supplied with the Id which i believe that's what you are trying to do, You can simply do a join with a where clause to have a cleaner query. Commented Mar 25, 2019 at 17:13
  • 4
    If the problem is more complex, why not give us the full problem? Commented Mar 25, 2019 at 17:15
  • 2
    Sounds like an XY Problem to me (especially based on the fact that you haven't/won't share the "bigger picture" with us). Commented Mar 25, 2019 at 18:08

2 Answers 2

2

Another and maybe more readable way might be to separate the logic for determining the sql parameter value vs the actual usage of the sql parameter. It's highly likely there is no performance gain or lost, the sql compiler is very smart.

DECLARE @parameterFromC int = 1

DECLARE @city1 nvarchar = 'London'
DECLARE @city2 nvarchar = 'Paris'
DECLARE @city3 nvarchar = 'New York'

DECLARE @mainParameter nvarchar

SET @mainParameter = CASE
  WHEN @parameterFromC = 1 THEN @city1
  WHEN @parameterFromC = 2 THEN @city2
  ELSE @city3
END

SELECT * from Customers
WHERE City = @mainParameter
Sign up to request clarification or add additional context in comments.

4 Comments

You could also just skip city1, city2, city3 and set the value of mainparmeter using this case expression.
@SeanLange I totally agree. My goal was just to add the logic to the existing code base.
This example worked on my home computer, thank you. However, the task had to be done on a old 2008 version of SQL server where SQL Switch does not work, so in the end another solution was used.
@CristiPriciu you did something wrong. CASE does exist in MSSQL 2008 and up.
2

I agree with Larnu - I think you should pass in the city via C#, however I don't know your circumstances. You should be able to use a CASE/WHEN.

SET @mainParameter=CASE 
               WHEN @parameterFromC=1 THEN @city1 
               WHEN @parameterFromC=2 THEN @city2
               ELSE @city3
             END

Use this line after you declare the variable and before you do your select on the Customers table.

You don't necessarily need @mainParameter. You could use the CASE/WHEN in the where clause.

SELECT * 
FROM Customers
WHERE City = CASE 
               WHEN @parameterFromC=1 THEN @city1 
               WHEN @parameterFromC=2 THEN @city2
               ELSE @city3
             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.