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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T04:46:25+00:00 2026-06-17T04:46:25+00:00

I have a SourceTable and a table variable @TQueries containing various T-SQL predicates that

  • 0

I have a SourceTable and a table variable @TQueries containing various T-SQL predicates that target SourceTable.

The expected result is to dynamically generate SELECT statements that return a list of Id’s as specified by the predicates in @TQueries. Each dynamically generated SELECT statement also needs to execute in a particular order, and the final set of values needs to be unique and the ordering must be preserved.

Fortunately, there’s a limit to how many values need to be retrieved and how many dynamic queries need to be generated. The Id list should contain at most 10 Ids, and we don’t expect more than 7 queries.

The following is a sample of this setup, not the actual data/database:

-- Set up some test data, this is quick and dirty just to provide some data to test against
IF NOT EXISTS (SELECT * FROM sys.objects WHERE object_id = OBJECT_ID(N'[dbo].[SourceTable]') AND type in (N'U'))
BEGIN
    -- Create a numbers table, sorta
    SELECT TOP 20 
        IDENTITY(INT,1,1) AS Id,
        ABS(CHECKSUM(NewId())) % 100 AS [SomeValue]
    INTO [SourceTable]
    FROM sysobjects a
END


DECLARE @TQueries TABLE (
    [Ordinal] INT,
    [WherePredicate] NVARCHAR(MAX),
    [OrderByPredicate] NVARCHAR(MAX)
);

-- Simulate SELECTs with different order by that get different data due to varying WHERE clauses and ORDER conditions
INSERT INTO @TQueries VALUES ( 1, N'[Id] IN (6,11,13,7,10,3,15)',  '[SomeValue] ASC' ) -- Sort Asc
INSERT INTO @TQueries VALUES ( 2, N'[Id] IN (9,15,14,20,17)', '[SomeValue] DESC' ) -- Sort Desc
INSERT INTO @TQueries VALUES ( 3, N'[Id] IN (20,10,1,16,11,19,9,15,17,6,2,3,13)', 'NEWID()' ) -- Sort Random

My main issue has been avoiding the use of a CURSOR or iterating through the rows one by one. The closest I’ve come to a set operation that meets this criteria is using a table variable to store the results of each query or a massive CTE.

Suggestions and comments are welcome.

  • 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-17T04:46:26+00:00Added an answer on June 17, 2026 at 4:46 am

    Here’s a solution that builds a single statement both to run all the queries and to return the results.

    It uses a similar approach as in your answer when iterating over the @TQueries table, i.e. it also uses {...} tokens where column values from @TQuery should go, and it puts the values there with nested REPLACE() calls.

    Other than that, it heavily depends on ranking functions, and I’m not sure if doesn’t really abuse them. You’d need to test this method before deciding if it’s better or worse than the one you’ve got so far.

    DECLARE @QueryTemplate nvarchar(max), @FinalSQL nvarchar(max);
    
    SET @QueryTemplate =
    N'SELECT
      [Id],
      QueryRank = {Ordinal},
      RowRank = ROW_NUMBER() OVER (ORDER BY {OrderByPredicate})
    FROM [dbo].[SourceTable]
    WHERE {WherePredicate}
    ';
    
    SET @FinalSQL =
    N'WITH AllData AS (
    ' +
    SUBSTRING(
      (
        SELECT
          'UNION ALL ' +
          REPLACE(REPLACE(REPLACE(@QueryTemplate,
            '{Ordinal}'         , [Ordinal]         ),
            '{OrderByPredicate}', [OrderByPredicate]),
            '{WherePredicate}'  , [WherePredicate]  )
        FROM @TQueries
        ORDER BY [Ordinal]
        FOR XML PATH (''), TYPE
      ).value('.', 'nvarchar(max)'),
      11,                      -- starting just after the first 'UNION ALL '
      CAST(0x7FFFFFFF AS int)  -- max int; no need to specify the exact length
    ) +
    '),
    RankedData AS (
      SELECT
        [Id],
        QueryRank,
        RowRank,
        ValueRank = ROW_NUMBER() OVER (PARTITION BY [Id] ORDER BY QueryRank)
      FROM AllData
    )SELECT TOP (@top)
      [Id]
    FROM RankedData
    WHERE ValueRank = 1
    ORDER BY
      QueryRank,
      RowRank
    ';
    
    PRINT @FinalSQL;
    EXECUTE sp_executesql @FinalSQL, N'@top int', 10;
    

    Basically, every subquery gets these auxiliary columns:

    • QueryRank – a constant value (within the subquery’s result set) derived from [Ordinal];

    • RowRank – a ranking assigned to a row based on the [OrderByPredicate].

    The result sets are UNIONed and then every entry of every unique value is again ranked (ValueRank) based on the query ranking.

    When pulling the final result set, duplicates are suppressed (by the condition ValueRank = 1), and QueryRank and RowRank are used in the ORDER BY clause to preserve the original row order.

    I used EXECUTE sp_executesql @query instead of EXECUTE (@query), because the former allows you to add parameters to the query. In particular, I parametrised the number of results to return (the argument of TOP). But you could certainly concatenate that value into the dynamic script directly, just like other things, if you prefer EXECUTE () over EXECUTE sq_executesql.

    If you like, you can try this query at SQL Fiddle. (Note: the SQL Fiddle version replaces the @TQueries table variable with the TQueries table.)

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

Sidebar

Related Questions

I have a target table containing a items that have an IsActive flag, and
I have a target table TargetTable that has a DECIMAL(20, 7) column. I also
I have two tables: source table result table I have an after update trigger
I hope somebody can help me here, I have a table that has the
I'm using SQL Server 2008 R2. I have a source table of data (I_Vendor)
I have the following scenario where I need to reload a table in SQL
I have some code that disables a trigger on a table, runs an update,
I have a fact table that needs a join to a dimension table however
Is it possible to have collate SQL_Latin1_General_CP1_CS_AS in table variable columns' definitions? The reason
I have C# .NET code with a DataSet (System.Data.DataSet) containing one table of data,

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.