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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T23:02:48+00:00 2026-05-13T23:02:48+00:00

I am in a situation where I am given a comma-separated VarChar as input

  • 0

I am in a situation where I am given a comma-separated VarChar as input to a stored procedure. I want to do something like this:

SELECT * FROM tblMyTable 
INNER JOIN /*Bunch of inner joins here*/ 
WHERE ItemID IN ($MyList);

However, you can’t use a VarChar with the IN statement. There are two ways to get around this problem:

  1. (The Wrong Way) Create the SQL query in a String, like so:

    SET $SQL = '
    SELECT * FROM tblMyTable
    INNER JOIN /*Bunch of inner joins here*/
    WHERE ItemID IN (' + $MyList + ');

    EXEC($SQL);

  2. (The Right Way) Create a temporary table that contains the values of $MyList, then join that table in the initial query.

My question is:

Option 2 has a relatively large performance hit with creating a temporary table, which is less than ideal.

While Option 1 is open to an SQL injection attack, since my SPROC is being called from an authenticated source, does it really matter? Only trusted sources will execute this SPROC, so if they choose to bugger up the database, that is their prerogative.

So, how far would you go to make your code secure?

  • 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-13T23:02:49+00:00Added an answer on May 13, 2026 at 11:02 pm

    What database are you using? in SQL Server you can create a split function that can split a long string and return a table sub-second. you use the table function call like a regular table in a query (no temp table necessary)

    You need to create a split function, or if you have one just use it. This is how a split function can be used:

    SELECT
        *
        FROM YourTable                               y
        INNER JOIN dbo.yourSplitFunction(@Parameter) s ON y.ID=s.Value
    

    I prefer the number table approach to split a string in TSQL but there are numerous ways to split strings in SQL Server, see the previous link, which explains the PROs and CONs of each.

    For the Numbers Table method to work, you need to do this one time table setup, which will create a table Numbers that contains rows from 1 to 10,000:

    SELECT TOP 10000 IDENTITY(int,1,1) AS Number
        INTO Numbers
        FROM sys.objects s1
        CROSS JOIN sys.objects s2
    ALTER TABLE Numbers ADD CONSTRAINT PK_Numbers PRIMARY KEY CLUSTERED (Number)
    

    Once the Numbers table is set up, create this split function:

    CREATE FUNCTION [dbo].[FN_ListToTable]
    (
         @SplitOn  char(1)      --REQUIRED, the character to split the @List string on
        ,@List     varchar(8000)--REQUIRED, the list to split apart
    )
    RETURNS TABLE
    AS
    RETURN 
    (
    
        ----------------
        --SINGLE QUERY-- --this will not return empty rows
        ----------------
        SELECT
            ListValue
            FROM (SELECT
                      LTRIM(RTRIM(SUBSTRING(List2, number+1, CHARINDEX(@SplitOn, List2, number+1)-number - 1))) AS ListValue
                      FROM (
                               SELECT @SplitOn + @List + @SplitOn AS List2
                           ) AS dt
                          INNER JOIN Numbers n ON n.Number < LEN(dt.List2)
                      WHERE SUBSTRING(List2, number, 1) = @SplitOn
                 ) dt2
            WHERE ListValue IS NOT NULL AND ListValue!=''
    
    );
    GO 
    

    You can now easily split a CSV string into a table and join on it:

    select * from dbo.FN_ListToTable(',','1,2,3,,,4,5,6777,,,')
    

    OUTPUT:

    ListValue
    -----------------------
    1
    2
    3
    4
    5
    6777
    
    (6 row(s) affected)
    

    Your can use the CSV string like this, not temp table necessary:

    SELECT * FROM tblMyTable 
    INNER JOIN /*Bunch of inner joins here*/ 
    WHERE ItemID IN (select ListValue from dbo.FN_ListToTable(',',$MyList));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Okay, this is getting complicated... Given situation: I have a canvas with the dimensions
Given the following situation (UML below), If Y has the method: public void PrintWs();
Forgive my verbosity. I'm not exactly sure how to describe my situation. Given the
I've got the following situation: <h2>This text is <span>pretty awesome</span></h2> I'm trying to give
Situation: I often run into this problematic and never know how to solve it.
This is a really odd situation that I can't seem to work out where
I have a situation where given a Column TotalDue and a Percentage(17,5%)Could be anything.I
Given the following situation: PC --- |aa RTR1 bb| --- |aa RTR2 bb| ---
I did not found a solution to this, so I want to ask you.
I want to get parameter from a URL under portlet environment. The URL i

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.