语法错误:解析错误,在创建回归测试时检查空表

时间:2015-05-29 10:02:39

标签: sql teradata

我正在创建一个回归测试查询,该查询将检查表是否已填充。这是通过获取每个表A,B,C中的第一行来完成的,如果它的id存在,它将得到值“Y”。如果不是则值“N”。我创建了下面的代码来执行以下操作,但我收到语法错误:解析错误。有人可以告诉我出了什么问题吗?

SELECT 

Case
    When  EXISTS( A.Id) THEN 'Y'
    ELSE 'N'
END AS A_exist,

Case
    When  EXISTS( B.Id) THEN 'Y'
    ELSE 'N'
END AS B_exist,

Case
    When  EXISTS( C.Id) THEN 'Y'
    ELSE 'N'
END AS C_exist

FROM
(select top 1 * from Table_A) A
(select top 1 * from Table_B) B
(select top 1 * from Table_C) C

3 个答案:

答案 0 :(得分:0)

你使用sql-server吗?这有用吗?

SELECT 

Case
    When  EXISTS(select * from Table_A) THEN 'Y'
    ELSE 'N'
END AS A_exist,

Case
    When  EXISTS(select * from Table_B) THEN 'Y'
    ELSE 'N'
END AS B_exist,

Case
    When  EXISTS(select * from Table_C) THEN 'Y'
    ELSE 'N'
END AS C_exist

答案 1 :(得分:0)

您可以使用以下查询。

SELECT 

Case
    When   A.Id IS NOT NULL THEN 'Y'
    ELSE 'N'
END AS A_exist,

Case
    When  B.Id IS NOT NULL THEN 'Y'
    ELSE 'N'
END AS B_exist,

Case
    When  C.Id IS NOT NULL THEN 'Y'
    ELSE 'N'
END AS C_exist

FROM
(select top 1 * from Table_A) A,
(select top 1 * from Table_B) B,
(select top 1 * from Table_C) C
;

答案 2 :(得分:0)

您可以使用COUNT

为空表强制执行一行
SELECT
   CASE WHEN a.CNT = 1 THEN 'Y' ELSE 'N' END AS A_exist,
   CASE WHEN b.CNT = 1 THEN 'Y' ELSE 'N' END AS B_exist,
   CASE WHEN c.CNT = 1 THEN 'Y' ELSE 'N' END AS C_exist
FROM
 (
   SELECT COUNT(*) AS CNT
   WHERE EXISTS(SELECT  * FROM Table_A) 
 ) AS a,
 (
   SELECT COUNT(*) AS CNT
   WHERE EXISTS(SELECT  * FROM Table_B) 
 ) AS b,
 (
   SELECT COUNT(*) AS CNT
   WHERE EXISTS(SELECT  * FROM Table_C)
 ) AS c;

但为什么不为每个表而不是一列返回一行?它更容易扩展:

SELECT 'Table_A' AS "Table", CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END AS "Exists?"
WHERE EXISTS(SELECT * FROM Table_A) 
UNION ALL
SELECT 'Table_B', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END
WHERE EXISTS(SELECT * FROM Table_B) 
UNION ALL
SELECT 'Table_C', CASE WHEN COUNT(*) = 1 THEN 'Y' ELSE 'N' END
WHERE EXISTS(SELECT * FROM Table_C)