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

  • Home
  • SEARCH
  • 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 1045223
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:57:05+00:00 2026-05-16T15:57:05+00:00

I have Created a Procedure for one of our .Net Devs where they can

  • 0

I have Created a Procedure for one of our .Net Devs where they can pass in values that will then build and output a result set. Part of the params they pass in will determine which table gets called. My question is how can I ensure that the dynamic sql statement I am building will get cached for faster execution? None of the cache examples I read discussed dynamic table names (might have just overlooked), just dynamic params within the query.

I am using sp_executesql and passing in params, but I am not sure how I use that method for deriving the table name without appending the Parameter to the select statement?

Here is a simple example of how I am currently building the string to generate my result set.

IF  EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[DynamicSQL]') AND type in (N'U'))
DROP TABLE [dbo].[DynamicSQL]
GO

CREATE TABLE [dbo].[DynamicSQL](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [TestName] [varchar](100) NULL
) ON [PRIMARY]

GO

Insert Into DynamicSQL
Values('Name1');
Insert Into DynamicSQL
Values('Name2');
Insert Into DynamicSQL
Values('Name3');
Insert Into DynamicSQL
Values('Name4');

GO

DECLARE @TableName VARCHAR(50),
        @SQL       NVARCHAR(500)

SELECT @TableName = 'DynamicSQL',
       @SQL = 'Select *
            From ' + @TableName;

execute sp_executesql @SQL;

Any info is greatly appreciated.

–S

  • 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-16T15:57:06+00:00Added an answer on May 16, 2026 at 3:57 pm

    I would strongly recommend against this sort of “do it” procedure. It is far better to have your .NET developers work in tandem with a DBA to create proper SQL statements or at the very least use an ORM rather than try to handle database calls in the manner you have suggested. If you are going to force your developers to use stored procedures, then write out the stored procedures. It will make maintenance and performance analysis substantially easier than having one stored proc to rule them all.

    how can I ensure that the dynamic sql statement I am building will get cached for faster execution?

    SQL Server should cache the execution plan of two identical queries. Thus, if your dynamic SQL generates two identical queries, SQL Server will cache the execution plan. What it will not do is cache two queries that ought to use the same execution plan but are slightly different. For example, if your dynamic SQL generates Select ... From Table Where Col = 1 and Select ... From Table Where Col = 2, SQL Server will calculate two execution plans even if the two values have the same cardinality. This is one place where parametrized queries or statically written stored procedures are better.

    I am using sp_executesql and passing in params, but I am not sure how I use that method for deriving the table name without appending the Parameter to the select statement?

    If you are going to use sp_executesql, you have to build the entire SQL statement including the table as a string. You cannot pass the table name as a parameter.

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

Sidebar

Related Questions

I have created a procedure and used the below statement inside that . select
I have a modal dialog that is created with the following: procedure TFormCompose.createParams(var Params:
we have a big portal that build using SharePoint 2007 , asp.net 3.5 ,
I currently have a stored procedure that copies content from one table to another.
We have an application that handling our own message - a timered procedure read
We have scaled-out some portions of our ASP.NET app to run on one server,
One of our programs is erroring out because a stored procedure was created with
I have created one stored procedure and its functioning well, what I am wanting
I have created a Windows Forms GUI in C# that can display tabular data
I have created a procedure with this structure but it doesn't work for datetime

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.