如何获取表名并将其附加到查询中?

时间:2014-05-13 23:06:38

标签: sql ms-access

我正在开发一个访问数据库,它有多个具有相似数据的表。除了识别每个记录来自哪个表之外,我想查询相似的字段。

这就是我到目前为止,我有多个联盟在多个表上追加相同的字段,但我遇到问题的部分是添加标识表名的列。

请帮忙!非常感谢!

SELECT DISTINCT SAP_charger.[Object type] FROM SAP_charger 
UNION
SELECT DISTINCT SAP_DME.[Object type] FROM SAP_DME
UNION 
SELECT DISTINCT SAP_fiber_optic_repeater.[Object type] FROM SAP_fiber_optic_repeater
UNION
SELECT DISTINCT SAP_gateway.[Object type] FROM SAP_gateway 
UNION
SELECT DISTINCT SAP_HMI.[Object type] FROM SAP_HMI 
UNION
SELECT DISTINCT SAP_managed_switch.[Object type] FROM SAP_managed_switch
UNION 
SELECT DISTINCT SAP_media_converter.[Object type] FROM SAP_media_converter
UNION
SELECT DISTINCT SAP_meter.[Object type] FROM SAP_meter 
UNION
SELECT DISTINCT SAP_OLM.[Object type] FROM SAP_OLM 
UNION
SELECT DISTINCT SAP_PLC.[Object type] FROM SAP_PLC 
UNION
SELECT DISTINCT SAP_relay.[Object type] FROM SAP_relay 
UNION
SELECT DISTINCT SAP_satellite_clock.[Object type] FROM SAP_satellite_clock 
UNION
SELECT DISTINCT SAP_terminal_server.[Object type] FROM SAP_terminal_server 
INNER JOIN
SELECT MSysObjects.Name AS table_name
FROM MSysObjects 
WHERE table_name in ( 'SAP_charger', 'SAP_DME', 'SAP_fiber_optic_repeater', 'SAP_gateway', ' SAP_HMI', 'SAP_managed_switch', 'SAP_media_converter', 'SAP_meter', 'SAP_OLM', 'SAP_PLC', 'SAP_relay', 'SAP_satellite_clock', 'SAP_terminal_server')
;

1 个答案:

答案 0 :(得分:2)

这样的事情

SELECT DISTINCT SAP_charger.[Object type],'SAP_charger' FROM SAP_charger 
UNION
SELECT DISTINCT SAP_DME.[Object type],'SAP_DME' FROM SAP_DME
.....