SQL> show parameter sga NAME TYPE VALUE ------------------------------------ ------------ ------------------------------ sga_max_size big integer 1000M sga_target big integer 1000M … SQL> ALTER SYSTEM SET SGA_TARGET = 'sga_max_size*80/100'; システムが変更されました。 SQL> show parameter sga_target NAME TYPE VALUE ------------------------------------ ------------ ------------------------------ sga_target big integer 800M 実行例:SGA_TARGET パラメータを SGA_MAX_SIZE の 80% を指定して設定 ※設定対象となっているパラメータを計算式に埋め込むのはNG SQL> ALTER SYSTEM SET SGA_TARGET=‘SGA_TARGET * 80/100’; ALTER SYSTEM SET SGA_TARGET=‘SGA_TARGET * 80/100’ * ERROR at line 1: ORA-00922: missing or invalid option
its affiliates 9 CREATE USER :!username IDENTIFIED BY :!password DEFAULT TABLESPACE example QUOTA 10M ON example PROFILE app_user PASSWORD EXPIRE; OCIプレースホルダを使用したDDL文 OCIStmtPlaceholderSubstitute(stmthp, "username", strlen("username"), "scott", strlen("scott"), OCI_DEFAULT); OCIStmtPlaceholderSubstitute()関数の例
affiliates 14 SELECT * FROM row_sampler(t=>sh.customer, pct=>15); create or replace function row_sampler (t dbms_tf.table_t, pct number default 5) return varchar2 SQL_MACRO(TABLE) as begin return q'{select * from t order by dbms_random.value fetch first row_sampler.pct percent rows only}'; end; /
its affiliates 23 SQL> SELECT trades.acno, trades.tday, SUM (agg.suma) OVER W ※ FROM trades, (SELECT acno, tday, SUM(amount) AS suma FROM trades WHERE ttype = 'buy' GROUP BY acno, tday ) agg WHERE trades.acno = agg.acno AND trades.tday = agg.tday AND trades.ttype = 'buy' WINDOW W AS (PARTITION BY trades.acno ORDER BY trades.tday ROWS BETWEEN 4 PRECEDING AND CURRENT ROW); ※"W" は、末尾の "WINDOWS W AS ..." の W を指す 実行例:
2021, Oracle and/or its affiliates 34 SQL> set serveroutput on SQL> BEGIN 2 FOR n NUMBER(5,1) IN 1.0 .. 3.0 BY 0.5 LOOP 3 DBMS_OUTPUT.PUT_LINE(n); 4 END LOOP; 5 END; 6 / 1 1.5 2 2.5 3
Oracle and/or its affiliates 36 SQL> set serveroutput on SQL> DECLARE 2 TYPE intvec_t IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER; 3 vec intvec_t := intvec_t(3 => 10, 1 => 11, 100 => 34); 4 BEGIN 5 FOR i IN VALUES OF vec LOOP 6 DBMS_OUTPUT.PUT_LINE(i); 7 END LOOP; 8 END; 9 / 11 10 34
Oracle and/or its affiliates 37 SQL> set serveroutput on SQL> DECLARE 2 TYPE intvec_t IS TABLE OF PLS_INTEGER INDEX BY PLS_INTEGER; 3 vec intvec_t := intvec_t(3 => 10, 1 => 11, 100 => 34); 4 BEGIN 5 FOR i IN INDICES OF vec LOOP 6 DBMS_OUTPUT.PUT_LINE(i); 7 END LOOP; 8 END; 9 / 1 3 100
its affiliates 39 SQL> DECLARE 2 cursor_str VARCHAR2(500) := 'SELECT last_name, employee_id FROM hr.employees ORDER BY last_name'; 3 TYPE rec_t IS RECORD (last_name VARCHAR2(25), 4 employee_id NUMBER); 5 BEGIN 6 FOR r rec_t IN VALUES OF (EXECUTE IMMEDIATE cursor_str) WHEN r.employee_id < 103 LOOP 7 DBMS_OUTPUT.PUT_LINE(r.last_name || ', ' || r.employee_id); 8 END LOOP; 9 END; 10 / De Haan, 102 King, 100 Kochhar, 101
43 SQL> declare 2 type vec is table of pls_integer index by pls_integer; 3 result vec; 4 begin 5 result := vec(FOR i IN 1..5 => 2*i); 6 for i in result.first .. result.last loop 7 dbms_output.put_line(result(i)); 8 end loop; 9 end; 10 / 2 4 6 8 10 この例では、 1~5をそれぞれ2倍した値を コレクション “result” に格納
SQL> declare 2 type vec is table of varchar2(3); 3 result vec; 4 begin 5 result := vec(for i in 10..21 by 2 index i/2 => i); 6 for i in result.first .. result.last loop 7 dbms_output.put_line(result(i)); 8 end loop; 9 end; 10 / 10 12 14 16 18 20 この例では、 10~21を2ずつカウントアップし、そのうち の偶数値のみをコレクション “result” に格納
45 SQL> declare 2 type vec is table of pls_integer index by pls_integer; 3 result vec; 4 v1 vec :=vec(for i in 1..3 => i); 5 v2 vec :=vec(for i in 4..6 => i); 6 begin 7 result := vec(for v in values of v1, reverse values of v2 sequence => v); 8 for i in result.first .. result.last loop 9 dbms_output.put_line(result(i)); 10 end loop; 11 end; 12 / 1 2 3 6 5 4 この例では、下記2つのコレクション結合結果を result に格納 ・ 1,2,3が格納されているコレクション v1 ・4,5,6が格納されているコレクション v2の逆順
47 SQL> CREATE TYPE objplsint AS OBJECT (I PLS_INTEGER) NOT PERSISTABLE; 2 / Type created. 例2:BOOREAN 型のユーザー定義タイプを作成する SQL> CREATE TYPE objbool AS OBJECT (I BOOLEAN) NOT PERSISTABLE; 2 / Type created.
its affiliates 53 拡張データ型 SQLスカラーデータ型 Binary RAW date DATE timestamp TIMESTAMP year-month interval INTERVAL YEAR TO MONTH day-second interval INTERVAL DAY TO SECOND double BINARY-DOUBLE float BINARY_FLOAT
Oracle and/or its affiliates 63 集計の例 with jtab as (select JSON_Array(1,2,3,4,5,6) jcol from dual) select jtab.jcol[*].maxNumber() from jtab jtab; JTAB.JCOL[*].MAXNUMBER() ------------------------ 6 with jtab as (select JSON_Array(1,2,3,4,5,6) jcol from dual) select jtab.jcol[*].sum() from jtab jtab; JTAB.JCOL[*].SUM() ------------------ 21 with jtab as (select JSON_Array(1,2,3,4,5,6) jcol from dual) select jtab.jcol[*].avg() from jtab jtab; JTAB.JCOL[*].AVG() ------------------ 3.5
its affiliates 64 -- JSON型インスタンスの直接比較はエラー SQL> select count(1)from customers c where c.cdata.address.zip > 12345; ORA-40796: invalid comparison operation involving JSON type value -- JSON型インスタンスでGROUP/ORDER BY式はエラー SQL> select count(1)from customers c group by c.cdata.address.zip; ORA-40796: invalid comparison operation involving JSON type value -- 項目メソッドでデータ型変換すれば実行できる SQL> select count(1) from customers c where c.cdata.address.zip.number() > 12345 2 group by c.cdata.address.zip.number(); COUNT(1) ---------- 1
-- 索引の作成 SQL> create multivalue index cust_zip_idx on customers c(c.cdata.address.zip.number()); 索引が作成されました -- 実行計画の取得 set autotrace trace exp -- JSON_Existsを条件にしたSELECT文を実行 ※実行計画は後に記載 SQL> select id from customers c 2 where JSON_Exists(cdata, '$.address?(@.zip.number()==94065)'); ID ---------- 2