2

从团队表中删除任何团队时,我需要检查 teamId 在其他表中是否可用。为此,我使用以下查询:-

        select  count(TeamId) from 
        ( select TeamId from Project union all 
          select  TeamId from TeamMember union all 
          select  TeamId from Department union all 
          select TeamId from Role   union all 
        ) as Id where TeamId = 1

此查询工作正常,并返回计数。

但现在我需要该表及其列的名称,我在其中找到了该 teamId。

如果可能的话,请给我正确的解决方案。

提前致谢..

4

2 回答 2

1
 select 'Project' tableName from Project
 where TeamId = 1
 union all 
 select 'TeamMember' from TeamMember
 where TeamId = 1
 union all 
 select 'Department' from Department
 where TeamId = 1
 union all 
 select 'Role' from Role
 where TeamId = 1

我不明白为什么您需要找到它的“列”,因为在所有情况下您都列出了“TeamId”。如果它在不同的表中被命名为不同的东西,那么你可以使用这个:

 select 'Project' tableName, 'TeamId' columnName from Project
 where TeamId = 1
 union all 
 select 'TeamMember', 'Member_TeamId' from TeamMember
 where Member_TeamId = 1

最后,如果您想要“行”(记录),您可以将 ID 添加到列中:

 select 'Project' tableName, ProjectID idInTable from Project
 where TeamId = 1
 union all 
 select 'TeamMember', TeamMemberId from TeamMember
 where TeamId = 1
于 2012-10-01T06:24:44.173 回答
1
select * from 
        ( select 'Project'    as table_name, TeamId from Project    union all 
          select 'TeamMember' as table_name, TeamId from TeamMember union all 
          select 'Department' as table_name, TeamId from Department union all 
          select 'Role'       as table_name, TeamId from Role    
        ) Id 
where TeamId = 1
于 2012-10-01T06:26:10.387 回答