Oct 30 2007

Change column width of show parameter in SQL Plus

Category: SQLPlusittichai @ 2:40 am

You can change the width of the NAME, TYPE and VALUE of “show parameter” in SQL/Plus.

SQL> show parameter sga

NAME                           TYPE
------------------------------ ------------------------------
VALUE------------------------------
lock_sga                       boolean
FALSE
pre_page_sga                   boolean
FALSE
sga_max_size                   big integer
300M
sga_target                     big integer0

SQL> column NAME_COL_PLUS_SHOW_PARAM format A20
SQL> column TYPE format A20
SQL> column VALUE_COL_PLUS_SHOW_PARAM format A20

SQL> show parameter sga

NAME                 TYPE                 VALUE
-------------------- -------------------- --------------------
lock_sga             boolean              FALSE
pre_page_sga         boolean              FALSE
sga_max_size         big integer          300M
sga_target           big integer          0

Tags: , ,


Oct 27 2007

Read XML data from URL and insert into Oracle

Category: URL,XMLittichai @ 8:26 pm

There are lot of articles (example) talking about how to read a physical XML file and insert into Oracle database. Here is a little bit different. This shows you how to read XML directly from web’s URL then insert into a database table without involving a physical XML file.

To manage the list of URLs, I create a table containing list of URLs. There will be only one URL in this example. But you can use this table to manage multiple URLs if needed.

CREATE TABLE url_tab
(
  URL_NAME VARCHAR2(100),
  URL      SYS.URIType
);

Insert the URL into this table. Note that URL will go to where column type is SYS.URIType.

SQL> INSERT INTO url_tab VALUES
('This is a test URL',
sys.UriFactory.getUri('http://www.domain.com/test.xml')
);

SQL> commit;

This is the sample of the test.xml file.

The XML_DATA_TAB is a target table containing a XMLTYPE column where data will be loaded into.

SQL> CREATE TABLE xml_data_tab ( xml_data xmltype );

Table created.

If there are multiple URLs, obviously you have to loop through all in the list to load data one URL at the time. But in this case, there is only one URL, so here is how to read XML data from an URL and then convert into XMLType data.

insert into xml_data_tab
select sys.xmltype.createXML(u.url.getClob()) from url_tab u;

* Note that if you’re running on the 11g database, you might run into an error on ACL. Please consult Metalink note 453786.1. Watch out for “&nbs” keyword in step #5 box #2. Remove this typo before execution.

SQL> insert into xml_data_tabselect sys.xmltype.createXML(u.url.getClob()) from url_tab u;

select sys.xmltype.createXML(u.url.getClob()) from url_tab u
             *
ERROR at line 2:
ORA-29273: HTTP request failed
ORA-06512: at "SYS.UTL_HTTP", line 1674
ORA-24247: network access denied by access control list (ACL)
ORA-06512: at "SYS.HTTPURITYPE", line 34

After the fix -

SQL>  insert into xml_data_tabselect sys.xmltype.createXML(u.url.getClob()) from url_tab u;
1 row created.

SQL> commit;
Commit complete.

Note that there is no mechanism for any error checking. You can add yourself if needed. So it is assumed that XML is well-formed.

These are sample queries to extract data from xml_data column.

Retrieve value of attribute -

SQL> selectextractvalue(xml_data, '/crm_data/@product') product_namefrom xml_data_tab;

PRODUCT_NAME
-----------------------------------
electronics

Retrieve value of element -

SQL> select
extractvalue(xml_data, '/crm_data/customer/first_name') first_name,
extractvalue(xml_data, '/crm_data/customer/last_name') last_name,
extractvalue(xml_data, '/crm_data/customer/address') address
from xml_data_tab;

FIRST_NAME           LAST_NAME            ADDRESS
-------------------- -------------------- --------------------
John                 Doe                  12345 Lala lane

Tags: , ,


Oct 27 2007

Oracle Sample Schemas

Category: Databaseittichai @ 8:04 pm

Well, I believe most of DBAs including myself do not want to create Oracle sample schemas when creating a database. Why bother because we thought we would never use those sample schemas anyway. However, recently I’m running into an issue when trying to use tutorials on Oracle Data Mining (ODM). It requires the Sale History (SH) schema. Since database is already created, when I run “dbca” again on that database and select “Configure Database Options”, the “Sample Schemas” option is grayed out.

In order to install these sample schemas manually on the existing Oracle 10g home, a script called “mksample.sql” located at @?/demo/schema is needed. By default, the Enterprise Edition version does not include this script when binaries are installed.

D:\oracle\product\10.2.0\db_1\demo\schema> ls
bus_intelligence
mk_dir.sql.ouibak sales_history

human_resources
mk_dir.sql.sbs
sted_mkplug.sql.dbl

log mkplug.sql
mk_dir.sql
order_entry

You will need to download the 10g Companion CD, run “Setup.exe”, select to install the “Oracle Database 10g Productions” (2nd option) then specify the existing Oracle Home. The installation takes about 10 minutes. After the installation, now mksample.sql is there.

D:\oracle\product\10.2.0\db_1\demo\schema>ls
README.txt
mk_dir.sql.ouibak
mkverify.sql

bus_intelligence
mk_dir.sql.ouibak.1
order_entry

human_resources
mkplug.sql
product_media

info_exchange
mksample.sql

sales_historylog

mksample.sql.sbs
shippingmk_dir.sql
mkunplug.sql

sted_mkplug.sql.dbl

To install the sample schemas, just simply run the following at the SQL prompt -

> sqlplus /nolog
SQL>@mksample systempwd syspwd hrpwd oepwd pmpwd ixpwd shpwd bipwd default_tablespace temporary_tablespace log_file_directory

Example:
@mksample manager change_on_install hr oe pm ix sh bi example temp $ORACLE_HOME/demo/schema/log/

The process takes about 20 minutes.

Please note that this same script can be used if there is a need to reset data of these sample schemas.

Tags: ,