You are on page 1of 40

What is difference between a PROCEDURE & FUNCTION ?

Page 1 of 5

A FUNCTION is always returns a value using the return statement. A PROCEDURE may return one or more values through parameters or may not return at all.

A function can be called from sql statements and queries while procedure can be called in a begin end block only.

Posted by: Jyoti

Contact Jyoti

In case of function,it must have return type. In case of procedure,it may or may n't have return type. In case of function, only it takes IN parameters IN case of procedure only it take IN,OUT,INOUT parameters..

Posted by: sudipta das

Contact sudipta das

A function always return a values while procedure can return one or more values through Parameters. A function can call directly by sql statement like select func_name from dual while procedure cannot.

Posted by: Madhavi

Contact Madhavi

A FUNCTION always returns a value using the return statement. And a FUNCTION can also use OUT parameter to return a value. But Return statment must be there. A PROCEDURE may return one or more values through parameters or may not return at all. A function can be called from sql stmt and pl/sql stmt. A procedure can be called within pl/sql stmt.

http://www.coolinterview.com/interview/3680/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between a PROCEDURE & FUNCTION ?

Page 2 of 5

In case of function,it must have return type. In case of procedure, it don't need return keyword.

Posted by: RISHI RAWAT

Contact RISHI RAWAT

Functions and Procedures both can return values using OUT parameter. But Functions has return clause to return value and Procedures does not have return statement. Functions can be used in the sql statement and can be used as the part of the expression but Procedures can't be used.

Posted by: Sanjay Patel

Contact Sanjay Patel

Function can have both IN and OUT parameter. Procedure cannot have return type but may return value through OUT parameter.

Posted by: Always Learner

Contact Always Learner

FUNCTION: -It must have return type. -It mainly used for calculation purpose. -It is part of PL/SQL block.(that is return value to block for exection of remaining code) PROCEDURE: -It may or may not return value. -It is mainly to perform a task. -It is a complete PL/SQL block. -It is used to perform DML operation.

http://www.coolinterview.com/interview/3680/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between a PROCEDURE & FUNCTION ?

Page 3 of 5

Posted by: Bharadwaj

Contact Bharadwaj

procedure is called as part of a statement..function is called as part of an expression. There are some purity level for a function..procedure dont have such restrictions.

Posted by: Bikash

Contact Bikash

We use function when we wnt to compute or calculate , where as procedure use for an action

Posted by: shashank gautam

Contact shashank gautam

Difference Between Function and Procedure 1.Function get terminated if error occure while Procedure continue. 2. Function compiled every time on calling but Procedure compiled one time and stored in compiled format for furthur use.

Posted by: Md Raquib Alam

Contact Md Raquib Alam

1) Function has to Return One Value. Howecer procedure may or may not return any values.we can return the values via procedure using OUT/IN OUT varaibles. 2)Unlike Procedures We can call function in a Select statement. Exception being we can't use those functions in select which has OUT varaibles. 3) We can call functions during declaration itself. e.g declare x number := func(); ---that is all i know.

Posted by: Jaidev Thakur

http://www.coolinterview.com/interview/3680/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between a PROCEDURE & FUNCTION ?

Page 4 of 5

Contact Jaidev Thakur

Functions Compute some value Should return a value It must return only one value Not possible to use OUT and IN OUT parameters, only IN parameter is allowed. We can able to call functions in SELECT statement

Posted by: priya

Contact priya

Functions Compute some value Should return a value It must return only one value Not possible to use OUT and IN OUT parameters, only IN parameter is allowed. We can able to call functions in SELECT statement Procedures Do some actions May return a value or may not Possible to return more than one value Possible to use IN, OUT and IN OUT parameters We cannot able to call in SELECT statement

Posted by: priya

Contact priya

http://www.coolinterview.com/interview/3680/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between a PROCEDURE & FUNCTION ?

Page 5 of 5

function is a collection of sql and plsql statments in function we can assign the return type dataype in procedure we can not assign the return type datatype with using function we can return only one output. wih using procedure we can return multiple output using parameters. in function whenever declaring a variable it's type of in parameter in procedure variable type is in inout out

Posted by: abhay

Contact abhay

Procedures: 1. Execute as a PL/SQL statement. 2. Do not contain RETURN clause in the header. 3. Can return none, one, or many values. 4. Can contain a RETURN statement. Function: 1. Invoke as part of an expression 2. Must contain a RETURN clause in the header 3. Must return a single value 4. Must contain at least one RETURN

http://www.coolinterview.com/interview/3680/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is Raise_application_error ?

Page 1 of 2

Raise_application_error is a procedure of package DBMS_STANDARD which allows to issue an user_defined error messages from stored sub-program or database trigger.

RAISE_APPLICATION_ERROR is a procedure of package DBMS which allows to issue user_defined error message.

Posted by: sudipta das

Contact sudipta das

The RAISE_APPLICATION_ERROR is a procedure defined by Oracle that allows the developer to raise an exception and associate an error number and message with the procedure other than just Oracle errors.

Posted by: Moumita Nandi

Contact Moumita Nandi

It is Its a STANDARD procedure to communicate with a pre defined exception interactively by returning a nonstandard error code and error message. error_number : between -20000 to -20999 message : 2048 bytes This can be used in both execution and exception setion; Ex: 1 : ..... BEGIN ... delete from employees where manager_id = v_mgr; if SQL%NOTFOUND then RAISE_APPLICATION_ERROR(-20020,'This is not a valid manager'); end if; .... --------------------------ex:2 : ...... EXCEPTION when NO_DATA_FOUND then

http://www.coolinterview.com/interview/3684/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is Raise_application_error ?

Page 2 of 2

RAISE_APPLICATION_ERROR(-20201,'manager is not a valid employee'); END;

http://www.coolinterview.com/interview/3684/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Name the tables where characteristics of Package, procedure and functions are stored ?

Page 1 of 1

User_objects, User_Source and User_error.

user_source,user_objects

Posted by: madhavi

Contact madhavi

characteristics of DB objects are stored in data dictionary. Which can be accessed by Data dict. views like (user/all/DBA_objects/source).

Posted by: pankaj

Contact pankaj

user_source,user_objects

Posted by: suvrat

Contact suvrat

http://www.coolinterview.com/interview/3669/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between stored procedures and application procedures,stored function ... Page 1 of 2

Stored procedures are sub programs stored in the database and can be called & execute multiple times where in an application procedure is the one being used for a particular application same is the way for function

Stored Procedure/Function is a compiled database object, which is used for fast response from Oracle Engine.Difference is Stored Procedure must return multiple value and function must return single value .

Posted by: Himanshu Kumar Tripathi

Contact Himanshu Kumar Tripathi

Procedure:Execute as a PL/SQL statement,No RETURN clause in the header,Can return none, one, or many values,Can contain a RETURN statement Function:-Invoke as part of an expression,Must contain a RETURN clause in the header,Must return a single value,Must contain at least one RETURN statement

Posted by: Dev

Contact Dev

Stored procedures are sub programs stored in the database and can be called & execute multiple times where in an application procedure is the one being used for a particular application same is the way for function Stored Procedure/Function is a compiled database object, which is used for fast response from Oracle Engine.Difference is Stored Procedure must return multiple value and function must return single value .

Posted by: kiran

Contact kiran

The basic difference between function and procedure is that a procedure can never be used in a sql statement while a function can be used

http://www.coolinterview.com/interview/10954/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between stored procedures and application procedures,stored function ... Page 2 of 2

Posted by: paritosh

Contact paritosh

Difference between Stored Procedure and Application Procedure: Stored Procedure is a named PLSQL block that will be compiled and used for further use. If maintained across Servers, they can be used for different applications and Users. Application Procedures are the one which will be used for local apps, this can be called local block of code. Apart from technical characteristics, same will apply to the differences between Stored Functions and Application Functions. Difference between Procedures and Functions: a. Functions can be used in a select statement where as procedures cannot b. Procedure takes both input and output parameters but Functions takes only input parameters c. Functions cannot return values of type text, ntext, image & timestamps where as procedures can d. Functions can be used as user defined datatypes in create table but procedures cannot ***Eg:-create table <tablename>(name varchar(10),salary getsal(name)) Here getsal is a user defined function which returns a salary type, when table is created no storage is allotted for salary type, and getsal function is also not executed, But when we are fetching some values from this table, getsal function get?s executed and the return Type is returned as the result set.

http://www.coolinterview.com/interview/10954/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

State the difference between implicit and explicit cursor's.

Page 1 of 2

Implicit Cursor are declared and used by the oracle internally. whereas the explicit cursors are declared and used by the user. more over implicitly cursors are no need to declare oracle creates and process and closes autometically. the explicit cursor should be declared and closed by the user.

Implict cursor can be used to handle single record (i.e) the select query used should not yield more than one row. if u have handle more than one record then Explict cursor should be used.

Posted by: Dinesh

Contact Dinesh

It is not mandatory that when the query retrieves more than one record then explicit cursor should be used. If the number of records is less than 100 then implicit cursor should be used. e.g. set serverout on; begin for i in (select * from emp) loop dbms_output.put_line(i.empno); end loop; end; /

Posted by: Sukhamoy

Contact Sukhamoy

Cursor: It is automatically created by oracle for all sql dml statements including the query that returns one row. Explicit cursor: These are created and managed by the user.And used for multi row select statement.

Posted by: josu

Contact josu

Implicit cursors are oracle created and used internally by oracle,the only implicit cursor is sql.

http://www.coolinterview.com/interview/10955/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

State the difference between implicit and explicit cursor's.

Page 2 of 2

Explicit cursors are user created cursors.

Posted by: Ashish

Contact Ashish

Cursors are used for the purpose of storing the intermediate results when executing a SQL Query or a block of code... Implicit Cursors are by default declared by ORacle itself and hence there is no scope of storing resultset with more than one record where as Explicit Cursors are declared by the User and hence can define the parameters with measure to store the no of records.

http://www.coolinterview.com/interview/10955/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

How we can create a table in PL/SQL block. insert records into it??? is it possible by som... Page 1 of 2

CREATE OR REPLACE PROCEDURE ddl_create_proc (p_table_name IN VARCHAR2) AS l_stmt VARCHAR2(200); BEGIN DBMS_OUTPUT.put_line('STARTING '); l_stmt := 'create table '|| p_table_name || ' as (select * from emp )'; execute IMMEDIATE l_stmt; DBMS_OUTPUT.put_line('end '); EXCEPTION WHEN OTHERS THEN DBMS_OUTPUT.put_line('exception '||SQLERRM || 'message'||sqlcode); END;

We can create table in procedure as explained in above case. but we can't create or perform any DDL in functions.

Posted by: Vinod Pandit

Contact Vinod Pandit

the above given answer is correct. but we can also create or perform any ddl in functions. I have tested myself

Posted by: Mike

Contact Mike

we can create a table through pl/sql block with dbms_sql package and execute immediate statement. CREATE OR REPLACE PROCEDURE PROC_CREATE_NEW_TABLE(STMT VARCHAR2) IS

http://www.coolinterview.com/interview/10950/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

How we can create a table in PL/SQL block. insert records into it??? is it possible by som... Page 2 of 2

AUTHID CURRENT_USER BEGIN EXECUTE IMMEDIATE STMT; END; here AUTHID CURRENT_USER is used to create a table under current user privileges. through this procedure we can add our own fields of a table on run time,even it is not depended on structure of an existing table. Insertion record into the table, because it is DML operation so we can easily create a procedure with simple statements or execute immediate statement.

http://www.coolinterview.com/interview/10950/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Explain the usage of WHERE CURRENT OF clause in cursors ?

Page 1 of 3

WHERE CURRENT OF clause in an UPDATE,DELETE statement refers to the latest row fetched from a cursor. Database Triggers

Where CURRENT OF clause means cursor points to present row of the cursor

Posted by: sudipta das

Contact sudipta das

WHERE CURRENT OF clause in an UPDATE,DELETE statement refers to the latest row fetched from a cursor.

Posted by: Alagappan A M

Contact Alagappan A M

This clause use for update & delete most resent row with out help of ROWID and befour update lock the particular row in the base table.FOR UPDATE clause is manditory for this clause.

Posted by: siva kumar

Contact siva kumar

Take an example, Cursor is Select * from EMP; and in cursor we write Update Emp set sal = sal + 100; that means the fetching loops will execute as many times as rows returned by the cursor and the update will also be done for the same number of times. Now suppose, If we put a where clause in the update statement, then the result would also not be predictable. In the third case, we use the WHERE CURRENT OF clause in the where clause, this point out to the current row returned by the cursor. In that case, Oracle differentiate all the rows on the basis of RowID. But what happens if someone changes one row in the table.....that means the rowid would be changed. So we have to manually lock all the rows of that table by writing the FOR UPDATE/DELETE clause. So In case of WHERE CURRENT OF clause, we should use the FOR UPDATE/DELETE clause

http://www.coolinterview.com/interview/3693/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Explain the usage of WHERE CURRENT OF clause in cursors ?

Page 2 of 3

also. This is known as the Pessimistic/Manual Locking and this the major difference between the Implicit Cursors and Explicit Cursors.

Posted by: Sachin Nagpal

Contact Sachin Nagpal

UPDATE and DELETE statements can use a WHERE CURRENT OF clause if they reference a cursor declared FOR UPDATE. This syntax indicates that the UPDATE or DELETE should modify the current row identified by the FOR UPDATE cursor. The syntax is: [UPDATE | DELETE ] ... WHERE CURRENT OF cursor_name;By using WHERE CURRENT OF, you do not have to repeat the WHERE clause in the SELECT statement. For example: DECLARE CURSOR wip_cur IS SELECT acct_no, enter_date FROM wip WHERE enter_date < SYSDATE -7 FOR UPDATE; BEGIN FOR wip_rec IN wip_cur LOOP INSERT INTO acct_log (acct_no, order_date) VALUES (wip_rec.acct_no, wip_rec.enter_ date); DELETE FROM wip WHERE CURRENT OF wip_cur; END LOOP; END;

Posted by: sallepali nagarjun reddy

Contact sallepali nagarjun reddy

If your program has positioned the cussor on a row,u can update its data by using update statemnet with where current clause.

Posted by: priya

Contact priya

Where current of is used to delete or update the current row that is being processed in the cursor loop.

http://www.coolinterview.com/interview/3693/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Explain the usage of WHERE CURRENT OF clause in cursors ?

Page 3 of 3

Posted by: karthick VenkataRaman

Contact karthick VenkataRaman

WHERE CURRENT OF CLAUSE is used when referencing the current row from an explicit cursor. this clause allows to apply updates and deletes to the row currently being addressed,without the need to explicitly reference ROWID. We must use the FOR UPDATE clause in the cursor query, such thay the rows are locked on open.

http://www.coolinterview.com/interview/3693/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Explian rowid,rownum?What are the pseduocolumns we have?

Page 1 of 1

ROWID - Hexa decimal number each and every row having unique.Used in searching ROWNUM - It is a integer number also unique for sorting Normally TOP N Analysys. Other Psudo Column are NEXTVAL,CURRVAL Of sequence are some exampls

ROWID : It gives the hexadecimal string representing the address of a row. It gives the location in database where row is physically stored. ROWNUM: It gives a sequence number in which rows are retrieved from the database.

Posted by: jyothsna

Contact jyothsna

ROWID : Every record in a database is uniquely identified by system generated value called Rowid. It Is a 18 character hexma decimal value. These Rowid's are physically existence. ROWNUM : It is a pseduocolumn which generates the sequence of numeric values based on the position of the records in the output. These ROWNUM'S logically generated. NEXTVAL,CURRVAL,SYSDATE,LEVEL ARE PSEDUOCOLUMNS

Posted by: Satyanarayana Reddy velagala

http://www.coolinterview.com/interview/10953/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

State the advantage and disadvantage of Cursor?

Page 1 of 2

Advantage : In pl/sql if you want perform some actions more than one records you should user these cursors only. bye using these cursors you process the query records. you can easily move the records and you can exit from procedure when you required by using cursor attributes. disadvantage: using implicit/explicit cursors are depended by sutiation. if the result set is les than 50 or 100 records it is better to go for implicit cursors. if the result set is large then you should use exlicit cursors. other wise it will put burdon on cpu.

Disadvantage: Each time we fetch a row from the cursor, it result a network round trip, where as a normal select statement query make only one round trip.

Posted by: Praveen Jindal

Contact Praveen Jindal

The guy's answer at the top of this page about "Cursors" is probably not wrong but sounds wrong and it is hard to understand, it sounds like he has tried to type the answer in english but it is shameful, his english is totally broken, it is bad. You guys at coolinterview.com should do something about such posting to keep up the readership.

Posted by: good programmer

Contact good programmer

Advantage of cursor 1.Basically cursor is a logical place or it is active set. when a single query returns more than one rows in that case we can use the cursor. we don't need hit the database several time. Cursor always points the latest rows. Disadvantage of cursor 1.As such there is no disadvantage of cursor.

Posted by: sudipta das

Contact sudipta das

http://www.coolinterview.com/interview/10956/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

State the advantage and disadvantage of Cursor?

Page 2 of 2

Advantages: Cursors are somewhat like Pointers of Oracle, Oracle executes a query and assign some name to that memory location, which is known as Cursor. Cursors are used for storing data temperarorily. Cursors are used for storing, managing and retrieving data. Cursors stores in the RAM so the data access and retrieval is fast. Disadvantage: No such disadvantage. Only thing is that rather than storing data in an intermediate variable and fetching it one by one via cursor and then processing it sequentially, we can use Collections. By using collections, we can pass the bundle of Data at once.

Posted by: Sachin Nagpal

Contact Sachin Nagpal

Cursors adv: easier to retrieve a batch of records into the cursor and then perform manipulations on them before displaying the results. Disadvantage: The biggest disadvantage of cursors is that they eat up you RAM. If you're retrieving a large number of records into your cursor inside a procedure on an OLAP database, it will take a long long time to execute that procedure. The better alternative would be to retrieve directly from one or more table using selection criteria to eliminate records, and perform manipulation on each record on the fly.

Posted by: HarshDeep Singh

Contact HarshDeep Singh

cursor is nothing but it's a memory area of executing sql or oracle statements. Advantage: 1. We can retrieve more than one record by one query. 2. It's use our RAM so retrieval is fast. 3. By using Collection we can retrieve the bundle of data in cursor. Disadvantage: No such of disadvantage of cursor. When we use cursor one thing is necessary. When I have select only 50 or 100 rows we should use implicit cursor and when more record then we should use Explicit cursor.

Posted by: Amit Singh

http://www.coolinterview.com/interview/10956/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

How many types of database triggers can be specified on a table ? What are they ?

Page 1 of 1

Insert Update Delete Before Row o.k. o.k. o.k. After Row o.k. o.k. o.k. Before Statement o.k. o.k. o.k. After Statement o.k. o.k. o.k. If FOR EACH ROW clause is specified, then the trigger for each Row affected by the statement. If WHEN clause is specified, the trigger fires according to the returned Boolean value.

Row level trigger,Statement level trigger,Instead of trigger(created on views)

Posted by: shrinivas singh

Contact shrinivas singh

Baically there only 1 type of trigger which can be fired on the table. .i.e., DML TRIGGER. There are 14 types of DML TRIGGER But we can fire only 12 types of triggers, because remaining 2 types of triggers fire on View. 1. Before insert on ROW LEVEL TRIGGER 2. 1. AFTER insert on ROW LEVEL TRIGGER 3. Before insert on STATEMENT LEVEL TRIGGER 4. After insert on STATEMENT LEVEL TRIGGER 5. Before update on ROW LEVEL TRIGGER 6. After update on ROW LEVEL TRIGGER 7. Before update on STATEMENT LEVEL TRIGGER. 8. After update on STATEMENT LEVEL TRIGGER. 9. Before Delete on STATEMENT LEVEL TRIGGER. 10 After Delete on STATEMENT LEVEL TRIGGER. 11. Before Delete on ROW LEVEL TRIGGER. 12 After Delete on ROW LEVEL TRIGGER

Posted by: RISHI RAWAT

Contact RISHI RAWAT

http://www.coolinterview.com/interview/3691/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What are the Restrictions on Cursor Variables?

Page 1 of 1

Currently, cursor variables are subject to the following restrictions:You cannot declare cursor variables in a package spec. For example, the following declaration is not allowed:CREATE PACKAGE emp_stuff AS TYPE EmpCurTyp IS REF CURSOR RETURN emp%ROWTYPE; emp_cv EmpCurTyp; -- not allowedEND emp_stuff;You cannot pass cursor variables to a procedure that is called through a database link.If you pass a host cursor variable to PL/SQL, you cannot fetch from it on the server side unless you also open it there on the same server call.You cannot use comparison operators to test cursor variables for equality, inequality, or nullity.You cannot assign nulls to a cursor variable.Database columns cannot store the values of cursor variables. There is no equivalent type to use in a CREATE TABLE statement.You cannot store cursor variables in an associative array, nested table, or varray.Cursors and cursor variables are not interoperable; that is, you cannot use one where the other is expected. For example, you cannot reference a cursor variable in a cursor FOR loop

u cannot declare cursor in package specification not allowed when using db links cannot use comparison operators cannot assign NULL cursor's values cannot be stored in table columns cannot be used with associative array, nested tables and varray cannot be use one where the other is expected cannot reference a cursor variable in cursor FOR LOOP cannot direclty goto any columns

Posted by: NA

Contact NA

http://www.coolinterview.com/interview/10946/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Can Commit,Rollback ,Savepoint be used in Database Triggers?If yes than HOW? If no ...

Page 1 of 1

we cannot commit inside a trigger. As we all know that when a dml is complete one can issue a commit. A trigger if created is fired before the dml completes. so we cannot commit intermediately.

Posted by: kapil sawant

Contact kapil sawant

As workaround, one can use autonomous transactions. Autonomous transactions execute separate from the current transaction. Unlike regular triggers, autonomous triggers can contain COMMIT and ROLLBACK statements

Posted by: Jobs

Contact Jobs

Having Commit / Rollback inside a trigger defeats the standard of whole transaction's commit / rollback al together. Once trigger execution is complete then only a transaction can be said as complete and then only commit should take place. If we still want to carry out some action which should be initiated from trigger but should be commited irrespective of trigger completion / failure we can have AUTONOMUS TRANSACTION. Inside Automomus transaction block we can have Commit and it will act as actual commit. Specific queries a most welcome. D.

Posted by: Durgesh

Contact Durgesh

We can't perform two action at a time in a trigger i.e trigger action and coomit/rollback/save point.Otherwise deadlock situation occur as a result we will get a error in the transaction.

Posted by: Swagatika Sarangi

http://www.coolinterview.com/interview/13419/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 1 of 3

Trigger is an event driven PL/SQL block. Event may be any DML transaction.
database,

Cursor is a stored select statement for that current session. It will not be stored in the it is a logical component. Function is a set of PL/SQL statements or a PL/SQL block, which performs an operation and must return a value.

Posted by: Girish P

Contact Girish P

Cursor cursor is a private sql work area. Every sql statement executed by oracle server has an individual cursor associated with it. Cursor are two types 1.Implicit cursor 2.Explicit cursor Implicit cursor: Implicit cursors are declared by pl/sql implicitly at the time of DML statement and select statement in pl/sql including queries that returns single row. cursor have four attributes 1. SQL%ROWCOUNT 2. SQL%ISOPEN 3. SQL%NOTFOUND 4. SQL%FOUND SQL%ROWCOUNT-Basically it returns number. means number of rows affected by present sql statement. SQL%ISOPEN-Always evalutes false because implicit cursor automatically closed after execution of sql statement. SQL%FOUND-Always evalutes true because one or more rows are affected by recent sql statement SQL%NOTFOUND-Always evalutes true when no rows are affected by present sql statement. example of explicit cursor DECLARE v_empno employees.employee_id%TYPE; v_name employees.last_name%TYPE; CURSOR emp_cur IS SELECT employee_id,last_name FROM employees BEGIN OPEN emp_cur; LOOP

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 2 of 3

FETCH emp_cur INTO v_empno,v_name ; EXIT WHEN emp_cur%ROWCOUNT>10 OR emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE('employee_id:'||TO_CHAR(v_empno) 'employee_name:'||'v_name'); END LOOP; CLOSE emp_cur; END; Trigger:-Trigger is pl/sql block or procedure that is associated with table,view,schema and database. Execute immidiately when particular event take place. there are two types of trigger 1.Application trigger:fires automatically when event occurs with particular application. 2.Database trigger:Fires when data such as DML oparation occured at that time. DML triggers are two types 1.Statementlevel trigger 2.Rowlevel trigger statement level trigger-statement level trigger means trigger body execute once for the triggering event.this is default.A statement level trigger fire once even no rows are affected at all. Row level- Trigger body execute once for each row affected by triggering event.if no rows are affected in that case trigger body not executed. trigger example CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT ON employees BEGIN TO_CHAR(SYSDATE,'DY')IN('SUN','SAT') OR TO_CHAR((SYSDATE,'HH24:MI')NOT BETWEEN '08:00' AND '18:00')THEN RAISE_APPLICATION_ERROR(-20253,'u may insert employee information at business hrs'); END; ||

Posted by: Sudipta Das

Contact Sudipta Das

Cursor: cursor is temporary work area in pl/sql. Type of cusor: 1.Implict Cursor 2.Explicit Cursor 3.Cursor for loop 4.Cursor With Parametter

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 3 of 3

5.Cursor for Update 6.Ref Cursor Curosr Attributes: %foud %notfound %rowCount %isopen Trigger: Trigger is asscoiated with table or database event(when a table affect like dml operation that time occurs fire) Types of Trigger: we can write 12 types trigger

Posted by: raja

Contact raja

Trigger is a stored procedure that invokes automatically when the event occurs. We can write 12 types of triggers on table and 15 types of triggers on views. Cursor pointer or handler to the context area . 2 types of cursors are there implicit and explicit cursors.

Posted by: Aruna

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 1 of 3

Trigger is an event driven PL/SQL block. Event may be any DML transaction. Cursor is a stored select statement for that current session. It will not be stored in the database, it is a logical component. Function is a set of PL/SQL statements or a PL/SQL block, which performs an operation and must return a value.

Posted by: Girish P

Contact Girish P

Cursor cursor is a private sql work area. Every sql statement executed by oracle server has an individual cursor associated with it. Cursor are two types 1.Implicit cursor 2.Explicit cursor Implicit cursor: Implicit cursors are declared by pl/sql implicitly at the time of DML statement and select statement in pl/sql including queries that returns single row. cursor have four attributes 1. SQL%ROWCOUNT 2. SQL%ISOPEN 3. SQL%NOTFOUND 4. SQL%FOUND SQL%ROWCOUNT-Basically it returns number. means number of rows affected by present sql statement. SQL%ISOPEN-Always evalutes false because implicit cursor automatically closed after execution of sql statement. SQL%FOUND-Always evalutes true because one or more rows are affected by recent sql statement SQL%NOTFOUND-Always evalutes true when no rows are affected by present sql statement. example of explicit cursor DECLARE v_empno employees.employee_id%TYPE; v_name employees.last_name%TYPE; CURSOR emp_cur IS SELECT employee_id,last_name FROM employees BEGIN OPEN emp_cur; LOOP

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 2 of 3

FETCH emp_cur INTO v_empno,v_name ; EXIT WHEN emp_cur%ROWCOUNT>10 OR emp_cur%NOTFOUND; DBMS_OUTPUT.PUT_LINE('employee_id:'||TO_CHAR(v_empno) 'employee_name:'||'v_name'); END LOOP; CLOSE emp_cur; END; Trigger:-Trigger is pl/sql block or procedure that is associated with table,view,schema and database. Execute immidiately when particular event take place. there are two types of trigger 1.Application trigger:fires automatically when event occurs with particular application. 2.Database trigger:Fires when data such as DML oparation occured at that time. DML triggers are two types 1.Statementlevel trigger 2.Rowlevel trigger statement level trigger-statement level trigger means trigger body execute once for the triggering event.this is default.A statement level trigger fire once even no rows are affected at all. Row level- Trigger body execute once for each row affected by triggering event.if no rows are affected in that case trigger body not executed. trigger example CREATE OR REPLACE TRIGGER secure_emp BEFORE INSERT ON employees BEGIN TO_CHAR(SYSDATE,'DY')IN('SUN','SAT') OR TO_CHAR((SYSDATE,'HH24:MI')NOT BETWEEN '08:00' AND '18:00')THEN RAISE_APPLICATION_ERROR(-20253,'u may insert employee information at business hrs'); END; ||

Posted by: Sudipta Das

Contact Sudipta Das

Cursor: cursor is temporary work area in pl/sql. Type of cusor: 1.Implict Cursor 2.Explicit Cursor 3.Cursor for loop 4.Cursor With Parametter

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is trigger,cursor,functions in pl-sql and we need sample programs about it?

Page 3 of 3

5.Cursor for Update 6.Ref Cursor Curosr Attributes: %foud %notfound %rowCount %isopen Trigger: Trigger is asscoiated with table or database event(when a table affect like dml operation that time occurs fire) Types of Trigger: we can write 12 types trigger

Posted by: raja

Contact raja

Trigger is a stored procedure that invokes automatically when the event occurs. We can write 12 types of triggers on table and 15 types of triggers on views. Cursor pointer or handler to the context area . 2 types of cursors are there implicit and explicit cursors.

Posted by: Aruna

http://www.coolinterview.com/interview/15106/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What are two virtual tables available during database trigger execution ?

Page 1 of 1

The table columns are referred as OLD.column_name and NEW.column_name. For triggers related to INSERT only NEW.column_name values only available. For triggers related to UPDATE only OLD.column_name NEW.column_name values only available. For triggers related to DELETE only OLD.column_name values only available.

Two tables are: OLD and NEW. Insert Trigger : OLD - no value. NEW - inserted value. UPDATE TRIGGER OLD- old value. NEW- new updated value. DELETE TRIGGER OLD - old value. NEW - no value.

Posted by: Sachin Nagpal

http://www.coolinterview.com/interview/3689/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is ref cursor?

Page 1 of 2

In PL/SQL ,pointer has a datatype REF X where REF-Reference X-class of objects Cursor Variables has a datatype REF-CURSOR where Cursor Varibales are like pointers which hold the memory location of some item instead of the item itself.

Posted by: shikha srivastava

Contact shikha srivastava

Ref Cursor is cursor variable. It is a pointer to a result set. It is useful in scenarios when result set is created in one program and the processing of the same in some other, might be written in different language, e.g. firing the select is done in PL/SQL and the processing will be done in Java.

Posted by: Sukhamoy

Contact Sukhamoy

Its a run time query binding with the cursor variable. Normal cursors are static cursors becaz they get acquited of query at the compile time.

Posted by: Rup

Contact Rup

Ref cursors are used when we want to use the cursor which is not bound to a specific query. Cursor variables can be tied to many such type of compatible queries.

Posted by: Jyothsna

Contact Jyothsna

Ref cursor is user defined type.Ref cursor allows any type of query for the cursor.Ref cursor is a global declaration of the cursor.Ref cursor is re-usable. Ref cursor reduces the number of declarations of the cursor in the entire plsql program.Ref cursor is either static or dynamic.

http://www.coolinterview.com/interview/11818/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is ref cursor?

Page 2 of 2

Posted by: jaali

Contact jaali

Ref cursor is a cursor variable is used to pass the result sets between sub programs. Ref cursors are 2 types 1.weak Ref cursor and 2. Strong Ref cursor If a ref cursor should not return any value then it is called as weak ref cursor and if a cursor returns the value then it is strong ref cursor. weak ref cursors are more flexible because you can associate a weakly typed cursor varible to any query

Posted by: Aruna

Contact Aruna

Ref cursor is a dynamic cursor with which we can change our query contents at run time. once it is defined thereafter we can declare a cursor variable of that(ref cursor) data type and write any number of type compatible query against this cursor variable.

Posted by: Ravindra Kumar Ray

Contact Ravindra Kumar Ray

http://www.coolinterview.com/interview/11818/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What is difference between % ROWTYPE and TYPE RECORD ?

Page 1 of 1

% ROWTYPE is to be used whenever query returns a entire row of a table or view. TYPE rec RECORD is to be used whenever query returns columns of different table or views and variables. E.g. TYPE r_emp is RECORD (eno emp.empno% type,ename emp ename %type ); e_rec emp% ROWTYPE cursor c1 is select empno,deptno from emp; e_rec c1 %ROWTYPE.

http://www.coolinterview.com/interview/3701/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

How to debug the procedure ?

Page 1 of 2

You can use DBMS_OUTPUT oracle supplied package or DBMS_DEBUG pasckage.

Posted by: Nasir Ali

Contact Nasir Ali

put a dbms output_putline statment which will dislapay that your procedure is executing successfully up to which stage.

Posted by: sanjay

Contact sanjay

By using the sql developer tools and in that use breakpoints (the location from which u want to debug and execute line by line at the same time you will notice the current value of the executed statement below in another windows) and don't forgot to enable the dbms_output ). In this way you can do the same things

Posted by: RAJESH KUMAR

Contact RAJESH KUMAR

We can also do line by line execution using tools such as Pl/ SQL Developer, Toad etc

Posted by: Amit Gupta

Contact Amit Gupta

if you want to tune a package then you can use dbms_profiler. if you want to debug use dbms_debug procedure. Thanks, Sudipta

Posted by: Sudipta

Contact Sudipta

http://www.coolinterview.com/interview/15804/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

How to debug the procedure ?

Page 2 of 2

ALTER PROCEDURE procedurename DEBUG; OR ALTER PROCEDURE procedurename PLSQL_DEBUG=TRUE

Posted by: priya

http://www.coolinterview.com/interview/15804/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What will the Output for this Coding? Declare Cursor c1 is select * from emp FORUPDA... Page 1 of 3

By declaring this cursor we can update the table emp through z,means wo not need to write table name for updation,it may be only by "z".

selecting in FOR UPDATE mode locks the result set of rows in update mode, which means that row cannot be updated or deleted until a commit or rollback is issued which will release the row(s).

Posted by: Ash

Contact Ash

You will get an error. Since, the commit/rollback closes the cursor and release the lock if you use for update of, so trying to fetch out of a closed cursor will raise an error.

Posted by: Nattu

Contact Nattu

The procedure executes successfully.COMMIT will not close the cursor. declare b boolean; cursor c is select * from emp for update; rec c%rowtype; begin open c; fetch c into rec; dbms_output.put_line('NAME is'||rec.ename); commit; if c%isopen then dbms_output.put_line('Cursor is not closed '||c%rowcount); end if; fetch c into rec; dbms_output.put_line('NAME is'||rec.ename); end; O/P **** NAME isSMITH Cursor is not closed 1 NAME isALLEN

http://www.coolinterview.com/interview/10945/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What will the Output for this Coding? Declare Cursor c1 is select * from emp FORUPDA... Page 2 of 3

Posted by: Srikri

Contact Srikri

This coding get sucessful but after completed this query we con't delete or update to the EMP table because of we used FOR UPDATE clause, they explictly locked the entire row still not relese.

Posted by: siva kumar

Contact siva kumar

The cursor will execute for first record and then for second record it will raise exception. Because the ROWS which were locked by FOR UPDATE clause, now been released by commit.

Posted by: RISHI RAWAT

Contact RISHI RAWAT

The following error will occur as there is a space between IN TO. ERROR at line 8: ORA-06550: line 8, column 11: PLS-00103: Encountered the symbol "IN" when expecting one of the following: . into bulk

Posted by: Chella

Contact Chella

It will ORA:01002 fetch out of sequence error If the cursor has been opened with the FOR UPDATE clause, fetching after a COMMIT will return the error.

Posted by: kalam

Contact kalam

http://www.coolinterview.com/interview/10945/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

What will the Output for this Coding? Declare Cursor c1 is select * from emp FORUPDA... Page 3 of 3

If the table which we are using in the cursor is empty then procedure will run successfully. But if table contains rows in it, it will give error as " fetch out of sequence"

Posted by: Trupti Salvi

http://www.coolinterview.com/interview/10945/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Explain how procedures and functions are called in a PL/SQL block ?

Page 1 of 1

Function is called as part of an expression. sal := calculate_sal ('a822'); procedure is called as a PL/ SQL statement calculate_bonus ('A822');

Function can be called from SQL query + explicitly as well e.g 1)select empno,salary,fn_comm(salary)from employee; 2)commision=fn_comm(salary); Procedure can be called from begin-end clause. e.g. Begin ( proc_comm(salary); ) end

Posted by: Hirenkumar

Contact Hirenkumar

Function: Example Select get_name(Emp_id) from dual. Procedure: Example Begin Process_employee(EMP_ID); end;

Posted by: nikhil

http://www.coolinterview.com/interview/3674/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

Where the Pre_defined_exceptions are stored ?

Page 1 of 1

In the standard package. Procedures, Functions & Packages ;

It stores in standard_package.

Posted by: Amit Singh

Contact Amit Singh

http://www.coolinterview.com/interview/3682/ PDF created with pdfFactory Pro trial version www.pdffactory.com

3/6/2010

You might also like