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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T22:17:00+00:00 2026-06-18T22:17:00+00:00

I have c# web application that submits a query to sql server. The query

  • 0

I have c# web application that submits a query to sql server. The query has three parameters, two are ints and one is nchar. I’m getting a different query plan for each length of the nchar parameter. Is there a way to only get one plan?

Some more details:

The query is fairly large and complex, so I won’t inlcude it all here. However, there WHERE part is

WHERE gp.Control=@Control AND r.RegionType=@RegionType

r.RegionType is a column in a table defined as nchar(10). My c# code, sets the parameter with the following:

cmd.Parameters.Add(parmRegionType, System.Data.SqlDbType.NChar).Value = rgn.Key;

where rgn.key may have one of the following four values: “All”, “County”, “DGP”, or “State”.

When I dump the query plan with

SELECT usecounts, cacheobjtype, objtype, text, query_plan
FROM sys.dm_exec_cached_plans 
CROSS APPLY sys.dm_exec_sql_text(plan_handle) 
CROSS APPLY sys.dm_exec_query_plan(plan_handle) 
WHERE text LIKE '%(@RegionType nchar(%),@CacheCnt int,@Control int)%'
AND text NOT LIKE '%this query%'

I get the following results (edited format and truncated text)

usecounts cacheobjtype  objtype  text                                                                                                                                                                                                                                                             query_plan
--------- ------------- -------- -----------------------------------------------
1         Compiled Plan Prepared (@RegionType nchar(5),@CacheCnt int,@Control...
1         Compiled Plan Prepared (@RegionType nchar(6),@CacheCnt int,@Control...
2         Compiled Plan Prepared (@RegionType nchar(3),@CacheCnt int,@Control...

(3 row(s) affected)

So you can see that there is a query plan for when @RegionType is three characters long (“All” and “DGP”), five characters long (“State”) and six characters long (“County”).

Is there a way to code this so that only one query plan is prepared independent of the length of the @RegionType parameter?

  • 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-18T22:17:01+00:00Added an answer on June 18, 2026 at 10:17 pm

    Use NVarChar and set the length to the maximum expected possible value (e.g. whatever the max length of the RegionType column is).

    Looks like 6 in your case as that is the length of County

    cmd.Parameters.Add(parmRegionType, System.Data.SqlDbType.NVarChar, 6).Value = rgn.Key;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a web application that has a constant URL and internal state machine.
I currently have a TCP server application written in .Net that receives and submits
I have a web application that runs multiple websites from one codebase. I have
I have mvc3 web application. In that i have used EF and populate two
My web application has a login page that submits authentication credentials via an AJAX
I have a web application that allows users to enter search criteria and the
I have a web application that will be used on iPhones, and want to
We have a web application that is generating some 3-5 parallel threads every five
I have a web application that maintains a version flag as a ServletContext.setAttribute('flag', flagValue)
I have a Web Application that use Hibernate 3.0. When I restart my database

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.