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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:56:49+00:00 2026-06-13T09:56:49+00:00

I have a cursor that gets data and fetches it into @data_table. Then in

  • 0

I have a cursor that gets data and fetches it into @data_table. Then in the while loop I want to pass that @data_table into another cursor (as the table name) to run some more processing. I keep getting a declare @data_table error. How do I fix this?

DECLARE @var_name varchar(50)
DECLARE @data_table varchar(50)
DECLARE @data_value varchar(50)

DECLARE curDiscreteVars CURSOR LOCAL

FOR SELECT DISTINCT v.var_name, v.data_table 
    FROM dbo.vars v
    GROUP BY v.var_name, v.data_table

-- Loop through cursor, translating variable values as needed, and generate counts for each val_code for a variable
OPEN curDiscreteVars
FETCH NEXT FROM curDiscreteVars 
    INTO @var_name, @data_table

WHILE @@FETCH_STATUS  = 0
BEGIN
    --loop through all possible data values

    DECLARE curValues CURSOR LOCAL

    FOR SELECT DISTINCT @var_name  
        FROM @data_table

    OPEN curValues
    FETCH NEXT FROM curValues 
        INTO @data_value

    WHILE @@FETCH_STATUS  = 0
    BEGIN
        print @var_name



        FETCH NEXT FROM curValues 
        INTO @data_value
    END

    CLOSE curValues
    DEALLOCATE curValues

    FETCH NEXT FROM curDiscreteVars 
    INTO @var_name, @data_table
END

CLOSE curDiscreteVars
DEALLOCATE curDiscreteVars
  • 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-13T09:56:50+00:00Added an answer on June 13, 2026 at 9:56 am

    I’m not sure I understand what you’re talking about doing, but variables cannot be used as table names. Or, really anything that’s not a field name. You’ll need to use dynamic SQL. That is, assign your SQL string to a variable, an then run EXEC() command.

    For example:

     DECLARE @sqlcmd varchar(max)
     DECLARE @table_name sysname
    
     DECLARE cur_tables FOR
     SELECT name FROM sys.tables
    
     OPEN cur_tables
     FETCH NEXT FROM cur_tables INTO @table_name
    
     WHILE @@FETCH_STATUS = 0 BEGIN
     SET @sqlcmd = 'SELECT TOP 10 * FROM ' + QUOTENAME(@table_name)
     EXEC ( @sqlcmd )
     FETCH NEXT from cur_tables INTO @table_name
     END
    
     CLOSE cur_tables
     DEALLOCATE cur_tables
    

    Alternately, if what you mean is that you need a location to store the data that is like a table, then create a temporary tabled for it.

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

Sidebar

Related Questions

If I want to have an ExpandableListView, which gets data from a response from
I have an do - while loop with 5000 numbers that should get added
I have a status table, and another table containing additional data. My object IDs
I currently have a database that within a loop checks a table if a
I have a a cursor loop that's building a string by concatenating the contents
I currently use SQL2008 where I have a stored procedure that fetches data from
I have created a custom Cursor Adapter for my list.The list gets some data
I have this class, that gets the data from the database and shows in
I have a DataGrid which gets its data from an sql table. I have
I have a javascript that gets the value of the cursor position and it

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.