Monday, 12 August 2013
Tuesday, 9 July 2013
Oracle: Cursors
A cursor is a mechanism by which you can assign a name to a "select statement" and manipulate the information within that SQL statement.
Oracle/PLSQL: Declare a Cursor
A cursor is a SELECT statement that is defined within the declaration section of your PLSQL code. We'll take a look at three different syntaxes for cursors.
Cursor without parameters (simplest)
The basic syntax for a cursor without parameters is:
CURSOR cursor_name
IS
SELECT_statement;
Cursor with parameters
The basic syntax for a cursor with parameters is:
CURSOR cursor_name (parameter_list)
IS
SELECT_statement;
Oracle/PLSQL: OPEN Statement
Once you've declared your cursor, the next step is to open the cursor.
The basic syntax to OPEN the cursor is:
OPEN cursor_name;
Oracle/PLSQL: FETCH Statement
The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that some type of operation can be performed on the data. After declaring and opening your cursor, the next step is to FETCH the rows from your cursor.
The basic syntax for a FETCH statement is:
FETCH cursor_name INTO <list of variables>;
Oracle/PLSQL: CLOSE Statement
The final step of working with cursors is to close the cursor once you have finished using it.
The basic syntax to CLOSE the cursor is:
CLOSE cursor_name;
Oracle/PLSQL: Declare a Cursor
A cursor is a SELECT statement that is defined within the declaration section of your PLSQL code. We'll take a look at three different syntaxes for cursors.
Cursor without parameters (simplest)
The basic syntax for a cursor without parameters is:
CURSOR cursor_name
IS
SELECT_statement;
Cursor with parameters
The basic syntax for a cursor with parameters is:
CURSOR cursor_name (parameter_list)
IS
SELECT_statement;
Oracle/PLSQL: OPEN Statement
Once you've declared your cursor, the next step is to open the cursor.
The basic syntax to OPEN the cursor is:
OPEN cursor_name;
Oracle/PLSQL: FETCH Statement
The purpose of using a cursor, in most cases, is to retrieve the rows from your cursor so that some type of operation can be performed on the data. After declaring and opening your cursor, the next step is to FETCH the rows from your cursor.
The basic syntax for a FETCH statement is:
FETCH cursor_name INTO <list of variables>;
Oracle/PLSQL: CLOSE Statement
The final step of working with cursors is to close the cursor once you have finished using it.
The basic syntax to CLOSE the cursor is:
CLOSE cursor_name;
Oracle: Transactions
We've divided Transactions into the following topics:
Commit
Rollback
Set Transaction
Lock Table
Oracle/PLSQL: Commit
The syntax for the COMMIT statement is:
COMMIT [WORK] [COMMENT text];
The Commit statement commits all changes for the current session. Once a commit is issued, other users will be able to see your changes.
Oracle/PLSQL: Rollback
The syntax for the ROLLBACK statement is:
ROLLBACK [WORK] [TO [SAVEPOINT] savepoint_name];
The Rollback statement undoes all changes for the current session up to the savepoint specified. If no savepoint is specified, then all changes are undone.
Commit
Rollback
Set Transaction
Lock Table
Oracle/PLSQL: Commit
The syntax for the COMMIT statement is:
COMMIT [WORK] [COMMENT text];
The Commit statement commits all changes for the current session. Once a commit is issued, other users will be able to see your changes.
Oracle/PLSQL: Rollback
The syntax for the ROLLBACK statement is:
ROLLBACK [WORK] [TO [SAVEPOINT] savepoint_name];
The Rollback statement undoes all changes for the current session up to the savepoint specified. If no savepoint is specified, then all changes are undone.
Thursday, 4 July 2013
Oracle: Grant/Revoke Privileges example
You can grant users various privileges to tables. These privileges can be any combination of select, insert, update, delete, references, alter, and index
To view the grant permission of all tables:
select *from user_tab_privs;
Grant permission:
grant select, insert on sales to test with grant option;
here.,
tiara-Grantor
sales-table name
test-Grantee
Revoke Permission:
revoke all on sales from test;
Login in as "test" user:
To view the grantor tables:
select *from tiara.sales;
Insert values in grantor tables:
insert into tiara.sales values(41411,'Ganesh','Chennai','Tamilnadu',444);
To view the grant permission of all tables:
select *from user_tab_privs;
Grant permission:
grant select, insert on sales to test with grant option;
here.,
tiara-Grantor
sales-table name
test-Grantee
Revoke Permission:
revoke all on sales from test;
Login in as "test" user:
To view the grantor tables:
select *from tiara.sales;
Insert values in grantor tables:
insert into tiara.sales values(41411,'Ganesh','Chennai','Tamilnadu',444);
Tuesday, 2 July 2013
Oracle: Function
Table Creation:
create table ebbill(cusno number,cus_name varchar2(20),init_read number(5),cur_read number(5),category varchar2(20));
alter table ebbill add total number(5);
Creating Function
create or replace function fun(i number,j number)
return number is s number;
begin
s:=i-j;
return s;
end;
Calling a function
declare
tno ebbill.cusno%type;
tname ebbill.cus_name%type;
initial ebbill.init_read%type;
current ebbill.cur_read%type;
category ebbill.category%type;
t number(6,2);
cursor cc is
select cusno,cus_name,init_read,cur_read,category from ebbill;
begin
open cc;
if cc%isopen then
loop
fetch cc into tno,tname,initial,current,category;
exit when cc%notfound;
if cc%found then
t:=fun(current,initial);
update ebbill set total=t where tno=ebbill.cusno;
end if;
end loop;
end if;
close cc;
end;
/
create table ebbill(cusno number,cus_name varchar2(20),init_read number(5),cur_read number(5),category varchar2(20));
alter table ebbill add total number(5);
Creating Function
create or replace function fun(i number,j number)
return number is s number;
begin
s:=i-j;
return s;
end;
Calling a function
declare
tno ebbill.cusno%type;
tname ebbill.cus_name%type;
initial ebbill.init_read%type;
current ebbill.cur_read%type;
category ebbill.category%type;
t number(6,2);
cursor cc is
select cusno,cus_name,init_read,cur_read,category from ebbill;
begin
open cc;
if cc%isopen then
loop
fetch cc into tno,tname,initial,current,category;
exit when cc%notfound;
if cc%found then
t:=fun(current,initial);
update ebbill set total=t where tno=ebbill.cusno;
end if;
end loop;
end if;
close cc;
end;
/
Monday, 1 July 2013
Oracle: Stored Procedure
Before executing a procedure in "Run SQL Command Line" we must execute the below query to display the server result in screen.SQL> set serveroutput on;
Example 1:
Stored Procedure
create procedure addn(a in number,b in number,c in out number)
is
begin
c:=a+b;
end;
/
Calls from PL/SQL
declare
a integer:=10;
b integer:=10;
c integer;
begin
addn(a,b,c);
dbms_output.put_line(c);
end;
/
Example 2:
Stored Procedure
create or replace procedure findmax(a in number,b in number,c in out number)
is
begin
if a>b then
c:=a;
else
c:=b;
end if;
end;
/
Calls from PL/SQL
declare
a integer:=10;
b integer:=21;
c integer;
begin
findmax(a,b,c);
dbms_output.put_line(c);
end;
/
Example 3:
Table SQL
CREATE TABLE DBUSER (
USER_ID NUMBER (5) NOT NULL,
USERNAME VARCHAR2 (20) NOT NULL,
CREATED_BY VARCHAR2 (20) NOT NULL,
CREATED_DATE DATE NOT NULL,
PRIMARY KEY ( USER_ID )
)
Insert Query
INSERT INTO DBUSER VALUES(11145,'Manoj','Admin',sysdate);
Stored Procedure
CREATE OR REPLACE PROCEDURE getDBUSERByUserId(
p_userid IN DBUSER.USER_ID%TYPE,
o_username OUT DBUSER.USERNAME%TYPE,
o_createdby OUT DBUSER.CREATED_BY%TYPE,
o_date OUT DBUSER.CREATED_DATE%TYPE)
IS
BEGIN
SELECT USERNAME , CREATED_BY, CREATED_DATE
INTO o_username, o_createdby, o_date
FROM DBUSER WHERE USER_ID = p_userid;
END;
/
Calls from PL/SQL
DECLARE
o_username DBUSER.USERNAME%TYPE;
o_createdby DBUSER.CREATED_BY%TYPE;
o_date DBUSER.CREATED_DATE%TYPE;
BEGIN
getDBUSERByUserId(11144,o_username,o_createdby,o_date);
DBMS_OUTPUT.PUT_LINE('username : ' || o_username);
DBMS_OUTPUT.PUT_LINE('createdby : ' || o_createdby);
DBMS_OUTPUT.PUT_LINE('createddate : ' || o_date);
END;
/
Oracle: Sequences (Autonumber)
In Oracle, you can create an autonumber field by using sequences. A sequence is an object in Oracle that is used to generate a number sequence. This can be useful when you need to create a unique number to act as a primary key.
The syntax for a sequence is:
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
For example:
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
This would create a sequence object called supplier_seq. The first sequence number that it would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will cache up to 20 values for performance.
If you omit the MAXVALUE option, your sequence will automatically default to:
MAXVALUE 999999999999999999999999999
So you can simplify your CREATE SEQUENCE command as follows:
CREATE SEQUENCE supplier_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
Now that you've created a sequence object to simulate an autonumber field, we'll cover how to retrieve a value from this sequence object. To retrieve the next value in the sequence order, you need to use nextval.
For example:
supplier_seq.nextval
This would retrieve the next value from supplier_seq. The nextval statement needs to be used in an SQL statement. For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(supplier_seq.nextval, 'Kraft Foods');
This insert statement would insert a new record into the suppliers table. The supplier_id field would be assigned the next number from the supplier_seq sequence. The supplier_name field would be set to Kraft Foods.
Note:
The CACHE option specifies how many sequence values will be stored in memory for faster access.
The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.
NOCACHE means that none of the sequence values are stored in memory. This option may sacrifice some performance, however, you should not encounter a gap in the assigned sequence values.
The syntax for a sequence is:
CREATE SEQUENCE sequence_name
MINVALUE value
MAXVALUE value
START WITH value
INCREMENT BY value
CACHE value;
For example:
CREATE SEQUENCE supplier_seq
MINVALUE 1
MAXVALUE 999999999999999999999999999
START WITH 1
INCREMENT BY 1
CACHE 20;
This would create a sequence object called supplier_seq. The first sequence number that it would use is 1 and each subsequent number would increment by 1 (ie: 2,3,4,...}. It will cache up to 20 values for performance.
If you omit the MAXVALUE option, your sequence will automatically default to:
MAXVALUE 999999999999999999999999999
So you can simplify your CREATE SEQUENCE command as follows:
CREATE SEQUENCE supplier_seq
MINVALUE 1
START WITH 1
INCREMENT BY 1
CACHE 20;
Now that you've created a sequence object to simulate an autonumber field, we'll cover how to retrieve a value from this sequence object. To retrieve the next value in the sequence order, you need to use nextval.
For example:
supplier_seq.nextval
This would retrieve the next value from supplier_seq. The nextval statement needs to be used in an SQL statement. For example:
INSERT INTO suppliers
(supplier_id, supplier_name)
VALUES
(supplier_seq.nextval, 'Kraft Foods');
This insert statement would insert a new record into the suppliers table. The supplier_id field would be assigned the next number from the supplier_seq sequence. The supplier_name field would be set to Kraft Foods.
Note:
The CACHE option specifies how many sequence values will be stored in memory for faster access.
The downside of creating a sequence with a cache is that if a system failure occurs, all cached sequence values that have not be used, will be "lost". This results in a "gap" in the assigned sequence values. When the system comes back up, Oracle will cache new numbers from where it left off in the sequence, ignoring the so called "lost" sequence values.
NOCACHE means that none of the sequence values are stored in memory. This option may sacrifice some performance, however, you should not encounter a gap in the assigned sequence values.
Subscribe to:
Posts (Atom)
Kiro - Core Features
What is Kiro Kiro is an innovative AI-powered IDE that revolutionizes software development through intelligent assistance and structured wor...
-
#Sample thislist = [ "apple" , "banana" , "cherry" ] print ( "MyList : " , thislist ) thislist...
-
"If-Then-Else" expressions, example: for $x in doc("books.xml")/bookstore/book return if ($x/@category="CHIL...
-
CREATE INDEX sync_idx_abc ON schemaname.tablename(columnname);
