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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:51:43+00:00 2026-06-14T13:51:43+00:00

I have a temp table like: #temp321 select * from #temp321 it’s return value

  • 0

I have a temp table like: #temp321

select * from #temp321

it’s return value like

Field1|Field2|Field3|....|FieldN|
--------------------------------
Value1|Value2|Value3|....|ValueN|

This #temp321 table is auto generated from another set of query.So, the number of #temp321 table’s column can be vary(1-25). But their will be always a single row. Now i need to write a where statement using those value.
Example :
where Field1='Value1' and Field2='Value2' and Field3='Value3'.....FieldN='ValueN'..(depends on number of column available into the #temp321 table)
How i do it. Thanks in advance

  • 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-14T13:51:44+00:00Added an answer on June 14, 2026 at 1:51 pm

    You can do this:

    SELECT *
    FROM Othertable t1
    INNER JOIN #temp321 t2 ON  t1.Field1 = t2.field1 
         AND t1.Field2 = t2.field2 
         AND t1.Field3 = t2.field3;
    

    Update: This what I could do:

    JOIN the two tables dynamically instead of the WHERE clause:

    DECLARE @DynamicJOINCondition AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    ;WITH cte
    AS
    (
        SELECT t1.COLUMN_NAME 'T1', t2.COLUMN_NAME 'T2'
        FROM 
        (
           SELECT 
             ordinal_position , 
             column_name
           FROM information_schema.columns 
           WHERE table_name LIKE '#temp321%'
        ) t1
        INNER JOIN
        (
           SELECT 
             ordinal_position , 
             column_name
           FROM information_schema.columns 
           WHERE table_name = 'othertable'
        ) t2 ON t1.ORDINAL_POSITION = t2.ORDINAL_POSITION
    )
    SELECT @DynamicJOINCondition = STUFF((SELECT distinct ' AND ' +  
                         ' t1.' + T1 + ' = ' + 't2.' + T2
                   FROM cte
                   FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)'), 4,1,'');
    
    -- Remove the first AND
    SELECT @DynamicJOINCondition = RIGHT(@DynamicJOINCondition, 
                                         LEN(@DynamicJOINCondition) - 4);
    
    SET @query = 'SELECT COUNT(*) ' +
                 ' FROM  #temp321 t1 INNER JOIN Othertable t2 ON ' +
                   @DynamicJOINCondition;
    
    EXECUTE(@query);
    

    How this works?

    First I generated the JOIN condition dynamically, by getting the list of the columns names from the temp table and the other list of columns’ names from the other table. Note that it. I used the columns’ ORDINAL_POSITIONs, which is a metadata stored in the table information_schema.columns for each column in the database, to compare the list of columns names from the two tables. For exapmle the column with the ordincary postition 1 will be joined with the column name with the ordinary position 1 from the second table, and so so. So, you have to watch out the temp table columns positions so that the columns listed in the same order of the other columns list of the second table.


    Update2:

    Generate the WHERE clause dynamically:

    If you want to generate the WHERE clause dynamically instead of using JOIN, in this case it will need more work. Like so:

    DECLARE @DynamicWHERECondition AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    
    ;WITH cte
    AS
    (
        SELECT t1.COLUMN_NAME 'T1', t2.COLUMN_NAME 'T2'
        FROM 
        (
           SELECT
             ordinal_position, 
             column_name
           FROM information_schema.columns 
           WHERE table_name LIKE '#temp321%'
        ) t1
        INNER JOIN
        (
          SELECT 
            ordinal_position, 
            column_name
          FROM information_schema.columns 
          WHERE table_name = 'othertable'
        ) t2 ON t1.ORDINAL_POSITION = t2.ORDINAL_POSITION
    ), cte2 
    AS
    (
        SELECT t2, fieldvalue
        FROM cte t1
        INNER JOIN
        (
            SELECT FieldName, fieldvalue
            FROM
            (
                SELECT field1, field2, field3, field4
                FROM #temp321
            ) p
            UNPIVOT
            (
                fieldvalue FOR FieldName IN (field1, field2, field3, field4)
            ) AS u
        ) t2 ON t1.T2 = t2.FieldName
    ) 
    SELECT @DynamicWHERECondition = STUFF((SELECT distinct ' AND ' +  
                          T2 + ' = ' + '''' + fieldvalue + '''' 
                   FROM cte2
                   FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)'), 4,1,'');
    
    SELECT @DynamicWHERECondition = RIGHT(@DynamicWHERECondition, LEN(@DynamicWHERECondition) - 4);
    SET @query = 'SELECT COUNT(*) ' +
                 ' FROM Othertable t2 WHERE ' + @DynamicWHERECondition;
    
    EXECUTE(@query);
    

    How this works?

    Since the temp table contains only one rows with the values that will be used to form the dynamically created WHERE clause, I UNPIVOTed this one rows into two columns: fieldname: field1, field2, ... and fieldvalue: value1, value2, ....

    Later I joined this unpivoted columns with the two tables I used with my first query:

        SELECT
          ordinal_position, 
          column_name
        FROM information_schema.columns 
        WHERE table_name LIKE '#temp321%';
    

    and:

        SELECT 
          ordinal_position, 
          column_name
        FROM information_schema.columns 
        WHERE table_name = 'othertable';
    

    With the join condition be the same as the condition in the first case, by the ordinal positions of the columns in the two tables.


    Generate the WHERE clause dynamically, with dynamically UNPIVOTing the temp tables’ values:

    But the previous approach has a big problem. The selection of
    You need to unpivot the table temp dynamically like so:

    SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(column_name)
                      FROM information_schema.columns 
                      WHERE table_name LIKE '#temp321%'
                      FOR XML PATH(''), TYPE
                      ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
    
    SET @dynamicunpivotquery = ' SELECT FieldName, fieldvalue ' +
                               ' FROM ' +
                               ' ( SELECT * FROM #temp321 ' +
                               ' ) p ' +
                               ' UNPIVOT ' +
                               ' ( ' + 
                               '   fieldvalue FOR FieldName IN (' + @cols + ' ) ' +
                               ' ) AS u ';
    

    But, in order to use the dynamically unpivoted table later in our query, you have to create a temp table:

    DECLARE @unpivotedTable TABLE(FieldName varchar(50), fieldvalue VARCHAR(50));
    INSERT INTO  @unpivotedTable
    exec(@dynamicunpivotquery);
    

    So that you could use it like so:

    SELECT t2, fieldvalue
    FROM cte t1
    INNER JOIN  @unpivotedTable t2 ON t1.T2 = t2.FieldName
    

    Here is the updated sql with dynamic unpivot:

    DECLARE @DynamicWHERECondition AS NVARCHAR(MAX);
    DECLARE @cols AS NVARCHAR(MAX);
    DECLARE @dynamicunpivotquery AS NVARCHAR(MAX);
    DECLARE @query AS NVARCHAR(MAX);
    DECLARE @unpivotedTable TABLE(FieldName varchar(50), fieldvalue VARCHAR(50));
    
    SELECT @cols = STUFF((SELECT DISTINCT ',' + QUOTENAME(column_name)
                      FROM information_schema.columns 
                      WHERE table_name LIKE '#temp321%'
                      FOR XML PATH(''), TYPE
                      ).value('.', 'NVARCHAR(MAX)'), 1, 1, '');
    
    SET @dynamicunpivotquery = ' SELECT FieldName, fieldvalue ' +
                               ' FROM ' +
                               ' ( SELECT * FROM #temp321 ' +
                               ' ) p ' +
                               ' UNPIVOT ' +
                               ' ( ' + 
                               '   fieldvalue FOR FieldName IN (' + @cols + ' ) ' +
                               ' ) AS u ';
    
    INSERT INTO  @unpivotedTable
    exec(@dynamicunpivotquery);
    
    ;WITH cte
    AS
    (
        SELECT t1.COLUMN_NAME 'T1', t2.COLUMN_NAME 'T2'
        FROM 
        (
           SELECT 
             ordinal_position, 
             column_name
           FROM information_schema.columns 
           WHERE table_name LIKE '#temp321%'
        ) t1
        INNER JOIN
        (
          SELECT 
            ordinal_position, 
            column_name 
          FROM information_schema.columns 
          WHERE table_name = 'othertable'
        ) t2 ON t1.ORDINAL_POSITION = t2.ORDINAL_POSITION
    ), cte2 
    AS
    (
        SELECT t2, fieldvalue
        FROM cte t1
        INNER JOIN  @unpivotedTable t2 ON t1.T2 = t2.FieldName
    ) 
    SELECT @DynamicWHERECondition = STUFF((SELECT distinct ' AND ' +  
                          T2 + ' = ' + '''' + fieldvalue + '''' 
                   FROM cte2
                   FOR XML PATH(''), TYPE
                ).value('.', 'NVARCHAR(MAX)'), 4,1,'');
    
    SELECT @DynamicWHERECondition = RIGHT(@DynamicWHERECondition, 
                                          LEN(@DynamicWHERECondition) - 4);
    SET @query = 'SELECT COUNT(*) ' +
                 ' FROM Othertable t2 WHERE ' + @DynamicWHERECondition;
    
    EXECUTE(@query);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a temp table and i want to map data from it like
So I have this temp table that has structure like: col1 col2 col3 col3
I have a temp table that stores rollup data and would like to create
I have a test table like this CREATE TABLE #temp ( rid INT IDENTITY
I have created a table like this: CREATE TABLE #TEMP(RecordDate datetime, First VARCHAR(255), Last
I have a temp table set up like this: Type Rate TotalCost ---- ----
I have a string like following: CREATE GLOBAL TEMPORARY TABLE some_temp_table_name or CREATE GLOBAL
I have a sample file like the following: CREATE GLOBAL TEMPORARY TABLE tt_temp_user_11 (
I have temp table #xml that have 2 columns: CREATE TABLE #xml ( id
I have a simple temp-table defined in SQL Server 2008 R2 representing a parent-child

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.