Oracle中特殊的INSERT语句

摘要:
---------------------------------------插入表名称选择…-----------------------------------------插入表名称值(coL值,…默认值)-----------------------------------------创建

---------------------------------------
insert into table-name select ...

---------------------------------------
insert into table-name values(coL-values, ... default)

---------------------------------------
CREATE TABLE raises (emp_id NUMBER, sal NUMBER
   CONSTRAINT check_sal CHECK(sal > 8000));

EXECUTE DBMS_ERRLOG.CREATE_ERROR_LOG('raises', 'errlog');

INSERT INTO raises
   SELECT employee_id, salary*1.1 FROM employees
   WHERE commission_pct > .2
   LOG ERRORS INTO errlog ('my_bad') REJECT LIMIT 10;

SELECT ORA_ERR_MESG$, ORA_ERR_TAG$, emp_id, sal FROM errlog;

ORA_ERR_MESG$               ORA_ERR_TAG$         EMP_ID SAL
--------------------------- -------------------- ------ -------
ORA-02290: check constraint my_bad               161    7700
 (HR.SYS_C004266) violated

-----------------------------------------
The multitable insert statement looks like this:

INSERT ALL
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date, sales_sun)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+1, sales_mon)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+2, sales_tue)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+3, sales_wed)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+4, sales_thu)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+5, sales_fri)
      INTO sales (prod_id, cust_id, time_id, amount)
      VALUES (product_id, customer_id, weekly_start_date+6, sales_sat)
   SELECT product_id, customer_id, weekly_start_date, sales_sun,
      sales_mon, sales_tue, sales_wed, sales_thu, sales_fri, sales_sat
      FROM sales_input_table;

INSERT ALL
   WHEN order_total < 100000 THEN
      INTO small_orders
   WHEN order_total > 100000 AND order_total < 200000 THEN
      INTO medium_orders
   ELSE
      INTO large_orders
   SELECT order_id, order_total, sales_rep_id, customer_id
      FROM orders;

INSERT ALL
   WHEN order_total < 1000000 THEN
      INTO small_orders
   WHEN order_total > 1000000 AND order_total < 2000000 THEN
      INTO medium_orders
   WHEN order_total > 2000000 THEN
      INTO large_orders
   SELECT order_id, order_total, sales_rep_id, customer_id
      FROM orders;

INSERT FIRST
   WHEN ottl < 100000 THEN
      INTO small_orders
         VALUES(oid, ottl, sid, cid)
   WHEN ottl > 100000 and ottl < 200000 THEN
      INTO medium_orders
         VALUES(oid, ottl, sid, cid)
   WHEN ottl > 290000 THEN
      INTO special_orders
   WHEN ottl > 200000 THEN
      INTO large_orders
         VALUES(oid, ottl, sid, cid)
   SELECT o.order_id oid, o.customer_id cid, o.order_total ottl,
      o.sales_rep_id sid, c.credit_limit cl, c.cust_email cem
      FROM orders o, customers c
      WHERE o.customer_id = c.customer_id;


Ref: http://www.dba-oracle.com/oracle_news/2005_5_9_converting_long_lob_data_types.htm

Using TO_LOB one can easily convert LONGS to LOBS.
Using the same conversion function one can convert LONG into CLOB and LONGRAW TO BLOB.

Have a look into following example

SQL>create table tlong(itemcd number(30),itemdesc long);
/

Table created.

SQL>Create table tlob(ItemCd Number(30),Itemdesc clob);
Table created

Now dump some values from some table into table tlong
SQL>insert into tlong select icode,iname from InvTab;
2000 rows created.

Now try to insert into lob table from long table
SQL>Insert into tlob select itemcd,TO_LOB(itemdesc) from tlong
2000 rows created.

but in PL/SQL you need to handle different way
normal select into insert will not work,but excute immediate will be the workaround in oracle 8.1.7

Let's see how it can be done PL/SQL
begin
insert into tlob select itemcd,TO_LOB(itemdesc) from tlong;
end;
/

the above pl/sql block works well with oralce 9i and oracle 10g but fails in oracle 8.1.7 with the following error
ERROR at line 2:
ORA-06550: line 2, column 33:
PLS-00201: identifier 'TO_LOB' must be declared
ORA-06550: line 2, column 1:
PL/SQL: SQL Statement ignored

Then you should use dynamic sql (Execute Immediate 'SQL statement') as follows
begin
execute immediate 'insert into tlob select itemcd,TO_LOB(itemdesc) from tlong';
end;
/

insert into tbclustertogrid
  (clusterno, rectsmid, rectspec, vcdist_id)
  select '{0}', '{1}', '500', '{2}'
    from dual
   where not exists (select clusterno
            from TBCLUSTERTOGRID
           where clusterno = '{0}'
             and rectsmid = '{1}'
             and vcdist_id = '{2}')

免责声明:文章转载自《Oracle中特殊的INSERT语句》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇3.kubernetes的CNI网络插件-FlannelJavaScript 当月第一天和最后一天下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

oracle恢复删除的数据

分为两种方法:scn和时间戳两种方法恢复。 一、通过scn恢复删除且已提交的数据   1、获得当前数据库的scn号     select current_scn from v$database; (切换到sys用户或system用户查询)      查询到的scn号为:1499223   2、查询当前scn号之前的scn     select * from...

[MySQL 5.6] 初识5.6的optimizer trace

在MySQL5.6中,支持将执行的SQL的查询计划树记录下来,目前来看,即使对于非常简单的查询,也会打印出冗长的查询计划,看起来似乎不是很可读,不过对于一个经验丰富,对查询计划的生成过程比较了解的DBA而言,这是一个优化SQL的宝藏,因为暴露了大量的内部产生查询计划的信息给用户,这意味着,我们可以对开销较大的部分进行优化。 新参数optimizer_t...

SQL 查询所有数据库、表名、表字段总结

SQL Server1、查询所有表    select [id], [name] from [sysobjects] where [type] = 'u' order by [name]2、查询所有数据库    select [name] from [sysdatabases] order by [name]3、查询表中字段     select [nam...

Mysql 合并结果接横向拼接字段

近日在做一个报表功能里面有一个这样的需求是统计各部门在某一月入职和离职的人数 我的步骤是这样先查出入职的人数关键sql如下: SELECT dept ,COUNT(1) rcNumber FROM 员工表 WHERE ( 入职时间 != '' OR 入职时间 IS NOT NULL) and DATE_FORMAT(入职时间...

oracle之数据限定与排序

数据限定与排序6.1 简单查询语句执行顺序from, where, group by, having, order by, selectwhere限定from后面的表或视图,限定的选项只能是表的列或列单行函数或列表达式,where后不可以直接使用分组函数SQL> select empno,job from emp where sal>2000;...

antd Table 可伸缩列没有效果

把antd 中的Table可伸缩示例代码,下载到自己的代码中发现,鼠标放到表格的边框上,并没有出现可伸缩鼠标样式,最后的解决方法是在css样式中添加下面的样式,就解决问题了。 .react-resizable { position: relative; background-clip: padding-box; } .r...