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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T23:07:15+00:00 2026-05-31T23:07:15+00:00

I am writing a program in C# that runs some select statements using parameters

  • 0

I am writing a program in C# that runs some select statements using parameters passed to sp_executesql. One issue that I’m running into when testing is that, whether I get the the commands executed from SQL Profiler or from a watch in Visual Studio, the values of the parameters are specified at the end of statement rather than being explicitly specified in-line in the query. For testing purposes, I would like a quick way to substitute the parameter values for the parameters.

So, instead of:

exec sp_executesql N'
SELECT CustomerName
FROM CustomerTable ct WITH(NOLOCK)
WHERE ct.CustomerId <> @CustomerId
AND ct.ItemId <> @ItemId
AND ct.TransactionId = @TransactionId'
,N'@CustomerId bigint,@ItemId nvarchar(1),@TransactionId nvarchar(30), @CustomerId = 3000, @ItemId = N'4', @TransactionId=N'43281'

I want:

exec sp_executesql N'
SELECT CustomerName
FROM CustomerTable ct WITH(NOLOCK)
WHERE ct.CustomerId = 3000
AND ct.ItemId <> N'4'
AND ct.TransactionId = N'43281''

Please don’t pay too much attention to the syntax of the example, since it is just being used to demonstrate the concept. Does anyone know a fast way to do this? Basically, I would like to have it substituted for testing purposes, as it will make it easier for me to modify conditions to test how they affect the results returned. I would appreciate any help anyone can give. Thanks.

  • 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-31T23:07:16+00:00Added an answer on May 31, 2026 at 11:07 pm

    Parameterized sp_executesql has many benefits, including

    • By explicit parameterizing you are giving the chance for SQL to cache decent query plans on definite types
    • By parameterizing it helps prevent nasties like SQL injection attacks, but also avoids the need to escape problematic characters.

    So even if you do manage to ‘unparameterize’ the generated sp_executesql, if you execute the inline sql, the query plan could be significantly different to the parameterized version, and you would also need to do escaping etc (i.e. it wouldn’t be suitable for apples vs apples testing).

    The only reason I can think of why you wouldn’t want parameterized sp_executesql would be for ease of readability?

    Edit: Trying to substitute would be dependent on what technology you are using

    As @mellamokb suggested, if you are using ExecuteReader this could be quite straightforward

    Assuming your code was something like

    string sqlCmd = "SELECT CustomerName 
    FROM CustomerTable ct WITH(NOLOCK) 
    WHERE ct.CustomerId <> @CustomerId 
    AND ct.ItemId <> @ItemId 
    AND ct.TransactionId = @TransactionId";
    
    cmd.CommandText = sqlCmd;
    cmd.CommandType = CommandType.Text;
    cmd.Parameters.Add(new SqlParameter("CustomerId", DbType.Int32, myCustomerId));
    cmd.Parameters.Add(new SqlParameter("ItemId", DbType.String, myItemId));
    ..
    cmd.ExecuteReader()
    

    You could then add code to build your test query:

    string sqlMyTest = sqlCmd.Replace("@CustomerId", myCustomerId.ToString());
    sqlMyTest = sqlMyTest.Replace("@ItemId", specialEscapeFunction(myItemId));
    .. do something with sqlMyTest
    

    However an ORM like Linq2SQL or EF would not be as easy

    customerTable.Where(c => (c.CustomerId != myCustomerId) && (c.ItemId != myItemId) && (c.TransactionId == myTransactionId))
    

    Possibly a tool like LinqPad might help?

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

Sidebar

Related Questions

I am writing a custom p2p program that runs on port 4900. In some
I am writing a program that runs some unit tests on code that that
I am writing a program that is meant to be extended by some function
I have a program that runs another one (let's call the first app Stater
I am writing a program that starts another java process which runs certain code.
I am writing a java program that runs under unix. It would like run
I am writing program witch runs msi installations silent on background. Using MsiSetExternalUI and
I'm writing a python program that runs on a rather slow phone (as well
I am writing a script that calls another java program, to perform some tasks.
I'm currently writing a little GUI program that does some work and exits afterwards.

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.