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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:36:43+00:00 2026-05-12T15:36:43+00:00

Hi is it possible for me to do this somehow? When i run the

  • 0

Hi is it possible for me to do this somehow? When i run the statement i get an exception, @Price_Plan is not delared, so obviously the adhoc query does not have scope to access @Price_Plan. Is there a workaround, or a better way to query a table whose name changes per execution of this query.

DECLARE @Price_Plan varchar(3), @MNP_Network varchar(3), @GSM_Code varchar(3), @GEO_Dist varchar(6),
        @Call_ProdNo varchar(7), @Call_Time datetime, @CallId int, @dtl_call_dur int,
        @Volume varchar(10), @Call_Cost int

--Assume CallId has a value

SET @Sql =
    'SELECT
        @Price_Plan = Price_Plan, @MNP_Network = MNP_Network, @GSM_Code = GSM_Code, @GEO_Dist = GEO_Dist,
        @Call_ProdNo = Call_ProdNo, @Call_Time = Call_Time, @dtl_call_dur = dtl_call_dur,
        @Volume = Volume
     FROM ' + @TableName + ' 
     WHERE CallId = ' + CONVERT(varchar(10),@CallId) + ''
    PRINT  @SQL
    EXEC (@Sql)
  • 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-12T15:36:43+00:00Added an answer on May 12, 2026 at 3:36 pm

    Are you sure this is this error? I tested that query and it returned two errors:

    - Not declared @TableName
    - Not declared @Sql
    

    When you declare those variables it should work good.

    But better way is to use sp_executesql.

    An example:

    EXEC sp_executesql 
          N'SELECT * FROM AdventureWorks.HumanResources.Employee 
          WHERE ManagerID = @level',
          N'@level tinyint',
          @level = 109;
    

    First argument is the query with parametes, 2nd argument – parameters names with types separated by commas, and then goes the actual values of the parameters.

    EDITED:

    Here is the another example using OUTPUT parameter:

    DECLARE @retCnt INT
    
    EXEC sp_executesql 
        N'SELECT @retCnt = COUNT(*) FROM sys.tables',
        N'@retCnt INT OUTPUT',
        @retCnt = @retCnt OUTPUT
    
    SELECT @retCnt
    

    SELECT return 5 on my computer.

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

Sidebar

Related Questions

I run a small blog network and on this I have a page where
Why does this not work? What I am trying to do: I need a
I have a perl script which when run from the command line generates a
I have a basic executable JAR with manifest.txt containg the MainClass and some other
Recently I created a script that accesses another script to run it. The only
For example, when doing Analytics, there can be a map/reduce run that takes 10
I have created a communication library which is linked statically into several different applications.
I had some problems figuring out a good title, but hopefully the code examples
I'm using Reportlab to create PDFs. I'm creating two PDFs which I want to
I am writing an application that must update parts of an already existing xml

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.