0

我有两张桌子

学生表

Id | Studentname | Adress

教师表

TID | TeacherName | Adress

studentTable表中我有列Id并且在teacherTable我有列中TID,在使用动态查询时如何选择条目而不考虑列名。

select ID or PID from @Tablename 

不起作用,我该怎么做,有什么想法吗?

我试过的查询:

SELECT  + '''' +  @TABLE_NAME + '''' + ',' + '''' +  @COLUMN_NAME + '''' + ',' + 'ID  + 
        ' FROM [' + @TABLE_NAME       
4

1 回答 1

1

您不需要动态查询,那会很慢。

这是你如何做的,两个查询结合在一起。

SELECT 'student' as [type], ID as [ID], studentname as name, address
from studentTable
where ID = @inID

union all

SELECT 'teacher' as [type], TID as [ID], teachername as name, address
from teacherTable
where TID = @inID
于 2013-10-28T14:45:01.427 回答