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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:37:25+00:00 2026-05-31T06:37:25+00:00

what I am trying to do is run a query multiple times over multiple

  • 0

what I am trying to do is run a query multiple times over multiple tables, so what I have here is a table of the table names that cycles through setting @tablename to the name of the table on each iteration that I want to run the query on.

As you can see below @tablename is the name of the table I want to run the queries on but how do i run these queries using @tablename as the table name?

CREATE TABLE [BusinessListings].[dbo].[temptablenames]
(id int,
name nvarchar(50),
)

INSERT INTO [BusinessListings].[dbo].[temptablenames] (id, name)
VALUES 
(1,'MongoOrganisationsACT1'),
(2,'MongoOrganisationsNSW1'),
(3,'MongoOrganisationsNT1'),
(4,'MongoOrganisationsQLD1'),
(5,'MongoOrganisationsSA1'),
(6,'MongoOrganisationsTAS1'),
(7,'MongoOrganisationsVIC1'),
(8,'MongoOrganisationsWA1');

DECLARE @tablename sysname,
@id int
SET @id = 1
WHILE (@id < 9)
BEGIN
select @tablename = name from temptablenames where id = @id

select @tablename


        select _key_out, sum(quality_score) as sumscore, count(*) as reccount, (sum(quality_score) / count(*)) as ave
        into tempga0
        from @tablename
        group by _key_out

        select _key_out, count(*) as reccount
        into tempga3
        from @tablename
        where dedupe_result is null
        group by _key_out
        having count(*)>1

        select a._key_out, max(quality_score) as maxdedupetotalscore
        into tempga4
        from
        @tablename a
        join
        tempga3 b
        on a._key_out = B._key_out
        --where isdeleted is null
        group by a._key_out

        --- keep records
        update @tablename
        set dedupe_result = 'Keep'
        from 
        @tablename a
        join
        tempga4 b
        on a._key_out = B._key_out  
        where a.quality_score = b.maxdedupetotalscore
        --and isdeleted is null
        and dedupe_result is null

SET @id = @id + 1 
END
GO

DROP TABLE [BusinessListings].[dbo].[temptablenames]

note: this is only part of the queries that I want run, I just want to figure out how to subsitute the variable in the query as the table name. Also I know this isnt good form but there is a reason I need to do it this way.

updated working code here:

DECLARE @tablename nvarchar(30),
@id int,
@SQLStr nvarchar(1000)
SET @id = 1
WHILE (@id < 9)
BEGIN
select @tablename = name from temptablenames where id = @id

IF OBJECT_ID('tempga0') IS NOT NULL
DROP TABLE tempga0

    set @SQLStr = 'select _key_out, sum(quality_score) as sumscore, count(*) as reccount, (sum(quality_score) / count(*)) as ave
into tempga0
from ' + @tablename + ' group by _key_out'

    exec(@SQLStr)


 SET @id = @id + 1 
END
GO
  • 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-31T06:37:26+00:00Added an answer on May 31, 2026 at 6:37 am

    Use the Exec command. Write your query in a variable like and execute it

    Declare @SQLStr = 'Select * into X from ' + @tablename
    exec(@SQLStr)
    

    You just have to be carefull. I see that you are using into statements. You will have to check that the table does not already exist because you will get an exception. You will need to drop the tables, or a better way would be to do this before you start your loop:

    CREATE TABLE tempga0 (
    _key_out int,
    sumscore numeric(18,9),
    reccount int,
    ave numeric(18,9))
    
    --rest of the tables to be created here...
    

    Create all the tables, and when you start your While loop add a

    WHILE (@id < 9)         
    BEGIN    
        TRUNCATE TABLE tempga0
        --truncate the rest of the tables
    
        --Do the rest of your stuff here
    END
    

    Hope it helps

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

Sidebar

Related Questions

I'm trying to run an update query that updates one table based on rows
Trying to construct a query such that I have multiple statement specifying joins, each
I'm currently trying to run a LINQ query over a MS SQL database. This
I'm trying to run a LINQ to SQL query that returns a result in
I am trying to query an .xls spreadsheet with VBScript, but I have run
I'm trying to insert information into multiple tables within a database, i have managed
I'm trying to form a query string from multiple checkboxes that will be used
Using SQL 2005, I am trying to run a query on a Orders table
as the title states, I am trying to query multiple mySQL tables in the
I have multiple tables in a mysql database and I am trying to put

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.