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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:09:07+00:00 2026-06-04T19:09:07+00:00

I have a database with dynamically generated tables. I’m writting a stored procedure to

  • 0

I have a database with dynamically generated tables. I’m writting a stored procedure to execute queries over the generated tables and I need to see the results in the query analyzer tool (or alike).

What I currently have is:

DECLARE @TableName sysname

DECLARE TableNameCursor CURSOR FOR
SELECT TableName FROM [xxxTables] WHERE xxx...

OPEN TableNameCursor
FETCH NEXT FROM TableNameCursor INTO @TableName

WHILE @@FETCH_STATUS = 0
BEGIN
    DECLARE @Query nvarchar
    SET @Query = 'SELECT * FROM ' + @TableName
    EXEC sp_executesql @Query

    -- This is executed as long as the previous fetch succeeds.
    FETCH NEXT FROM TableNameCursor INTO @TableName
END

But as I have multiple selects I’m not getting the output. How can I fix the stored procedure to show the result?

All the tables have the same layout so I can join them.

  • 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-04T19:09:10+00:00Added an answer on June 4, 2026 at 7:09 pm

    Unless this is merely a typo produced when posting the script here, I might have finally found the issue. You haven’t specified the maximum length for the @Query variable:

    DECLARE @Query nvarchar
    

    In this context the maximum length defaults to 1, as per the manual:

    When n is not specified in a data definition or variable declaration statement, the default length is 1. When n is not specified with the CAST function, the default length is 30.

    When you are assigning a query string to @Query, it only stores the first character. Naturally, when you are later trying to execute the query, it returns nothing and should produce an error instead.

    You can fix it by specifying a maximum length. In SQL Server 2005 or later version, it could be an explicit number (up to 4000) or max:

    DECLARE @Query nvarchar(max)
    

    In earlier versions max is not supported, so just use an explicit number:

    DECLARE @Query nvarchar(4000)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have database, and in one the tables I need change the values of
I have a part of webpage that is dynamically generated using MySQL queries: <table
i have a menu generated dynamically by taking title from database. i have set
I have a drop down list that is dynamically generated using a mySQL database
I'm binding a gridview dynamically from a table in my database I have called
I have a web application that dynamically loads assemblies based on database configuration entries
I have a DataGrid which is being bound dynamically to a database query. The
I have some flags I store in my mysql database so I can dynamically
I have Database with date field. I see the time like: 1900-01-01 13:38:00.000 How
I have a table of data that is generated dynamically based on the contents

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.