有人可以解释一下为什么这个脚本返回’some_word’而不是错误,因为没有存在的模式试图从schema_that_doesnt_exist.tab中检索数据?
with tab as ( select 'some_word' str from dual ) select * from schema_that_doesnt_exist.tab;
有关此问题的Oracle文档中的某些URL也会对我有所帮助.
解决方法
我猜它与
qualified names bypass with有联系:
SQLite Demo – 没有这样的表:schema_that_doesnt_exists.tab
PostgreSQL Demo – 关系“schema_that_doesnt_exists.tab”不存在
SQLServer Demo – 无效的对象名称’schema_that_doesnt_exists.tab’.
与…一样:
图片来自:https://modern-sql.com/blog/2018-04/mysql-8.0
无论如何,当您需要为数据库单元测试(只读查询)模拟一些数据时,它可能很有用.
例如:
SELECT * FROM schema.table_name -- here goes real data (lots of records) WHERE col = 'sth';
如果我想为测试准备输入数据集,我必须使用实际数据.
使用WITH我可以将其重写为:
WITH table_name AS ( SELECT 'sth' AS col,... FROM dual UNION ALL SELECT 'sth2' AS col,... FROM dual... ) SELECT * FROM schema.table_name -- cte is closer and data is taken from it WHERE col = 'sth';