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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:16:43+00:00 2026-05-14T04:16:43+00:00

I am trying to use sp_executesql to prevent SQL injection in SQL 2005, I

  • 0

I am trying to use sp_executesql to prevent SQL injection in SQL 2005, I have a simple query like this:

SELECT * from table WHERE RegionCode in ('X101', 'B202')

However, when I use sp_executesql to execute the following, it doesn’t return anything.

Set @Cmd = N'SELECT * FROM table WHERE RegionCode in (@P1)'
SET @ParamDefinition = N'@P1 varchar(100)';
DECLARE @Code as nvarchar(100);
SET @Code = 'X101,B202'
EXECUTE sp_executesql @Cmd, @ParamDefinition, @P1 = @Code

The is what I have tested:

SET @Code = 'X101'   <-- This works, it returns a single region
SET @Code = 'X101,B202'   <--- Returns nothing
SET @Code = '''X101'',''B202'''  <-- Returns nothing

Please help…. what did I do wrong?

  • 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-14T04:16:43+00:00Added an answer on May 14, 2026 at 4:16 am

    The reason it doesn’t work is because @P1 is treated as one, single value.

    e.g. when @Code is X101,B202 then the query is just being run as:
    SELECT * FROM Table WHERE RegionCode IN (‘X101,B202’)
    So, it’s looking for a RegionCode with the value that is contained with @P1. Even when you include single quotes, all that means is the value it searches for in RegionCode is expected to contain those single quotes.

    You’d need to actually concatenate the @Code variable into the @Cmd sql command text in order for it to work the way you are thinking:

    SET @Code = '''X101'',''B202'''
    SET @Cmd = 'SELECT * FROM Table WHERE RegionCode IN (' + @Code + ')'
    EXECUTE (@Cmd)
    

    Obviously though, this just opens you up to SQL injection so you’d need to be very careful if you took this approach to make sure you guard against that.

    There are alternative ways of dealing with this situation where you want to pass in a dynamic list of values to search for.

    Check out the examples on my blog for 2 approaches you could use with SQL Server 2005. One involves passing in a CSV list in the form “Value1,Value2,Value3” which you then split out into a TABLE variable using a user defined function (there’s a lot of mentions of this approach if you do a quick google or search of this site). Once split out, you then join that TABLE var in to your main query. The second approach is to pass in an XML blob containing the values and use the built-in XML functionality of SQL Server. Both these approaches are demonstrated with performance metrics in that link, and they require no dynamic SQL.

    If you were using SQL Server 2008, Table Value Parameters would be the way to go – that’s the 3rd approach I demonstrate in that link which comes out best.

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

Sidebar

Related Questions

I have a ASPX.NET DataGrid and im trying to USE a select LIKE 'X'%
I have this query: SELECT p.text,se.name,s.sub_name,SUM((p.volume / (SELECT SUM(p.volume) FROM phrase p WHERE p.volume
I'm trying to use this DataBase that come with this sample project with from
I am trying (unsuccessfully) to use sp_send_dbmail (Transact-SQL) . The @recipients value is said
Trying to use: :On Error exit :r D:\opt\db_objects\REPORTS\dbo.sp_ReportCountLORUsers.sql ...and I get: Msg 102, Level
I am trying use gem tire to search in my application. I have tables
I'm trying use mod_rewrite to rewrite URLs from the following: http://www.site.com/one-two-file.php to http://www.site.com/one/two/file.php The
Hi I'm trying use a datepicker on a field I have. I'm trying to
Whenever I use the ORM deisgner in LinqToSql I have real problems trying to
Hi I have the following SP, however when I use LINQ to SQL it

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.