Mar 20 2011

Oracle 11g SQL Error Logging

Category: 11g,Database,SQLPlus,Toolittichai @ 8:00 am

We’re working on the installation scripts for an internal PL/SQL applications. Multiple scripts are called from the main one. To capture errors from script execution, normally the spool syntax will be used in the scripts to pipe out all executions into log files, and then later the deployment team members will examine them using find/search ORA- for any errors. This would work fine if there are only a few scripts but it becomes cumbersome when multiple scripts are involved. In addition we’d like to be able to run the scripts on either Windows and Unix platforms, handling OS file paths for multiple platforms using spool adds unnecessary layer.

The SQL Error Logging is a new feature in 11g. This simplifies the way we capture and locate error messages as information now will be stored in database table instead of OS files.

To check whether or not the Error Logging is enabled.

SQL> show errorlogging

errorlogging is OFF

To enable the Error Logging. The default table SPERRORLOG is created.

SQL> set errorlogging on

By default, the SPERRORLOG will be created under current user. In this sample, the current schema is TEST1_USER.

SQL> show errorlogging

errorlogging is ON TABLE TEST1_USER.SPERRORLOG

Here is the structure of this table SPERRORLOG.

SQL> desc SPERRORLOG
Name                                      Null?    Type
----------------------------------------- -------- ----------------------------
USERNAME                                           VARCHAR2(256)
TIMESTAMP                                          TIMESTAMP(6)
SCRIPT                                             VARCHAR2(1024)
IDENTIFIER                                         VARCHAR2(256)
MESSAGE                                            CLOB
STATEMENT                                          CLOB

To enable the Error Logging to an user defined table instead of the default SPERRORLOG table.

SQL> set errorlogging on table [schema].[table]

If non-default, the table has to be created in advance, otherwise you will get an error.

SQL> set errorlogging on table my_sperrorlog;

SP2-1507: Errorlogging table, role or privilege is missing or not accessible

Here is the syntax to create an user-defined table. If this table is created on a different schema, an insert grant to is needed for the current user.

SQL> CREATE TABLE my_sperrorlog (
username     VARCHAR(256),
timestamp    TIMESTAMP,
script       VARCHAR(1024),
identifier   VARCHAR(256),
message      CLOB,
statement    CLOB
);
SQL> set errorlogging on table my_sperrorlog

SQL> show errorlogging
errorlogging is ON TABLE TEST1_USER.my_sperrorlog

Demo
Generate error #1.

SQL> SELECT 1/0 FROM DUAL;
SELECT 1/0 FROM DUAL
*
ERROR at line 1:
ORA-01476: divisor is equal to zero

Generate error #2.

SQL> alter table EMP add new_column VARCHR2(1);
alter table EMP add new_column VARCHR2(1)
*
ERROR at line 1:
ORA-01735: invalid ALTER TABLE option

Check the SQL Error Logging.

SQL> column username format A10
SQL> column message format A30 wrap
SQL> column statement format A30 wrap

SQL> SELECT username, STATEMENT, message FROM sperrorlog;

USERNAME   STATEMENT                      MESSAGE
---------- ------------------------------ ------------------------------
TEST1_USER SELECT 1/0 FROM DUAL           ORA-01476: divisor is equal to
zero

TEST1_USER alter table EMP add new_column ORA-01735: invalid ALTER TABLE
VARCHR2(1)                     option

Without commit, other sessions won’t see this information.

SQL> commit;

Commit complete.

Set an unique identifier to make it easier to identify the logging record. In sample here, the identifier is set to REL1.

SQL> set errorlogging on identifier 'REL1'

Generate error #3.

SQL> alter table EMP modify ABC NOT NULL;
alter table EMP modify ABC NOT NULL
*
ERROR at line 1:
ORA-00904: "ABC": invalid identifier

Check the SQL Error Logging with the identifier=’REL1′.

SQL> SELECT username, statement, message, IDENTIFIER
FROM sperrorlog where IDENTIFIER='REL1';

USERNAME   STATEMENT                      MESSAGE                        IDENT
---------- ------------------------------ ------------------------------ -----
TEST1_USER alter table EMP modify ABC NOT ORA-00904: "ABC": invalid iden REL1
NULL                          tifier

Truncate the Error Logging to clear all existing rows in the error log table. This will clear out all records regardless of who creates them.

SQL> set errorlogging on truncate

SQL> SELECT * FROM sperrorlog;
No rows selected

There is no set errorlogging truncate only a specified identifier. Doing below is the same set errorlogging on truncate. So basically the identifier is ignored.

SQL> set errorlogging on truncate identifier 'REL1'

But you can just delete records as the regular table.

SQL> delete sperrorlog where IDENTIFIER='REL1';

SQL> commit;

Disable Error Logging. (Log off will automatically disable it.)

SQL> set errorlogging OFF

SQL> show errorlogging
errorlogging is OFF

Will it work with previous versions of Oracle database?

Since the SQL Error Logging is the feature on client, I have no reason to believe that it would not work with pre-11g databases.

The 11g SQL Plus Client to 10.2.0.1 Database

SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - 64bi
PL/SQL Release 10.2.0.1.0 - Production
CORE    10.2.0.1.0      Production
TNS for Solaris: Version 10.2.0.1.0 - Production
NLSRTL Version 10.2.0.1.0 - Production

SQL> show errorlogging
errorlogging is OFF

SQL> set errorlogging ON

SQL> show errorlogging
errorlogging is ON TEST1_USER.SPERRORLOG

SQL> select username, TIMESTAMP from  sperrorlog;

no rows selected

SQL> SELECT 1/0 FROM DUAL;
SELECT 1/0 FROM DUAL
*
ERROR at line 1:
ORA-01476: divisor is equal to zero

SQL> SELECT username, STATEMENT, message FROM sperrorlog;

USERNAME   STATEMENT                      MESSAGE
---------- ------------------------------ ------------------------------
TEST1_USER SELECT 1/0 FROM DUAL           ORA-01476: divisor is equal to
zero

The 11g SQL Plus Client to 10.1.0.4 Database

SQL> select * from v$version;
BANNER
----------------------------------------------------------------
Oracle Database 10g Enterprise Edition Release 10.1.0.4.0 - 64bi
PL/SQL Release 10.1.0.4.0 - Production
CORE    10.1.0.4.0      Production
TNS for Solaris: Version 10.1.0.4.0 - Production
NLSRTL Version 10.1.0.4.0 - Production

SQL> show errorlogging
errorlogging is OFF

SQL> set errorlogging ON

SQL> show errorlogging
errorlogging is ON TABLE TEST1_USER.SPERRORLOG

SQL> select username, TIMESTAMP from  sperrorlog;

no rows selected

SQL> SELECT 1/0 FROM DUAL;
SELECT 1/0 FROM DUAL
*
ERROR at line 1:
ORA-01476: divisor is equal to zero

SQL> SELECT username, STATEMENT, message FROM sperrorlog;

USERNAME   STATEMENT                      MESSAGE
---------- ------------------------------ ------------------------------
TEST1_USER SELECT 1/0 FROM DUAL           ORA-01476: divisor is equal to
zero

The 11g SQL Plus Client to 9i Database
Unfortunately I don’t have any Oracle 9i databases.  :-)

The 11g SQL Plus Client to 8i Database

Surprisingly, I still have one 8i database left. But, well, the 11g client no longer supports the 8i database.

ERROR:
ORA-03134: Connections to this server version are no longer supported.

Related topics:

Tags: , , , , , ,


Feb 14 2011

Easy Connect (EZCONNECT) Naming Method with Instance Name

Category: 11g,Network,SQLPlusittichai @ 7:11 pm

Oftentimes I use the Easy Connect (EZCONNECT) to connect to a new database just to quickly verify database connectivity. The syntax I usually use is as follows:

CONNECT username/password@[//]host[:port][/service_name]
CONNECT scott/tiger@host1-vip:1522/RACDB

In RAC, this will connect to whatever instance based on the service’s load balancing goal.

I just learned that with the 11g net services client software, you can also specify the instance name to connect to. This may be useful especially for RAC setup if you have the need to connect to a specific instance.

CONNECT username@[//]host[:port][/[service_name][:server]][/instance_name]]

-- Connect to the instance name RACDB1
CONNECT scott/tiger@host1-vip:1522/RACDB/RACDB1

-- Connect to the instance name RACDB2
CONNECT scott/tiger@host1-vip:1522/RACDB/RACDB2

Also, the optional [:server] is either dedicated or shared.

CONNECT scott/tiger@host1-vip:1522/RACDB:dedicated/RACDB1

Reference: http://download.oracle.com/docs/cd/B28359_01/network.111/b28316/naming.htm#NETAG255

Tags: , , , , , , , , ,


Jan 22 2009

SQL and PL/SQL reserved words

Category: SQLPlusittichai @ 10:27 am

I found this tip from forum discussing about Oracle’s reserved words. You can use it to quickly view reserved words for both SQL and PL/SQL.

SQL> help reserved

RESERVED WORDS (PL/SQL)
-----------------------

PL/SQL Reserved Words have special meaning in PL/SQL, and may not be used
for identifier names (unless enclosed in "quotes").

An asterisk (*) indicates words are also SQL Reserved Words.

ALL*            DESC*           JAVA            PACKAGE         SUBTYPE
ALTER*          DISTINCT*       LEVEL*          PARTITION       SUCCESSFUL*
AND*            DO              LIKE*           PCTFREE*        SUM
ANY*            DROP*           LIMITED         PLS_INTEGER     SYNONYM*
ARRAY           ELSE*           LOCK*           POSITIVE        SYSDATE*
AS*             ELSIF           LONG*           POSITIVEN       TABLE*
ASC*            END             LOOP            PRAGMA          THEN*
AT              EXCEPTION       MAX             PRIOR*          TIME
AUTHID          EXCLUSIVE*      MIN             PRIVATE         TIMESTAMP
AVG             EXECUTE         MINUS*          PROCEDURE       TIMEZONE_ABBR
BEGIN           EXISTS*         MINUTE          PUBLIC*         TIMEZONE_HOUR
BETWEEN*        EXIT            MLSLABEL*       RAISE           TIMEZONE_MINUTE
BINARY_INTEGER  EXTENDS         MOD             RANGE           TIMEZONE_REGION
BODY            EXTRACT         MODE*           RAW*            TO*
BOOLEAN         FALSE           MONTH           REAL            TRIGGER*
BULK            FETCH           NATURAL         RECORD          TRUE
BY*             FLOAT*          NATURALN        REF             TYPE
CHAR*           FOR*            NEW             RELEASE         UI
CHAR_BASE       FORALL          NEXTVAL         RETURN          UNION*
CHECK*          FROM*           NOCOPY          REVERSE         UNIQUE*
CLOSE           FUNCTION        NOT*            ROLLBACK        UPDATE*
CLUSTER*        GOTO            NOWAIT*         ROW*            USE
COALESCE        GROUP*          NULL*           ROWID*          USER*
COLLECT         HAVING*         NULLIF          ROWNUM*         VALIDATE*
COMMENT*        HEAP            NUMBER*         ROWTYPE         VALUES*
COMMIT          HOUR            NUMBER_BASE     SAVEPOINT       VARCHAR*
COMPRESS*       IF              OCIROWID        SECOND          VARCHAR2*
CONNECT*        IMMEDIATE*      OF*             SELECT*         VARIANCE
CONSTANT        IN*             ON*             SEPERATE        VIEW*
CREATE*         INDEX*          OPAQUE          SET*            WHEN
CURRENT*        INDICATOR       OPEN            SHARE*          WHENEVER*
CURRVAL         INSERT*         OPERATOR        SMALLINT*       WHERE*
CURSOR          INTEGER*        OPTION*         SPACE           WHILE
DATE*           INTERFACE       OR*             SQL             WITH*
DAY             INTERSECT*      ORDER*          SQLCODE         WORK
DECIMAL*        INTERVAL        ORGANIZATION    SQLERRM         WRITE
DECLARE         INTO*           OTHERS          START*          YEAR
DEFAULT*        IS*             OUT             STDDEV          ZONE
DELETE*         ISOLATION

RESERVED WORDS (SQL)
--------------------

SQL Reserved Words have special meaning in SQL, and may not be used for
identifier names unless enclosed in "quotes".

An asterisk (*) indicates words are also ANSI Reserved Words.

Oracle prefixes implicitly generated schema object and subobject names
with "SYS_". To avoid name resolution conflict, Oracle discourages you
from prefixing your schema object and subobject names with "SYS_".

ACCESS          DEFAULT*         INTEGER*        ONLINE          START
ADD*            DELETE*          INTERSECT*      OPTION*         SUCCESSFUL
ALL*            DESC*            INTO*           OR*             SYNONYM
ALTER*          DISTINCT*        IS*             ORDER*          SYSDATE
AND*            DROP*            LEVEL*          PCTFREE         TABLE*
ANY*            ELSE*            LIKE*           PRIOR*          THEN*
AS*             EXCLUSIVE        LOCK            PRIVILEGES*     TO*
ASC*            EXISTS           LONG            PUBLIC*         TRIGGER
AUDIT           FILE             MAXEXTENTS      RAW             UID
BETWEEN*        FLOAT*           MINUS           RENAME          UNION*
BY*             FOR*             MLSLABEL        RESOURCE        UNIQUE*
CHAR*           FROM*            MODE            REVOKE*         UPDATE*
CHECK*          GRANT*           MODIFY          ROW             USER*
CLUSTER         GROUP*           NOAUDIT         ROWID           VALIDATE
COLUMN          HAVING*          NOCOMPRESS      ROWNUM          VALUES*
COMMENT         IDENTIFIED       NOT*            ROWS*           VARCHAR*
COMPRESS        IMMEDIATE*       NOWAIT          SELECT*         VARCHAR2
CONNECT*        IN*              NULL*           SESSION*        VIEW*
CREATE*         INCREMENT        NUMBER          SET*            WHENEVER*
CURRENT*        INDEX            OF*             SHARE           WHERE
DATE*           INITIAL          OFFLINE         SIZE*           WITH*
DECIMAL*        INSERT*          ON*             SMALLINT*

Tags: , ,


Dec 24 2008

Generating HTML reports from SQL Plus

Category: SQLPlusittichai @ 3:13 am

Last week I was asked by one of IT managers on how to generate reports from database. He’d like to remind his direct reports on ticket’s backlog status. His requirement is just simply to run a simple SQL statement and email the result out weekly. I quickly told him that you can just cron or schedule the script. “Hold on! I’m not finished yet,” he said. If possible, he’d like to have some report data color-coded so everyone can be visually reminded of meeting or missing goals. “And lastly, if possible, report should be formatted nicely – not just in plain text,” he added.

Um…

This is the sample of report I tried to assist him. The required features would include a company logo, color-coded report data based on thresholds, and URL link on ID.

sample

To have the query result in the html format, fortunately, the SQL Plus provides the “markup html” feature since version 8.1.6. There have been a quite number of discussions about this already. But the best place for reference is at Oracle SQL Plus User’s Guide and Reference web site.

I started out with the current configuration of the markup html which by default is OFF.

SQL> show markup
markup HTML OFF HEAD "<style type='text/css'> body {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;} p {font:10pt Arial,Helvetica,sans-serif; color:black; background:White;} table,tr,td {font:10pt Arial,Helvetica,sans-serif; color:Black; background:#f7f7e7; padding:0px 0px 0px 0px; margin:0px 0px 0px 0px;} th {font:bold 10pt Arial,Helvetica,sans-serif; color:#336699; background:#cccc99; padding:0px 0px 0px 0px;} h1 {font:16pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; border-bottom:1px solid #cccc99; margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;-} h2 {font:bold 10pt Arial,Helvetica,Geneva,sans-serif; color:#336699; background-color:White; margin-top:4pt; margin-bottom:0pt;} a {font:9pt Arial,Helvetica,sans-serif; color:#663300; background:#ffffff; margin-top:0pt; margin-bottom:0pt; vertical-align:top;}</style><title>SQL*Plus Report</title>" BODY "" TABLE "border='1' width='90%' align='center' summary='Script output'" SPOOL OFF ENTMAP ON PREFORMAT OFF

This looks like it is a lot, but actually it is just a bunch of CSS syntaxes which control how the data content to be formatted. If you re-arrange it, it should look like this -

markup HTML ON HEAD "<style type='text/css'> -
body { -
font:10pt Arial,Helvetica,sans-serif; -
color:black; background:white; } -
p { -
font:8pt Arial,sans-serif; -
color:grey; background:white; } -
table,tr,td { -
font:10pt Arial,Helvetica,sans-serif; -
text-align:right; -
color:black; background:white; -
padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; } -
th { -
font:bold 10pt Arial,Helvetica,sans-serif; -
color:#336699; -
background:#cccc99; -
padding:0px 0px 0px 0px;} -
h1 { -
font:16pt Arial,Helvetica,Geneva,sans-serif; -
color:#336699; -
background-color:White; -
border-bottom:1px solid #cccc99; -
margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;} -
h2 { -
font:bold 10pt Arial,Helvetica,Geneva,sans-serif; -
color:#336699; -
background-color:White; -
margin-top:4pt; margin-bottom:0pt;} -
a { -
font:9pt Arial,Helvetica,sans-serif; -
color:#663300; -
background:#ffffff; -
margin-top:0pt; margin-bottom:0pt; vertical-align:top;} -
</style> -
<title>SQL*Plus Report</title>" -
BODY "" -
TABLE "border='1' width='90%' align='center' -
summary='Script output'" -
SPOOL OFF ENTMAP ON PREFORMAT OFF

Note that I added “-” (hyphen) so the tool will treat the next line as the continued line.

To make the query result to be in the html format, the “markup html” must be enabled – MARKUP HTML ON. Also, I disabled the ENTMAP to be OFF, so the SQL Plus won’t replace special characters <,>, ” and & with HTML entitles &lt;, &gt;, &quot; and &amp; respectively. This is needed so I can use the HTML tags in SQL statement. This could be done at the column level as well.

So this is the updated version with a “set” syntax to be ran at the SQL prompt -

$ cat /scripts/set_markup.sql
set markup HTML ON HEAD "<style type='text/css'> -
body { -
font:10pt Arial,Helvetica,sans-serif; -
color:blue; background:white; } -
p { -
font:8pt Arial,sans-serif; -
color:grey; background:white; } -
table,tr,td { -
font:10pt Arial,Helvetica,sans-serif; -
text-align:right; -
color:Black; background:white; -
padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; } -
th { -
font:bold 10pt Arial,Helvetica,sans-serif; -
color:#336699; -
background:#cccc99; -
padding:0px 0px 0px 0px;} -
h1 { -
font:16pt Arial,Helvetica,Geneva,sans-serif; -
color:#336699; -
background-color:White; -
border-bottom:1px solid #cccc99; -
margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;} -
h2 { -
font:bold 10pt Arial,Helvetica,Geneva,sans-serif; -
color:#336699; -
background-color:White; -
margin-top:4pt; margin-bottom:0pt;} -
a { -
font:9pt Arial,Helvetica,sans-serif; -
color:#663300; -
background:#ffffff; -
margin-top:0pt; margin-bottom:0pt; vertical-align:top;} -
.threshold-critical { -
font:bold 10pt Arial,Helvetica,sans-serif; -
color:red; } -
.threshold-warning { -
font:bold 10pt Arial,Helvetica,sans-serif; -
color:orange; } -
.threshold-ok { -
font:bold 10pt Arial,Helvetica,sans-serif; -
color:green; } -
</style> -
<title>SQL*Plus Report</title>" -
BODY "<img src=http://www.acme.com/company_logo..gif/>" -
TABLE "border='1' width='90%' align='center'" -
ENTMAP OFF SPOOL ON

For ease of execution, I placed above syntax in a SQL file named /scripts/set_markup.sql which will be called when report runs.

I added new classes (“.threshold-XXX”) so different texts can be displayed into different colors based on defined thresholds. I also added the company logo’s image on report under BODY section.

Please note that you can remove unneeded CSS formats, i.e., a{} or margins, to make script cleaner. However, for a demonstration purpose, I leave everything as is.

The report part /scripts/run_weekly_backlog_report.sql is something like this -

$ cat /scripts/run_weekly_backlog_report.sql

connect user/password

@/scripts/set_markup.sql

set pages 100
TTITLE LEFT _DATE CENTER '<h1>Weekly Top Backlog Report : Database Team NA Region</h1>' -
RIGHT 'Page:' FORMAT 999 SQL.PNO SKIP CENTER '<h2>Red = Backlog > 30 days, Orange > 10 days</h2>'

spool d:\temp\backlog_report.htm

select '<a href="http://www.acme-intra.com/pls/apex/f?p=100:3:::::PID:'||ID||'">'||ID||'</a>' ID,
DESCRIPTION, ASSIGNED_TO, OPEN_DATE,
CASE
when SYSDATE-OPEN_DATE > 30 then
'<span class="threshold-critical">'||to_char(trunc(SYSDATE-OPEN_DATE))||'</span>'
when SYSDATE-OPEN_DATE > 10 then
'<span class="threshold-warning">'||to_char(trunc(SYSDATE-OPEN_DATE))||'</span>'
ELSE
'<span class="threshold-ok">'||to_char(trunc(SYSDATE-OPEN_DATE))||'</span>'
END BACKLOG
from TBL_TICKETS
where STATUS='Open'
order by SYSDATE-OPEN_DATE DESC;

spool off

exit

This will execute “set_markup.sql” first to enable markup html. For the rest, there is nothing unusual. The ID column is embedded with an URL, so users can view the content of the ticket if needed. Also please note the use of the <h1>, <h2> and <span class> tags to format data based on defined thresholds.

This script can be called just by using

sqlplus /nolog @/scripts/run_weekly_backlog_report.sql

The last thing is just put above syntax in a batch file and add sending email (using mail or blat for Windows) with the content of the spooled html file.

Tags: , , , , ,


Mar 16 2008

11g SQL*Plus

Category: SQLPlusittichai @ 2:08 am

In 11g SQL*Plus, when session is terminated, somehow it displays the process ID, SID and Serial#. Not quite sure what is really for? I guess it is for debugging purpose.

10g Client

user @db10w1> select * from dual;
select * from dual
*
ERROR at line 1:
ORA-01012: not logged on

11g Client

user @db10w1> select * from dual;
select * from dual
*
ERROR at line 1:
ORA-01012: not logged on
Process ID: 0
Session ID: 135 Serial number: 29158

Tags: ,


Mar 16 2008

show spparameters

Category: SQLPlusittichai @ 1:25 am

I found one of the interesting features in SQL*Plus 11g for Oracle11g database when doing “help show” from the tool.

SHOW  SPPARAMETERS [parameter_name]

Show specific parameters -

SYSTEM @db11r1> show spparameters sga
SID      NAME                          TYPE        VALUE
-------- ----------------------------- ----------- ------------------
*        lock_sga                      boolean
*        pre_page_sga                  boolean
*        sga_max_size                  big integer
*        sga_target                    big integer

Show all parameters -

SYSTEM @db11r1> show spparameters
SID      NAME                          TYPE        VALUE
-------- ----------------------------- ----------- ------------------
*        O7_DICTIONARY_ACCESSIBILITY   boolean
*        active_instance_count         integer
*        aq_tm_processes               integer     2
*        archive_lag_target            integer
*        asm_diskgroups                string
*        asm_diskstring                string
*        asm_power_limit               integer
*        asm_preferred_read_failure_gr string
:         :         :
*        use_indirect_data_buffers     boolean
*        user_dump_dest                string
*        utl_file_dir                  string
*        workarea_size_policy          string
*        xml_db_events                 string

Originally I thought it is a feature of SQL*Plus 11g, but if I connect to the 10g database, it does not display anything. Note that error indicates that Server version too low for this feature.

SYSTEM @db10w1> show spparameters
SP2-0614: Server version too low for this feature
SP2-0735: unknown SHOW option beginning "lege'@'_co..."
SP2-0158: unknown SHOW option """

If database is started up with pfile, all values of “show spparameters” will be empty.

Tags:


Dec 04 2007

SQL*Plus Copy

Category: SQLPlusittichai @ 5:17 pm

The SQL*Plus COPY command can copy data between two databases via SQL*Net.

Syntax:

USER1 @srcdb> copy
usage: COPY FROM [db] TO [db] [opt] [table] { ([cols]) } USING [sel]
[db]   : database string, e.g., hr/your_password@d:chicago-mktg
[opt]  : ONE of the keywords: APPEND, CREATE, INSERT or REPLACE
[table]: name of the destination table
[cols] : a comma-separated list of destination column aliases
[sel]  : any valid SQL SELECT statement
A missing FROM or TO clause uses the current SQL*Plus connection.

Sample:
@Target database, the table initially does not exist.

USER1 @targetdb> select * from rep_tab;
select * from rep_tab
   *
ERROR at line 1:ORA-00942: table or view does not exist

@Source database, run copy command within SQL Plus.

USER1 @srcdb> copy from user1/user1@srcdb -
> to user1/user1@target_host:1521/target_sid -
> create rep_tab -
> using -
> select * from rep_tab;

Array fetch/bind size is 15. (arraysize is 15)
Will commit when done. (copycommit is 0)
Maximum long size is 80. (long is 80)
Table REP_TAB created.

1 rows selected from user1@SRCDB.
1 rows inserted into REP_TAB.
1 rows committed into REP_TAB at user1@host1:1521/targetsid.

@Target database, now table is copied here.

USER1 @targetdb> select * from rep_tab;

A
----------
1

This is a quick and easy way to copy data between two databases especially on where the usage of database link is restricted. It only requires SQL*Net service name (or host/port/service) and proper privileges.

Tags:


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: , ,