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 8343699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T06:08:44+00:00 2026-06-09T06:08:44+00:00

Possible Duplicate: Need help in dynamic query with IN Clause I am using SQL

  • 0

Possible Duplicate:
Need help in dynamic query with IN Clause

I am using SQL server 2008 and this is the problem that I am facing. I have a table named Cars with a column Company. Now I have a stored procedure that looks something like this

CREATE PROCEDURE FindCars (@CompanyNames varchar(500))
AS
SELECT * FROM Cars WHERE Company IN (@CompanyNames)

I tried something like this and failed

DECLARE @CompanyNames varchar(500)
SET @CompanyNames = '''Ford'',''BMW'''

exec FindCars @CompanyNames

I dont get any rows returned. When I do the following

DECLARE @CompanyNames varchar(500)
SET @CompanyNames = '''Ford'',''BMW'''

Select @CompanyNames

I get the following result

'Ford','BMW'

and if I replace this value in the select statement inside the stored procedure, it works

SELECT * FROM Cars where Company in ('Ford','BMW')

Thus I think that the stored procedure seems to be treating 'Ford','BMW' as one string rather than an array. Could someone please help me with this. How do I dynamically construct the string/array required in the IN clause of the select statement inside the stored procedure.

  • 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-06-09T06:08:46+00:00Added an answer on June 9, 2026 at 6:08 am

    You are right, you created one string, and that is being processed as a list of strings that contains one string. The commas are just characters in that one string. The only equivilent to an array in SQL Server is a table.

    For example; WHERE x IN (SELECT y FROM z).

    For this reason many people create a SPLIT_STRING() function that returns a table of items from a given comma delimitted string…

    WHERE x IN (SELECT item FROM dbo.split_string(@input_string))
    

    There are many ways to implement that split string. Some return strings, some cast to integers, some accept a second “delimiter” parameter, etc, etc. You can search the internet for SQL SERVER SPLIT STRING and get many results – Including here in StackOverflow.

    An alternative is to use dynamic SQL; SQL that writes SQL.

    SET @sql = 'SELECT * FROM x WHERE y IN (' + @input_string_list + ')'
    
    SP_EXECEUTESQL @sql
    

    (I recommend SP_EXECUTESQL over just EXEC because the former allows you to use parameterised queries, but the latter does not.)

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Need help with a SQL query that combines adjacent rows into a
Possible Duplicate: Need help solving Project Euler problem 200 Similar to this question Project
Possible Duplicate: Regular expression: match all words except I need your help for using
Possible Duplicate: How does this CSS triangle shape work? Please help me i need
Possible Duplicate: need help with some stored procedure plz ERROR:- A SELECT statement that
Possible Duplicate: LINQ Between Operator Dear All, Hi, I need to write this query
Possible Duplicate: Import CSV to mysql Right I need some help with this: I
Possible Duplicate: How to set this Layout in iPhone need Some help I draw
Possible Duplicate: Help with algorithm problem from SPOJ Came across this interview question. Given
Possible Duplicate: Grouping WHERE clauses with Zend_Db_Table_Abstract I need to create something like this:

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.