Showing posts with label iSQL. Show all posts
Showing posts with label iSQL. Show all posts

Thursday, January 29, 2009

Formatting SQL*Plus Reports Part 2

Computing Summary of a column
With combination of BREAK and COMPUTE command you can do several calculations on SQL reports using sql*plus.
Remember the COMPUTE command has no effect without a corresponding BREAK command.

Below is the lists of compute functions of Sql*plus and their effects.

1)SUM: Sum of the values in the column.

2)MINIMUM: Minimum value in the column.

3)MAXIMUM: Maximum value in the column.

4)AVG: Average of the values in the column.

5)STD: Standard deviation of the values in the column.

6)VARIANCE: Variance of the values in the column.

7)COUNT: Number of non-null values in the column.

8)NUMBER: Number of rows in the column.

Let's look at our data,

SQL> select dept_id,name,salary from emp;

DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

6 rows selected.

Now we wish to compute the total of SALARY by department. To do that use,

SQL> break on dept_id
SQL> compute sum of salary on dept_id
SQL> /


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
ABDUL 22000
KHALEEL 40000

********** ----------
sum 122000
20 IBRAHIM 35000
RAVI 60000

********** ----------
sum 95000
30 KASARAPU 25000
********** ----------
sum 25000

6 rows selected.


Note that the word sum appears in every break. If you don't want to print sum word then do as,

SQL> COLUMN DUMMY NOPRINT;
SQL> COMPUTE SUM OF SALARY ON DUMMY;
SQL> BREAK ON DUMMY SKIP 1;
SQL> select dept_id DUMMY,dept_id,name,salary from emp;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
----------
122000

20 IBRAHIM 35000
20 RAVI 60000

----------
95000
30 KASARAPU 25000

----------
25000
6 rows selected.

To compute the salaries just at the end of the report,

SQL> COLUMN DUMMY NOPRINT;
SQL> COMPUTE SUM OF SALARY ON DUMMY;
SQL> BREAK ON DUMMY;
SQL> SELECT NULL DUMMY,DEPT_ID,NAME,SALARY FROM EMP;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

----------
242000

6 rows selected.


To calculate grand total of salary and make it a level do,

SQL> BREAK ON REPORT
SQL> COMPUTE SUM LABEL TOTAL OF SALARY ON REPORT
SQL> select dept_id,name,salary from emp;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

----------
TOTAL 242000

6 rows selected.

To compute the both average and sum of salaries of a department do,

SQL> BREAK ON DEPT_ID
SQL> COMPUTE AVG SUM OF SALARY ON DEPT_ID
SQL> select dept_id,name,salary from emp where dept_id=10;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
ABDUL 22000
KHALEEL 40000

********** ----------
avg 40666.6667
sum 122000


To see current compute settings,
SQL> compute
COMPUTE sum LABEL 'sum' OF SALARY ON DUMMY
COMPUTE sum LABEL 'TOTAL' OF SALARY ON REPORT
COMPUTE avg LABEL 'avg' sum LABEL 'sum' OF SALARY ON DEPT_ID
To remove all computes definitions,
SQL> clear compute
computes cleared

Formatting SQL*Plus Reports Part 2

Computing Summary of a column
With combination of BREAK and COMPUTE command you can do several calculations on SQL reports using sql*plus.
Remember the COMPUTE command has no effect without a corresponding BREAK command.

Below is the lists of compute functions of Sql*plus and their effects.

1)SUM: Sum of the values in the column.

2)MINIMUM: Minimum value in the column.

3)MAXIMUM: Maximum value in the column.

4)AVG: Average of the values in the column.

5)STD: Standard deviation of the values in the column.

6)VARIANCE: Variance of the values in the column.

7)COUNT: Number of non-null values in the column.

8)NUMBER: Number of rows in the column.

Let's look at our data,

SQL> select dept_id,name,salary from emp;

DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

6 rows selected.

Now we wish to compute the total of SALARY by department. To do that use,

SQL> break on dept_id
SQL> compute sum of salary on dept_id
SQL> /


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
ABDUL 22000
KHALEEL 40000

********** ----------
sum 122000
20 IBRAHIM 35000
RAVI 60000

********** ----------
sum 95000
30 KASARAPU 25000
********** ----------
sum 25000

6 rows selected.


Note that the word sum appears in every break. If you don't want to print sum word then do as,

SQL> COLUMN DUMMY NOPRINT;
SQL> COMPUTE SUM OF SALARY ON DUMMY;
SQL> BREAK ON DUMMY SKIP 1;
SQL> select dept_id DUMMY,dept_id,name,salary from emp;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
----------
122000

20 IBRAHIM 35000
20 RAVI 60000

----------
95000
30 KASARAPU 25000

----------
25000
6 rows selected.

To compute the salaries just at the end of the report,

SQL> COLUMN DUMMY NOPRINT;
SQL> COMPUTE SUM OF SALARY ON DUMMY;
SQL> BREAK ON DUMMY;
SQL> SELECT NULL DUMMY,DEPT_ID,NAME,SALARY FROM EMP;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

----------
242000

6 rows selected.


To calculate grand total of salary and make it a level do,

SQL> BREAK ON REPORT
SQL> COMPUTE SUM LABEL TOTAL OF SALARY ON REPORT
SQL> select dept_id,name,salary from emp;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
10 ABDUL 22000
10 KHALEEL 40000
20 IBRAHIM 35000
20 RAVI 60000
30 KASARAPU 25000

----------
TOTAL 242000

6 rows selected.

To compute the both average and sum of salaries of a department do,

SQL> BREAK ON DEPT_ID
SQL> COMPUTE AVG SUM OF SALARY ON DEPT_ID
SQL> select dept_id,name,salary from emp where dept_id=10;


DEPT_ID NAME SALARY
---------- ------------ ----------
10 SHAIK 60000
ABDUL 22000
KHALEEL 40000

********** ----------
avg 40666.6667
sum 122000


To see current compute settings,
SQL> compute
COMPUTE sum LABEL 'sum' OF SALARY ON DUMMY
COMPUTE sum LABEL 'TOTAL' OF SALARY ON REPORT
COMPUTE avg LABEL 'avg' sum LABEL 'sum' OF SALARY ON DEPT_ID
To remove all computes definitions,
SQL> clear compute
computes cleared

Controlling the Autotrace Report in sql*plus

In sql*plus with AUTOTRACE command you can see the execution plan of successful SQL DML (that is, SELECT, DELETE, UPDATE and INSERT) statements which is useful for monitoring and tuning the performance of these statements.

You can control the report by setting the AUTOTRACE system variable.

Following is the available AUTOTRACE settings.

1)SET AUTOTRACE OFF: No AUTOTRACE report is generated. This is the default. Result of a query will be displayed on the console as usual.

2)SET AUTOTRACE ON EXPLAIN: The AUTOTRACE report shows the optimizer execution path along with query output and note but does not show statistics.

3)SET AUTOTRACE ON STATISTICS:
The AUTOTRACE report shows the SQL statement execution statistics along with query output but does not display execution plan.

4)SET AUTOTRACE ON: The AUTOTRACE report includes both the optimizer execution path and the SQL statement execution statistics along with query output.

5)SET AUTOTRACE TRACEONLY: Report displays both optimizer execution path as well as execution statistics but does not display query output. If STATISTICS is enabled, query data is still fetched, but not printed.

Controlling the Autotrace Report in sql*plus

In sql*plus with AUTOTRACE command you can see the execution plan of successful SQL DML (that is, SELECT, DELETE, UPDATE and INSERT) statements which is useful for monitoring and tuning the performance of these statements.

You can control the report by setting the AUTOTRACE system variable.

Following is the available AUTOTRACE settings.

1)SET AUTOTRACE OFF: No AUTOTRACE report is generated. This is the default. Result of a query will be displayed on the console as usual.

2)SET AUTOTRACE ON EXPLAIN: The AUTOTRACE report shows the optimizer execution path along with query output and note but does not show statistics.

3)SET AUTOTRACE ON STATISTICS:
The AUTOTRACE report shows the SQL statement execution statistics along with query output but does not display execution plan.

4)SET AUTOTRACE ON: The AUTOTRACE report includes both the optimizer execution path and the SQL statement execution statistics along with query output.

5)SET AUTOTRACE TRACEONLY: Report displays both optimizer execution path as well as execution statistics but does not display query output. If STATISTICS is enabled, query data is still fetched, but not printed.

Tuesday, January 27, 2009

Use of bind variables in Sql*plus

Bind variables are variables that are declared inside PL/SQL or a variable in a SQL statement that must be replaced with a valid value.

In the SQL*Plus you can also create bind variable. If you create a bind variable in SQL*Plus, you can use the variable as you would a declared variable in your PL/SQL subprogram and then access the variable from SQL*Plus.

Creating Bind Variables
In order to create bind variable in Sql*plus you have to use variable keyword. To create a bind variable named bind_var as number datatype use,
variable bind_var NUMBER

Displaying Bind Variables

To display all bind variables in the session just use variable keyword.
SQL> variable
variable bind_var
datatype NUMBER

In order to display the value of a particular bind variable use,
SQL> print bind_var
BIND_VAR
----------

Using Bind Variables
To use bind variable first use colon(:) and then give variable name. In order to change values of bind variable you must enter in a PL/SQL block. To change bind variable of bind_var to 10 use,
begin
:bind_var:=10;
end;
/

Formatting SQL*Plus Reports

Sometimes you may find difficulties while displaying result on SQL*Plus like a column length displayed as a long length and thus does not result a fine output. In this post I will try to show how we can make good looking result displayed on SQL*Plus.

With COLUMN a column can be formatted. For string type data format is specified by A and then length. Like A7 means total column will span to 7 word length. For number data type the format is 99999 where number of 9s decide the length of digits of number data type.
Changing Column Headings
To change the heading of column a,b and c use commands like below. The vertical bar is used if you want to display the columns in a new line.

SQL> COLUMN A HEADING 'FIRST|COLUMN'
SQL> COLUMN C HEADING 'THIRD|COLUMN'
SQL> COLUMN B HEADING 'SECOND|COLUMN' FORMAT A7
SQL> select a,b,c from test1;


FIRST SECOND THIRD
COLUMN COLUMN COLUMN
---------- ------- ------
1 Hi Hi2

Setting the Underline Character
You see the underline character in the above output under heading is set to -. If you want to change it to = then use,

SQL> SET UNDERLINE =
SQL> /


FIRST SECOND THIRD
COLUMN COLUMN COLUMN
========== ======= ======
1 Hi Hi2

Default Display of columns
1)A NUMBER column's width equals the width of the heading or the width of the FORMAT plus one space for the sign, whichever is greater. If you do not explicitly use FORMAT, then the column's width will always be at least the value of SET NUMWIDTH.

BY default the NUMWIDTH is set to 10.

SQL> select 22/7 "This is the pi value" from dual;

This is the pi value
--------------------
3.14285714

SQL> show numwidth

numwidth 10

2)The default width of datatype columns is the width of the column in the database. The column width of a LONG, CLOB, NCLOB or XMLType defaults to the value of SET LONGCHUNKSIZE or SET LONG, whichever is the smaller.

If LONG is set to 80 and LONGCHUNKSIZE is set to 90 then for CLOB database only first 80 character will be shown on Sql*plus by default.

3)The default width and format of unformatted DATE columns in SQL*Plus is determined by the database NLS_DATE_FORMAT parameter. Otherwise, the default format width is A9.

Listing and Resetting Column Display Attributes
1)To list the current display attributes for a given column, use the COLUMN command followed by the column name.

COLUMN column_name

2)To list the current display attributes for all columns, enter just the COLUMN command.

COLUMN

3)To reset the display attributes for a column to their default values, use the CLEAR clause of the COLUMN command as below.

COLUMN column_name CLEAR

4)To reset all column display attributes to their default values enter,

CLEAR COLUMNS


Suppressing and Restoring Column Display Attributes

Though COLUMN command you once format a column and now you want to use the default display attributes for the column, but you don't want to remove the attributes you have defined through the COLUMN command. You can achieve that by,
COLUMN column_name OFF

To restore the attributes you defined through COLUMN, use the ON clause:

COLUMN column_name ON

Printing a Line of Characters after Wrapped Column Values
RECSEP determines when the line of characters is printed;
-you set RECSEP to EACH to print after every line,
-to WRAPPED to print after wrapped lines,
-The default setting of RECSEP is WRAPPED.

RECSEPCHAR sets the character printed in each line.

To print a line of dashes after each wrapped column value, enter:

SET RECSEP WRAPPED
SET RECSEPCHAR "-"


Clarifying Your Report with Spacing and Summary Lines
With the BREAK command you can suppress the duplicate values in a column specified in an ORDER BY clause. If two columns values come in the subsequent rows and you use BREAK on the column then only one will be shown as below.

Note that here I did not use order by clause. You should use order by on the column that you break. If you do not do this, breaks occur every time the column value changes.

SQL> select station_id,uname from users where station_id!=1 and rownum<=10;

STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat
14 parvez
14 reza
14 voyeger
40 support
71 accounts5
71 bill5
71 chinmoy
71 crash5
71 mubeen5

10 rows selected.

SQL> break on station_id skip 2
SQL> select station_id,uname from users where station_id!=1 and rownum<=10;


STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat


14 parvez
reza
voyeger


40 support


71 accounts5
bill5
chinmoy
crash5
mubeen5



10 rows selected.

SQL> break on station_id skip page
SQL> /


STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat

STATION_ID UNAME
---------- --------------------------------------------------
14 parvez
reza
voyeger

STATION_ID UNAME
---------- --------------------------------------------------
40 support

STATION_ID UNAME
---------- --------------------------------------------------
71 accounts5
bill5
chinmoy
crash5
mubeen5

10 rows selected.

To insert n blank lines, use the BREAK command in the following form:

BREAK ON break_column SKIP n

where n can be 1 or 2 or ....
To skip a page, use the command in this form:

BREAK ON break_column SKIP PAGE


You may wish to insert blank lines or a blank page after every row.
To skip n lines after every row, use BREAK in the following form:

BREAK ON ROW SKIP n

To skip a page after every row, use

BREAK ON ROW SKIP PAGE


You can list your current break definition by entering the BREAK command with no clauses:

BREAK


You can remove the current break definition by entering the CLEAR command with the BREAKS clause:

CLEAR BREAKS

Use of bind variables in Sql*plus

Bind variables are variables that are declared inside PL/SQL or a variable in a SQL statement that must be replaced with a valid value.

In the SQL*Plus you can also create bind variable. If you create a bind variable in SQL*Plus, you can use the variable as you would a declared variable in your PL/SQL subprogram and then access the variable from SQL*Plus.

Creating Bind Variables
In order to create bind variable in Sql*plus you have to use variable keyword. To create a bind variable named bind_var as number datatype use,
variable bind_var NUMBER

Displaying Bind Variables

To display all bind variables in the session just use variable keyword.
SQL> variable
variable bind_var
datatype NUMBER

In order to display the value of a particular bind variable use,
SQL> print bind_var
BIND_VAR
----------

Using Bind Variables
To use bind variable first use colon(:) and then give variable name. In order to change values of bind variable you must enter in a PL/SQL block. To change bind variable of bind_var to 10 use,
begin
:bind_var:=10;
end;
/

Formatting SQL*Plus Reports

Sometimes you may find difficulties while displaying result on SQL*Plus like a column length displayed as a long length and thus does not result a fine output. In this post I will try to show how we can make good looking result displayed on SQL*Plus.

With COLUMN a column can be formatted. For string type data format is specified by A and then length. Like A7 means total column will span to 7 word length. For number data type the format is 99999 where number of 9s decide the length of digits of number data type.
Changing Column Headings
To change the heading of column a,b and c use commands like below. The vertical bar is used if you want to display the columns in a new line.

SQL> COLUMN A HEADING 'FIRST|COLUMN'
SQL> COLUMN C HEADING 'THIRD|COLUMN'
SQL> COLUMN B HEADING 'SECOND|COLUMN' FORMAT A7
SQL> select a,b,c from test1;


FIRST SECOND THIRD
COLUMN COLUMN COLUMN
---------- ------- ------
1 Hi Hi2

Setting the Underline Character
You see the underline character in the above output under heading is set to -. If you want to change it to = then use,

SQL> SET UNDERLINE =
SQL> /


FIRST SECOND THIRD
COLUMN COLUMN COLUMN
========== ======= ======
1 Hi Hi2

Default Display of columns
1)A NUMBER column's width equals the width of the heading or the width of the FORMAT plus one space for the sign, whichever is greater. If you do not explicitly use FORMAT, then the column's width will always be at least the value of SET NUMWIDTH.

BY default the NUMWIDTH is set to 10.

SQL> select 22/7 "This is the pi value" from dual;

This is the pi value
--------------------
3.14285714

SQL> show numwidth

numwidth 10

2)The default width of datatype columns is the width of the column in the database. The column width of a LONG, CLOB, NCLOB or XMLType defaults to the value of SET LONGCHUNKSIZE or SET LONG, whichever is the smaller.

If LONG is set to 80 and LONGCHUNKSIZE is set to 90 then for CLOB database only first 80 character will be shown on Sql*plus by default.

3)The default width and format of unformatted DATE columns in SQL*Plus is determined by the database NLS_DATE_FORMAT parameter. Otherwise, the default format width is A9.

Listing and Resetting Column Display Attributes
1)To list the current display attributes for a given column, use the COLUMN command followed by the column name.

COLUMN column_name

2)To list the current display attributes for all columns, enter just the COLUMN command.

COLUMN

3)To reset the display attributes for a column to their default values, use the CLEAR clause of the COLUMN command as below.

COLUMN column_name CLEAR

4)To reset all column display attributes to their default values enter,

CLEAR COLUMNS


Suppressing and Restoring Column Display Attributes

Though COLUMN command you once format a column and now you want to use the default display attributes for the column, but you don't want to remove the attributes you have defined through the COLUMN command. You can achieve that by,
COLUMN column_name OFF

To restore the attributes you defined through COLUMN, use the ON clause:

COLUMN column_name ON

Printing a Line of Characters after Wrapped Column Values
RECSEP determines when the line of characters is printed;
-you set RECSEP to EACH to print after every line,
-to WRAPPED to print after wrapped lines,
-The default setting of RECSEP is WRAPPED.

RECSEPCHAR sets the character printed in each line.

To print a line of dashes after each wrapped column value, enter:

SET RECSEP WRAPPED
SET RECSEPCHAR "-"


Clarifying Your Report with Spacing and Summary Lines
With the BREAK command you can suppress the duplicate values in a column specified in an ORDER BY clause. If two columns values come in the subsequent rows and you use BREAK on the column then only one will be shown as below.

Note that here I did not use order by clause. You should use order by on the column that you break. If you do not do this, breaks occur every time the column value changes.

SQL> select station_id,uname from users where station_id!=1 and rownum<=10;

STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat
14 parvez
14 reza
14 voyeger
40 support
71 accounts5
71 bill5
71 chinmoy
71 crash5
71 mubeen5

10 rows selected.

SQL> break on station_id skip 2
SQL> select station_id,uname from users where station_id!=1 and rownum<=10;


STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat


14 parvez
reza
voyeger


40 support


71 accounts5
bill5
chinmoy
crash5
mubeen5



10 rows selected.

SQL> break on station_id skip page
SQL> /


STATION_ID UNAME
---------- --------------------------------------------------
4 a.sadat

STATION_ID UNAME
---------- --------------------------------------------------
14 parvez
reza
voyeger

STATION_ID UNAME
---------- --------------------------------------------------
40 support

STATION_ID UNAME
---------- --------------------------------------------------
71 accounts5
bill5
chinmoy
crash5
mubeen5

10 rows selected.

To insert n blank lines, use the BREAK command in the following form:

BREAK ON break_column SKIP n

where n can be 1 or 2 or ....
To skip a page, use the command in this form:

BREAK ON break_column SKIP PAGE


You may wish to insert blank lines or a blank page after every row.
To skip n lines after every row, use BREAK in the following form:

BREAK ON ROW SKIP n

To skip a page after every row, use

BREAK ON ROW SKIP PAGE


You can list your current break definition by entering the BREAK command with no clauses:

BREAK


You can remove the current break definition by entering the CLEAR command with the BREAKS clause:

CLEAR BREAKS

Sql*plus Basics and Tips

The Sql Buffer
The Sql buffer stores most recently SQL command or most recently PL/SQL block.

SQL*Plus does not store SQL*Plus commands, or the semicolon or slash characters
you type to execute a command in the SQL buffer.

You can execute the command or block in the SQL buffer using the RUN or /(slash)
commands.

RUN commands displays the command or block in the buffer and then execute it.
/(slash) executes the command or block in the buffer but does not display it.

Below is the example.
A simple SQL command.
SQL> select sysdate from dual;
SYSDATE
---------
29-NOV-08

This one (Setting linesize) is SQL*plus command and hence is not stored in SQL buffer.
SQL> set linesize 100

Invoking list will show SQL buffer, not SQL*plus command.

SQL> list
1* select sysdate from dual

Run will execute the command after displaying it.
SQL> run
1* select sysdate from dual

SYSDATE
---------
29-NOV-08

/(Slash) will not display the command. It will only execute it.
SQL> /

SYSDATE
---------
29-NOV-08

Continuing a Long SQL*Plus Command on Additional Lines
Suppose you want to write a long sql*plus command or a sql command, a long identifier or a long word in the SQL*plus. You can put them in one line by entering hyphen(-) at the end and then entering a carriage return. SQL*Plus displays a right angle-bracket (>) as a prompt for each additional line.

So hyphen(-) in Sql*plus is considered as continuation character. For example to format a long column you can use hyphen in sql*plus.
SQL> column salary format 99,999 -
> heading 'Monthly Salary'

Similarly,
SQL> select 200--
> 100 from dual;


200-100
----------
100
SQL> select 200-
> -100 from dual;


200-100
----------
100
Hyphen at the last is considered as continuation character. So hyphen at the last is ignored. Hence following will return error.
SQL> select 200-
> 100 from dual;

select 200 100 from dual
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

Stopping a Command while it is Running
In order to stop displaying a long running output on Sql*plus press CTRL+C.

Displaying output page by page in Sql*plus

Normally whenever we run any SQL comamnd the output of it came all in a row. If the command return 1000 rows then it shows all on the console. You may also could not be able previous output by scrolling. This may be annoying. In order to display output page by page you have to set two variable.

SET PAGESIZE 30 --which will say in a page how many lines will be displayed.
SET PAUSE ON --which will pause the output to display unless you press enter.

Now run sql command like SELECT OBJECT_NAME FROM DBA_OBJECTS; and proceed further after pressing enter. Every new pressing of ENTER keyword will display you a new page.


Saving Changes to database automatically

With the Sql*plus variable AUTOCOMMIT option you can decide when to commit and when to not. By default it is set to OFF which means committing transaction is done whenever you give COMMIT explicitly. If you want committing will happen immediately then invoke,
SET AUTOCOMMIT ON

In order to do committing after 10 DML statements do,
SET AUTOCOMMIT 10
To turn the autocommit feature off again, enter
SET AUTOCOMMIT OFF

Sql*plus Error messages
Sql*plus errors begin will letter SP2.
Like,
SQL> @testing.sql
SP2-0310: unable to open file "testing.sql"

Sql*plus copy command error messages start with CPY.

Sql*plus Basics and Tips

The Sql Buffer
The Sql buffer stores most recently SQL command or most recently PL/SQL block.

SQL*Plus does not store SQL*Plus commands, or the semicolon or slash characters
you type to execute a command in the SQL buffer.

You can execute the command or block in the SQL buffer using the RUN or /(slash)
commands.

RUN commands displays the command or block in the buffer and then execute it.
/(slash) executes the command or block in the buffer but does not display it.

Below is the example.
A simple SQL command.
SQL> select sysdate from dual;
SYSDATE
---------
29-NOV-08

This one (Setting linesize) is SQL*plus command and hence is not stored in SQL buffer.
SQL> set linesize 100

Invoking list will show SQL buffer, not SQL*plus command.

SQL> list
1* select sysdate from dual

Run will execute the command after displaying it.
SQL> run
1* select sysdate from dual

SYSDATE
---------
29-NOV-08

/(Slash) will not display the command. It will only execute it.
SQL> /

SYSDATE
---------
29-NOV-08

Continuing a Long SQL*Plus Command on Additional Lines
Suppose you want to write a long sql*plus command or a sql command, a long identifier or a long word in the SQL*plus. You can put them in one line by entering hyphen(-) at the end and then entering a carriage return. SQL*Plus displays a right angle-bracket (>) as a prompt for each additional line.

So hyphen(-) in Sql*plus is considered as continuation character. For example to format a long column you can use hyphen in sql*plus.
SQL> column salary format 99,999 -
> heading 'Monthly Salary'

Similarly,
SQL> select 200--
> 100 from dual;


200-100
----------
100
SQL> select 200-
> -100 from dual;


200-100
----------
100
Hyphen at the last is considered as continuation character. So hyphen at the last is ignored. Hence following will return error.
SQL> select 200-
> 100 from dual;

select 200 100 from dual
*
ERROR at line 1:
ORA-00923: FROM keyword not found where expected

Stopping a Command while it is Running
In order to stop displaying a long running output on Sql*plus press CTRL+C.

Displaying output page by page in Sql*plus

Normally whenever we run any SQL comamnd the output of it came all in a row. If the command return 1000 rows then it shows all on the console. You may also could not be able previous output by scrolling. This may be annoying. In order to display output page by page you have to set two variable.

SET PAGESIZE 30 --which will say in a page how many lines will be displayed.
SET PAUSE ON --which will pause the output to display unless you press enter.

Now run sql command like SELECT OBJECT_NAME FROM DBA_OBJECTS; and proceed further after pressing enter. Every new pressing of ENTER keyword will display you a new page.


Saving Changes to database automatically

With the Sql*plus variable AUTOCOMMIT option you can decide when to commit and when to not. By default it is set to OFF which means committing transaction is done whenever you give COMMIT explicitly. If you want committing will happen immediately then invoke,
SET AUTOCOMMIT ON

In order to do committing after 10 DML statements do,
SET AUTOCOMMIT 10
To turn the autocommit feature off again, enter
SET AUTOCOMMIT OFF

Sql*plus Error messages
Sql*plus errors begin will letter SP2.
Like,
SQL> @testing.sql
SP2-0310: unable to open file "testing.sql"

Sql*plus copy command error messages start with CPY.

Working with Sql*plus Scripts

Running Sql Scripts
To run a sql scripts in Sql*plus use @ or @@ or start command. To run a script named C:\test.sql use,
@c:\test
or,
@@c:\test
or,
start c:\test


In addition,
CLEAR BUFFER (CL BUFF): Will clear buffer, i.e delete all lines from the Sql buffer.

To insert a line before line 1, enter a zero ("0") and follow the zero with text.
0 SELECT Username

Placing comments in Sql Scripts
1)Using Sql*plus REMARK command: If a line starts with REMARK rest words of the line are considered as comment and hence ignored to compile. This is applied to single line only.
Example:
REMARK This script will generate the salary history
REM This script is generated on 11/01/2008


2)Using /*...*/: In order to comment multiple lines start with /* and until it gets */ entire commands inside it are considered as comments. With /*...*/ you can also comment a single line.
You must enter a space after the slash-asterisk(/*).
Example:
/* Column Salary for 9999
Column Name for a30
*/


3)Using --: You can use ANSI/ISO "- -" style comments within SQL statements, PL/SQL blocks, or SQL*Plus commands. This comment cannot span multiple lines.
To span it multiple lines start each line with --.
Example:
--This is a comment.

Issues while placing comments in Sql Scripts

SQL*Plus does not have a SQL or PL/SQL command parser.

It scans the first few keywords of each new statement to determine the command type, whether it is SQL, PL/SQL or SQL*Plus.

Comments in some locations can prevent SQL*Plus from correctly identifying the command type, and thus give unexpected results.

Rules
1)Do not put comments within the first few keywords of a statement. Beow is an example. In first case I commented before procedure keyword and thus PL/SQL block terminates before it is expected and in the second case, it is successfully created because of right select for comment.

SQL> create or replace
2 /* This is a comment */
3 procedure test_proc as
4 begin
5 dbms_output.put_line('Test Comment');

Warning: Procedure created with compilation errors.

SQL> end;
SP2-0042: unknown command "end" - rest of line ignored.
SQL> /

Warning: Procedure created with compilation errors.

SQL> create or replace procedure
2 /* This is a comment */
3 test_proc as
4 begin
5 dbms_output.put_line('Test Comment');
6 end;
7 /

Procedure created.

2)Do not put comments after statement terminators (period, semicolon or slash).
Example:
SQL> select sysdate from dual; --This is a comment
2
SQL> /
select sysdate from dual; --This is a comment
*
ERROR at line 1:
ORA-00911: invalid character

3)Don't put statement termination character at the end of the comment. For example,
select sysdate
--This is comment;
from dual;


statement will fail because statement termination occurs in the comments.
SQL> select sysdate from dual; --This is a comment
2
SQL> /
select sysdate from dual; --This is a comment
*
ERROR at line 1:
ORA-00911: invalid character


SQL> select sysdate
2 --This is comment;
--This is comment
*
ERROR at line 2:
ORA-00923: FROM keyword not found where expected

SQL> from dual;
SP2-0042: unknown command "from dual" - rest of line ignored.

However, if we omit semicolon from comment then statement successfully executed.
SQL> select sysdate
2 --This is comment
3 from dual;


SYSDATE--
---------
29-NOV-08

4)Do not use ampersand characters '&' in comments in a SQL statement or PL/SQL
block. Because & is identified as substitution variable unless if you set SET DEFINE OFF to prevent scanning for the substitution character.

Example:
SQL> select sysdate
2 --Comment &
3 from dual;

SP2-0317: expected symbol name is missing

SYSDATE--
---------
29-NOV-08

SQL> set define off
SQL> select sysdate
2 --Comment &
3 from dual;


SYSDATE--
---------
29-NOV-08

Working with Sql*plus Scripts

Running Sql Scripts
To run a sql scripts in Sql*plus use @ or @@ or start command. To run a script named C:\test.sql use,
@c:\test
or,
@@c:\test
or,
start c:\test


In addition,
CLEAR BUFFER (CL BUFF): Will clear buffer, i.e delete all lines from the Sql buffer.

To insert a line before line 1, enter a zero ("0") and follow the zero with text.
0 SELECT Username

Placing comments in Sql Scripts
1)Using Sql*plus REMARK command: If a line starts with REMARK rest words of the line are considered as comment and hence ignored to compile. This is applied to single line only.
Example:
REMARK This script will generate the salary history
REM This script is generated on 11/01/2008


2)Using /*...*/: In order to comment multiple lines start with /* and until it gets */ entire commands inside it are considered as comments. With /*...*/ you can also comment a single line.
You must enter a space after the slash-asterisk(/*).
Example:
/* Column Salary for 9999
Column Name for a30
*/


3)Using --: You can use ANSI/ISO "- -" style comments within SQL statements, PL/SQL blocks, or SQL*Plus commands. This comment cannot span multiple lines.
To span it multiple lines start each line with --.
Example:
--This is a comment.

Issues while placing comments in Sql Scripts

SQL*Plus does not have a SQL or PL/SQL command parser.

It scans the first few keywords of each new statement to determine the command type, whether it is SQL, PL/SQL or SQL*Plus.

Comments in some locations can prevent SQL*Plus from correctly identifying the command type, and thus give unexpected results.

Rules
1)Do not put comments within the first few keywords of a statement. Beow is an example. In first case I commented before procedure keyword and thus PL/SQL block terminates before it is expected and in the second case, it is successfully created because of right select for comment.

SQL> create or replace
2 /* This is a comment */
3 procedure test_proc as
4 begin
5 dbms_output.put_line('Test Comment');

Warning: Procedure created with compilation errors.

SQL> end;
SP2-0042: unknown command "end" - rest of line ignored.
SQL> /

Warning: Procedure created with compilation errors.

SQL> create or replace procedure
2 /* This is a comment */
3 test_proc as
4 begin
5 dbms_output.put_line('Test Comment');
6 end;
7 /

Procedure created.

2)Do not put comments after statement terminators (period, semicolon or slash).
Example:
SQL> select sysdate from dual; --This is a comment
2
SQL> /
select sysdate from dual; --This is a comment
*
ERROR at line 1:
ORA-00911: invalid character

3)Don't put statement termination character at the end of the comment. For example,
select sysdate
--This is comment;
from dual;


statement will fail because statement termination occurs in the comments.
SQL> select sysdate from dual; --This is a comment
2
SQL> /
select sysdate from dual; --This is a comment
*
ERROR at line 1:
ORA-00911: invalid character


SQL> select sysdate
2 --This is comment;
--This is comment
*
ERROR at line 2:
ORA-00923: FROM keyword not found where expected

SQL> from dual;
SP2-0042: unknown command "from dual" - rest of line ignored.

However, if we omit semicolon from comment then statement successfully executed.
SQL> select sysdate
2 --This is comment
3 from dual;


SYSDATE--
---------
29-NOV-08

4)Do not use ampersand characters '&' in comments in a SQL statement or PL/SQL
block. Because & is identified as substitution variable unless if you set SET DEFINE OFF to prevent scanning for the substitution character.

Example:
SQL> select sysdate
2 --Comment &
3 from dual;

SP2-0317: expected symbol name is missing

SYSDATE--
---------
29-NOV-08

SQL> set define off
SQL> select sysdate
2 --Comment &
3 from dual;


SYSDATE--
---------
29-NOV-08