Use dbms_sql.bind_variable, dbms_sql.execute to insert value to a table

Thursday, July 23rd, 2009

 
SQL>
SQL>
SQL> create table foo (
  2         a   integer,
  3         b   dec( 9, 2 ),
  4         c   character varying( 30 ),
  5         d   national char( 3 )
  6      )
  7  /

SQL>
SQL>      declare
  2        l_cursor number := dbms_sql.open_cursor;
  3        l_ignore number;
  4      begin
  5        dbms_sql.parse( l_cursor,‘insert into foo values ( :n, :c )‘,dbms_sql.native );
  6        dbms_sql.bind_variable( l_cursor, ‘:N’, 1 );
  7        dbms_sql.bind_variable( l_cursor, ‘:C’, ’Chris‘ );
  8        l_ignore := dbms_sql.execute( l_cursor );
  9        dbms_sql.bind_variable( l_cursor, ‘:N’, 2 );
 10        dbms_sql.bind_variable( l_cursor, ‘:C’, ’Sean‘ );
 11        l_ignore := dbms_sql.execute( l_cursor );
 12        dbms_sql.close_cursor( l_cursor );
 13      end;
 14      /

PL/SQL procedure successfully completed.

SQL>      select * from foo;
         N
———-
V
—————————————————————————————————-
         1
Chris

         2
Sean

2 rows selected.

SQL>
SQL> drop table foo;

Table dropped.

SQL> –

 

Call dbms_random.normal to get a random number

Sunday, July 19th, 2009

 
SQL>
SQL>
SQL> begin
  2        for i in 1 .. 10 loop
  3          dbms_output.put_line( dbms_random.normal );
  4        end loop;
  5      end;
  6      /
1.54614544732963133287654886045271895089
.4317388077546154281139738992526089097407
-.3932262942586239869251307222842090454052
-1.6670328488404622986050198363653775926
.6688556901515673452664296942201359807998
1.33788099021421741537616822012046180008
.4640589645668300518621566089428065420796
-.2593943569301190968371056213308217873704
-.928080380810723360417809948629051042821
.3610354349426925924563975682781230663073

PL/SQL procedure successfully completed.

SQL> –

 

utl_raw.cast_from_number

Saturday, July 4th, 2009

 
SQL>
SQL>  select utl_raw.cast_from_number( 123.45 ) from dual;
UTL_RAW.CAST_FROM_NUMBER(123.45)
—————————————————————————————————-
C202182E

1 row selected.

SQL> –

 

DBMS_LOB.SUBSTR: Select the first 50 characters of clob_col, and the first 25 bytes of blob_col, for each row.

Thursday, July 2nd, 2009

 
SQL>
SQL>
SQL> CREATE TABLE lobdemo (
  2    key NUMBER,
  3    clob_col CLOB,
  4    blob_col BLOB);

Table created.

SQL>
SQL>
SQL> column CLOB_Data format a60
SQL> column BLOB_Data format a60
SQL> SELECT key,
  2         DBMS_LOB.SUBSTR(clob_col, 50) CLOB_Data,
  3         DBMS_LOB.SUBSTR(blob_col, 25) BLOB_Data
  4    FROM lobdemo
  5    WHERE key IN (0, 100, 101, 102, 103, 1000, 1001, 1002)
  6    ORDER BY key;

no rows selected

SQL>
SQL> drop table lobdemo;

Table dropped.

SQL>