在Teradata中查询运行结果

时间:2015-08-05 11:28:23

标签: teradata

我在Teradata编辑器中运行以下查询:

SELECT 'EMP_INFO_MAIN' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE1 UNION ALL
SELECT 'EMP_SAL' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE2 UNION ALL
SELECT 'DEPARTMENT_INFO' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE3;

查询给出了以下结果:

TABLE_NAME  | RECORD_COUNT
------------|------------- 
EMP_INFO    | 10 
EMP_SAL     | 11 
DEPARTME    | 110 

第一列未显示完整的表名。 有人可以帮忙吗?

1 个答案:

答案 0 :(得分:2)

这是一个常见问题。

在Teradata中,UNION的第一个SELECT确定结果数据类型和列名称,因此要么将SELECT的顺序更改为以最长名称开头,要么在第一个SELECT中添加CAST:

SELECT CAST('EMP_INFO_MAIN' AS VARCHAR(20)) as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE1 UNION ALL
SELECT 'EMP_SAL' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE2 UNION ALL
SELECT 'DEPARTMENT_INFO' as TABLE_NAME, COUNT(1) as RECORD_COUNT FROM SCHEMA.TABLE3;