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);