栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

从不存在的表中选择

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

从不存在的表中选择

问题的确切原因在于,您的过程没有被编译,因为它引用了不存在的对象。为此,您可能需要一些动态SQL;例如:

create or replace procedure checkTable is    vCheckExists  number;    vNum          number;begin    -- check if the table exists    select count(1)    into vCheckExists    from user_tables    where table_name = 'NON_EXISTING_TABLE';    --     if vCheckExists = 1 then        -- query the table with dynamic SQL        execute immediate 'select count(1) from NON_EXISTING_TABLE'        into vNum;    else        vNum := -1;    end if;    dbms_output.put_line(vNum);end;

即使表不存在,该过程也会编译;如果立即致电,您将获得:

SQL> select count(1) from NON_EXISTING_TABLE;select count(1) from NON_EXISTING_TABLE          *ERROR at line 1:ORA-00942: table or view does not existSQL> exec checkTable;-1PL/SQL procedure successfully completed.

然后,如果您创建表并再次调用该过程:

SQL> create table NON_EXISTING_TABLE(a) as select 1 from dual;Table created.SQL> exec checkTable;1PL/SQL procedure successfully completed.

与我显示的方式相同

SELECt
,您可以执行所需的
UPDATE
SQL查询;如果您执行的操作不同于
SELECT
,则
INTO
必须删除该子句。

例如,假设您需要插入另一个表中,则应以这种方式编辑上面的代码:

if vCheckExists = 1 then    execute immediate 'insert into target(a, b, c) select a, 1, 100 from NON_EXISTING_TABLE';end if;


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/517522.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号