sql - CASE when EXISTS (SELECT ...) seems to always returns true
问题描述
I need to test if a table exists. If it exists, I need to get the rowcount of that table, else I need to set the rowcount value to NULL.
Here is a snippet of the query I'am using :
SELECT
CASE
-- TABLE A does not exists
WHEN EXISTS (Select TABLE_NAME from ALL_TABLES where TABLE_NAME ='A')
THEN (SELECT COUNT(*) FROM SYS."A")
ELSE NULL
END AS TABLE_count
FROM dual;
When executing this query, I get the following error message :
SQL Error [942] [42000]: ORA-00942: table or view does not exist
It seems that even if the EXISTS returns 0 rows, the THEN clause is always executed instead of going into the ELSE clause.
I don't understand the behavior here. Any idea ?
Thanks
解决方案
您不能在纯 sql 中执行此操作,因为查询是作为一个整体解析的,包括部分SELECT COUNT(*) FROM SYS."A"
所以如果表SYS."A"
不存在,则整个查询解析失败。
另一种方法是使用 pl/sql。如果表存在,则创建一个计算行数的函数,如果不存在,则返回 null。
考虑以下示例:
create or replace function get_table_count(sTableName varchar2)
return number
is
iCount number;
begin
for c in (
select null
from ALL_TABLES
where TABLE_NAME = sTableName
) loop
execute immediate 'select count(*) from '||sTableName into iCount;
return iCount;
end loop;
return null;
end;
/
select get_table_count('DUAL') c1, get_table_count('ABSENT_TABLE') c2 from dual
返回
C1 C2
1 (null)
推荐阅读
- c# - 将基于任务的 C# 示例转换为 VB.Net 的问题
- dart - ScopedModelDescendant 字符串返回 null。不检索状态
- python - 使用类、函数和方法变量
- java - 如何在不更改清单指定主类的情况下修复启动 .jar 文件?
- sharepoint - 从表单获取字段值并传递到 Web 部件列表视图筛选器
- mocha.js - 无法使用赛普拉斯中的 mocha-allure-reporter 读取未定义错误的属性“基础”
- serverless - 尝试部署时连接到不同的 ARN/Role/Amazon 账户
- php - php文件名中的通配符
- python - 由于串口打开情况,无法从 arduino 和无线调制解调器读取串行数据
- c# - 需要更新列表项,然后将其放入文本文件中