Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6833193
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:56:55+00:00 2026-05-26T22:56:55+00:00

I have an application that executes a stored procedure. There is a combo box

  • 0

I have an application that executes a stored procedure. There is a combo box “City” and the user that can select only one value and I use that value as an SQL parameter in My C# code.

Simplified Example :

Select StudentName, Age 
From dbo.Students
Where City = @city

@city is replaced by the value from the combo box.

Now the requirement is changed and the combo box supports multiple selection. The selected elements are stored in an array. Now I am using the following approach.

  1. Use String.Join(“‘,'”) and join the array elements and the result is assigned to @city.
  2. Changing the query to use IN operator.

    Select StudentName, Age
    From dbo.Students
    Where City IN (@city)

Suppose the selected cities are London and Sydney then @City becomes London’,’Sydney. Then in SP it will be used as ‘London’,’Sydney’ which is syntactically correct. But this query fails.

What do I do incorrectly?
What is the best (Nice) approach to handle this?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:56:56+00:00Added an answer on May 26, 2026 at 10:56 pm

    The way you want to use IN is incorrect, it doesn’t work that way.

    Options:

    1. send a CSV string to the SP then split it and create a temporary dataset (table variable for instance) with which you will then do a JOIN
    2. dynamic SQL

    Example split function:

    CREATE FUNCTION [dbo].[Split](
        @String nvarchar (4000),
        @Delimiter nvarchar (10)
     )
    RETURNS @ValueTable TABLE ([Value] nvarchar(4000), [ElementsCount] int)
    BEGIN
         DECLARE @nextString nvarchar(4000)
         DECLARE @pos int
         DECLARE @nextPos int
         DECLARE @commaCheck nvarchar(1)
         DECLARE @elementsCount int
    
         SET @elementsCount = 0
    
         --Initialize
         SET @nextString = ''
         SET @commaCheck = RIGHT(@string, 1) 
    
         --Check for trailing Comma, if not exists, INSERT
         SET @string = @string + @delimiter
    
         --Get position of first Comma
         SET @pos = CHARINDEX(@delimiter, @string)
         SET @nextPos = 1
    
         --Loop while there is still a comma in the String of levels
         WHILE (@pos <>  0)  
         BEGIN
              SET @nextString = SUBSTRING(@string, 1, @pos - 1)
    
              INSERT INTO @ValueTable ([Value]) VALUES (@nextString)
    
              SET @string = SUBSTRING(@string, @pos +1, LEN(@string))
    
              SET @nextPos = @pos
              SET @pos = CHARINDEX(@delimiter, @string)
    
              SET @elementsCount = @elementsCount + 1
         END
    
         UPDATE @ValueTable SET [ElementsCount] = @elementsCount
    
         RETURN
    END
    

    And then you use it like so:

    SELECT
        StudentName, Age
    FROM
        dbo.Students
        JOIN [dbo].[Split](@city, ',') Cities ON City = Cities.Value
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that executes the following MySQL query: SELECT 402 AS user_id,
I have a stored procedure that executes a couple of queries. Each query might
I have a ruby application that executes ant as a subprocess using backtick. This
I have a C++ application that works and compile perfectly. I can execute and
I have application that is up more than 3 days. I can see in
We have a stored procedure in which there are two parts. In part 1,
I have this code which calls a stored procedure within ZEND. Is there a
I have a stored procedure, PROC , which receives some parameters. If one of
I have a problem where it seems that when running the same stored procedure
I have a stored procedure that is executed by remote clients on an ongoing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.