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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T21:39:14+00:00 2026-05-11T21:39:14+00:00

Given a table name and column name in a pair of variables, can I

  • 0

Given a table name and column name in a pair of variables, can I perform a select query without using dynamic sql?

for example, I’d like something nicer than this:

CREATE PROCEDURE spTest (@table NVARCHAR(30), @column NVARCHAR(30)) AS

  DECLARE @sql NVARCHAR(2000) 
  SELECT @sql = N'SELECT ' + @column + N' FROM ' + @table
  PRINT @sql

  EXEC sp_executesql @sql

I’d like to do this because my dynamic sql version is 3x slower than the non-dynamic version (which doesn’t support a programmable table/column name, hence this question).

  • 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-11T21:39:15+00:00Added an answer on May 11, 2026 at 9:39 pm

    The dynamic version is going to be 3x slower, solely because your’e going to be swapping in and out the table name, and the parser can’t optimize for that.

    You might want to use a nested switch statement of sorts in your routine and use that to select your SELECT statement from pre-defined tables/columns, especially if your schema won’t be changed frequently. That should run lots faster, but you’ll lose the true dynamic-ness.

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

Sidebar

Related Questions

I want to convert a table storing in Name-Value pair data to relational form
If some one does right click on a given Table in a Database using
How to get the dynamic select results of EXECUTE within PL/SQL from Oracle sqlplus
Similar but NOT IDENTICAL to SQL Server 2000 - Query a Table’s Foreign Key
I would like to query using hibernate given a particulay query string I did
I am currently using... select Table_Name, Column_name, data_type, is_Nullable from information_Schema.Columns ...to determine information
I have the following entity (getters and setters ommited)... @Entity @Table(name = TBL_PROJECT_RUN) public
I have a column called DayShift in a table which is of Yes/No data
I have a table with rows like below id, name , phName , startDate,
I want to have a database table that keeps data with revision history (like

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.