Using the same table twice in a select returns duplicate rows I'm currently trying to put together something that will tell me the name of two important tables each of our customers use. Sadly, the names of these tables are not always the same, so I've currently come up with the following T-SQL;

create table #test  (   [Database] sysname,
                                    [TenantId] sysname,
                                    [TasksTable] sysname,
                                    [TimeRecordTable] sysname   )

insert into #test
select      db_name() as [Database], 
                SCHEMA_NAME(tbl.schema_id) as [TenantId], 
                tbl.name AS [TasksTable],
                tbl2.name AS [TimeRecordTable]
from    sys.tables tbl, 
            sys.tables tbl2
where tbl.name IN ('Tasks', 'Task', 'Tasks_NEW', 'Task_NEW') 
and     tbl2.name IN ('TaskTimeRecord','TaskTimeRecord_NEW')
and     SCHEMA_NAME(tbl.schema_id) = '2'

select * from #test
drop table #test


When running this, I'm getting over 200 rows of duplicated data, instead of a single line that includes the results I'm looking for. 

Does anyone have any suggestions on how to get this working?
You could use a subquery

SELECT DB_NAME() as DBName,
       SCHEMA_NAME(t.schema_id) as SchemaName,
       t.name as TaskTable,
       (SELECT t2.Name from sys.tables t2 where t2.name in ('TaskTimeRecord_NEW','TaskTimeRecord') and t2.schema_id = t.schema_id) as TimeRecordTable
FROM sys.tables t
WHERE  t.name IN ('Tasks', 'Task', 'Tasks_NEW', 'Task_NEW');


However, if there is more than one tablename returned by the subquery, the query will fail with the following error:


  Subquery returned more than 1 value. This is not permitted when the
  subquery follows =, !=, &lt;, &lt;= , >, >= or when the subquery is used as
  an expression.


If more than one tablename could be returned

If the subquery is an issue due to multiple table names, you could create a comma separated string with FOR XML PATH('').

SELECT DB_NAME() as DBName,
       SCHEMA_NAME(t.schema_id) as SchemaName,
       t.name as TaskTable,
       stuff(((SELECT ', '+ t2.Name from sys.tables t2 where t2.name in ('TaskTimeRecord_NEW','TaskTimeRecord') and t2.schema_id = t.schema_id for xml path(''))), 1, 2, '') as TimeRecordTable
FROM sys.tables t
WHERE  t.name IN ('Tasks', 'Task', 'Tasks_NEW', 'Task_NEW');


If both the subquery and the main query could return multiple tablenames

And you still need one row per schema, you could create two comma separated strings

SELECT DISTINCT DB_NAME() as DBName,
       SCHEMA_NAME(t.schema_id) as SchemaName,
       stuff(((SELECT ', '+ t2.Name from sys.tables t2 where t2.name in ('Tasks', 'Task', 'Tasks_NEW', 'Task_NEW') and t2.schema_id = t.schema_id for xml path(''))), 1, 2, '') as TaskTable,
       stuff(((SELECT ', '+ t3.Name from sys.tables t3 where t3.name in ('TaskTimeRecord_NEW','TaskTimeRecord') and t3.schema_id = t.schema_id for xml path(''))), 1, 2, '') as TimeRecordTable
FROM sys.tables t
WHERE  t.name IN ('Tasks', 'Task', 'Tasks_NEW', 'Task_NEW','TaskTimeRecord_NEW','TaskTimeRecord');