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

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);

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;
/

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.

Oracle: Change a user's password

To change a user's password in Oracle, you need to execute the alter user command.

The syntax for changing a password is:

alter user user_name identified by new_password;


  • user_name is the user whose password you wish to change.
  • new_password is the new password to assign.


For Example

If you wanted to reset the password for a user named smithj, and you wanted to set the new password to autumn, you would run the following command:

alter user smithj identified by autumn;

Oracle: Grant/Revoke Privileges

Grant Privileges on Tables

You can grant users various privileges to tables. These privileges can be any combination of select, insert, update, delete, references, alter, and index. Below is an explanation of what each privilege means.

Description


  1. Select Ability to query the table with a select statement.
  2. Insert Ability to add new rows to the table with the insert statement.
  3. Update Ability to update rows in the table with the update statement.
  4. Delete Ability to delete rows from the table with the delete statement.
  5. References Ability to create a constraint that refers to the table.
  6. Alter         Ability to change the table definition with the alter table statement.
  7. Index Ability to create an index on the table with the create index statement.

The syntax for granting privileges on a table is:

grant privileges on object to user;

For example, if you wanted to grant select, insert, update, and delete privileges on a table called suppliers to a user name smithj, you would execute the following statement:

grant select, insert, update, delete on suppliers to smithj;

You can also use the all keyword to indicate that you wish all permissions to be granted. For example:

grant all on suppliers to smithj;

If you wanted to grant select access on your table to all users, you could grant the privileges to the public keyword. For example:

grant select on suppliers to public;

Revoke Privileges on Tables

Once you have granted privileges, you may need to revoke some or all of these privileges. To do this, you can execute a revoke command. You can revoke any combination of select, insert, update, delete, references, alter, and index.

The syntax for revoking privileges on a table is:

revoke privileges on object from user;

For example, if you wanted to revoke delete privileges on a table called suppliers from a user named anderson, you would execute the following statement:

revoke delete on suppliers from anderson;

If you wanted to revoke all privileges on a table, you could use the all keyword. For example:

revoke all on suppliers from anderson;

If you had granted privileges to public (all users) and you wanted to revoke these privileges, you could execute the following statement:

revoke all on suppliers from public;

Grant Privileges on Functions/Procedures

When dealing with functions and procedures, you can grant users the ability to execute these functions and procedures. The Execute privilege is explained below:

Description

Execute
      1. Ability to compile the function/procedure.
      2. Ability to execute the function/procedure directly.

The syntax for granting execute privileges on a function/procedure is:

grant execute on object to user;

For example, if you had a function called Find_Value and you wanted to grant execute access to the user named smithj, you would execute the following statement:

grant execute on Find_Value to smithj;

If you wanted to grant all users the ability to execute this function, you would execute the following:

grant execute on Find_Value to public;


Revoke Privileges on Functions/Procedures

Once you have granted execute privileges on a function or procedure, you may need to revoke these privileges from a user. To do this, you can execute a revoke command.

The syntax for the revoking privileges on a function or procedure is:

revoke execute on object from user;

If you wanted to revoke execute privileges on a function called Find_Value from a user named anderson, you would execute the following statement:

revoke execute on Find_Value from anderson;

If you had granted privileges to public (all users) and you wanted to revoke these privileges, you could execute the following statement:

revoke execute on Find_Value from public;

DBT - Models

Models are where your developers spend most of their time within a dbt environment. Models are primarily written as a select statement and ...