GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  Oracle  >  SQL
Go To First  |  Previous Question  |  Next Question 
 SQL  |  Question 73 of 171    Print  
difference between case expresssion and case statement

  
Total Answers and Comments: 2 Last Update: February 06, 2008     Asked by: samba 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
February 09, 2006 06:00:36   #1  
oraappsexp Member Since: December 2005   Contribution: 2    

RE: difference between case expresssion and case state...

CASE Expressions And Statements

The CASE expression was first added to SQL in Oracle8i. Oracle9i extends its support to PL/SQL to allow CASE to be used as an expression or statement:

Value Match CASE Expression

The CASE expression is a more flexible version of the DECODE function. In its simplest form it is used to return a value when a match is found:
SELECT ename empno (CASE deptno  WHEN 10 THEN 'Accounting'  WHEN 20 THEN 'Research'  WHEN 30 THEN 'Sales'  WHEN 40 THEN 'Operations'  ELSE 'Unknown' END) departmentFROM empORDER BY ename;
The value match CASE expression is also supported in PL/SQL:
SET SERVEROUTPUT ONDECLARE deptno  NUMBER : 20; dept_desc VARCHAR2(20);BEGIN dept_desc : CASE deptno     WHEN 10 THEN 'Accounting'     WHEN 20 THEN 'Research'     WHEN 30 THEN 'Sales'     WHEN 40 THEN 'Operations'     ELSE 'Unknown'    END; DBMS_OUTPUT.PUT_LINE(dept_desc);END;/

Searched CASE Expression

A more complex version is the searched CASE expression where a comparison expression is used to find a match. In this form the comparison is not limited to a single column:
SELECT ename empno (CASE  WHEN sal < 1000 THEN 'Low'  WHEN sal BETWEEN 1000 AND 3000 THEN 'Medium'  WHEN sal > 3000 THEN 'High'  ELSE 'N/A' END) salaryFROM empORDER BY ename;
The searched CASE expression is also supported in PL/SQL:
SET SERVEROUTPUT ONDECLARE sal  NUMBER : 2000; sal_desc VARCHAR2(20);BEGIN sal_desc : CASE     WHEN sal < 1000 THEN 'Low'     WHEN sal BETWEEN 1000 AND 3000 THEN 'Medium'     WHEN sal > 3000 THEN 'High'     ELSE 'N/A'    END; DBMS_OUTPUT.PUT_LINE(sal_desc);END;/

Value Match CASE Statement

The CASE statement supported by PL/SQL is very similar to the CASE expression. The main difference is that the statement is finished with an END CASE statement rather than just END. The PL/SQL statements are essentially an alternative to lists of IF .. THEN .. ELSIF statements:
SET SERVEROUTPUT ONBEGIN FOR cur_rec IN (SELECT ename empno deptno FROM emp ORDER BY ename) LOOP DBMS_OUTPUT.PUT(cur_rec.ename || ' : ' || cur_rec.empno || ' : '); CASE cur_rec.deptno  WHEN 10 THEN   DBMS_OUTPUT.PUT_LINE('Accounting');  WHEN 20 THEN   DBMS_OUTPUT.PUT_LINE('Research');  WHEN 30 THEN   DBMS_OUTPUT.PUT_LINE('Sales');  WHEN 40 THEN   DBMS_OUTPUT.PUT_LINE('Operations');  ELSE   DBMS_OUTPUT.PUT_LINE('Unknown'); END CASE; END LOOP;END;/

Searched CASE Statement

As with its expression counterpart the searched CASE statement allows multiple comparisons using mulitple variables:
SET SERVEROUTPUT ONBEGIN FOR cur_rec IN (SELECT ename empno sal FROM emp ORDER BY ename) LOOP DBMS_OUTPUT.PUT(cur_rec.ename || ' : ' || cur_rec.empno || ' : '); CASE  WHEN cur_rec.sal < 1000 THEN   DBMS_OUTPUT.PUT_LINE('Low');  WHEN cur_rec.sal BETWEEN 1000 AND 3000 THEN   DBMS_OUTPUT.PUT_LINE('Medium');  WHEN cur_rec.sal > 3000 THEN   DBMS_OUTPUT.PUT_LINE('High');  ELSE   DBMS_OUTPUT.PUT_LINE('Unknown'); END CASE; END LOOP;END;/

 
Is this answer useful? Yes | No
February 06, 2008 23:06:22   #2  
g_sidhu Member Since: August 2007   Contribution: 122    

RE: difference between case expresssion and case statement
The CASE statement supported by PL/SQL is very similar to the CASE expression. The main difference is that the statement is finished with an END CASE statement rather than just END. The PL/SQL statements are essentially an alternative to lists of IF .. THEN .. ELSIF statements:
 
Is this answer useful? Yes | No

 Related Questions

Rename is a permanent name given to a table or column whereas Alias is a temporary name given to a table or column which do not exist once the SQL statement is executed. 
Latest Answer : Rename means give the new name to existed objects alise is use to refrence to objects ...

A table can have only one PRIMARY KEY whereas there can be any number of UNIQUE keys. The columns that compose PK are automatically define NOT NULL, whereas a column that compose a UNIQUE is not automatically 
Latest Answer : Primay Key                     Unique key-----------                  ...

SQL*PLUS is a command line tool where as SQL and PL/SQL language interface and reporting tool. Its a command line tool that allows user to type SQL commands to be executed directly against an Oracle database. 
Latest Answer : SQL keywords cannot be abbrevated but SQL*PLUS keywords can be  abbrevated. ...

SUBSTR returns a specified portion of a string eg SUBSTR('BCDEF',4) output BCDEINSTR provides character position in which a pattern is found in a string. eg INSTR('ABC-DC-F','-',2) 
Latest Answer : hi Buddy,   Substr only give the sub part of the stringSubstr(String,'start postion','length')Substr('ABCDEFAG',3,7); /*out put is =cdefag */Instr to give only possion  of letter in use in a stringInstr(String,'letter',possition  ...

PL/SQL declares a cursor implicitly for all SQL data manipulation statements, including quries that return only one row. However,queries that return more than one row you must declare an explicit cursor 
Latest Answer : explicit ...

NO DATA FOUND is an exception raised only for the SELECT....INTO statements when the where clause of the querydoes not match any rows. When the where clause of the explicit cursor does not match any rows 
Latest Answer : NO DATA FOUND: Is an exception which is raised when no rows are retrieved from the database in a SELECT statement, then PL/SQL raises the exception NO_DATA_FOUND.%NOTFOUND: is a Boolean attribute that evaluates to TRUE if the most recent SQL statement ...

What a SELECT FOR UPDATE cursor represent.[ANSWER]SELECT......FROM......FOR......UPDATE[OF column-reference][NOWAIT] The processing done in a fetch loop modifies the rows that have been retrieved by the cursor. A convenient way of modifying the rows is done by a method with two parts: the FOR UPDATE clause in the cursor declaration, WHERE CURRENT OF CLAUSE in an UPDATE or declaration statement.

OPEN cursor variable FOR SELECT...StatementCLOSE cursor variable In order to associate a cursor variable with a particular SELECT statement OPEN syntax is used. In order to free the resources used for 
Latest Answer : • The OPEN-FOR statement associates a cursor variable with a multirow query, executes the query, identifies the result set, and positions the cursor to point to the first row of the result set.• The FETCH statement returns a row from the result ...

Functions are named PL/SQL blocks that return a value and can be called with arguments procedure a named block that can be called with parameter. A procedure all is a PL/SQL statement by itself, while 
Latest Answer : Procedure:Parameters IN, OUT and IN OUT and can return n number of values via sys cursor.Function:Only IN parameter and must return a value by using RETURN.Can be used in where clause of the Query but performance issue will raise. ...

The variables declared in the procedure and which are passed, as arguments are called actual, the parameters in the procedure declaration. Actual parameters contain the values that are passed to a procedure 
Latest Answer : Formal Parameter: A variable declared in the parameter list of a subprogram specificationExample: create or replace procedure/function x (p_id number, p_sal number)Actual Parameter: A variable or expression refrenced in the parameter list of a subprogram ...


 Sponsored Links

 
Related Articles

Querying Data with Oracle XQuery

Querying Data with Oracle XQuery Starting with Oracle Database 10g Release 2 you can take advantage of a full featured native XQuery engine integrated with the database With Oracle XQuery you can accomplish various tasks involved in developing PHP Oracle XML applications operating on any kind of dat
 

Using Oracle XML DB Repository

Using Oracle XML DB Repository Another variation on accessing and manipulating XML content stored in Oracle database is provided by Oracle XML DB repository which is an essential component of Oracle XML DB mosgoogle NOTE Oracle XML DB repository also known as XML repository is a hierarchically organ
 

Using Oracle Database for Storing, Modifying, and Retrieving XML Data

Using Oracle Database for Storing Modifying and Retrieving XML Data With Oracle XML DB you have various XML storage and XML processing options allowing you to achieve the required level of performance and scalability One of the most interesting things about Oracle XML DB is that it allows you to per
 

XML Processing in PHP and Oracle Applications

Processing XML in PHP Oracle Applications As mentioned there are two alternatives when it comes to performing XML processing in your PHP Oracle application You can perform any required XML processing using either PHP s XML extensions or PEAR XML packages or Oracle s XML features mosgoogle In the fol
 

PHP Oracle Web Development

PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax The book is written by Yuli Vaseliev a well known author of different web development and programming books PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax is a good starting b
 

Getting Started with Oracle and ODP.NET

ODP NET Developer&rsquo; s Guide by Jagadish Chatarji Pulakhandam Sunitha Paruchuri A practical guide for developers working with the Oracle Data Provider for NET and the Oracle Developer Tools for Visual Studio 2005 Application development with ODP NET Dealing with XML DB using ODP NET Oracle
 

PHP Oracle Web Development Review

PHP Oracle Web Development Data processing Security Caching XML Web Services and Ajax The book is written by Yuli Vaseliev a well known author of different web development and programming books The author is also an expert in open source technologies and SOA Service Oriented Architecture But besides
 

Step by Step Oracle PL-SQL Tutorial

This introductory tutorial to PL SQL will help you to understand the basic concepts of PL SQL Please review the following tutorials and practice the sample SQL Statements on your local Oracle Database Please note that you must learn these basic things before we actually start getting in to Advanced
 

Working with XML in Oracle

Working with XML in Oracle Introduction to XML Extensive markup language is the language which presents data in a human readable form of text The data can be anything from a purchase order or a stock quote or weather radar or a flight schedule it can be represented using XML XML is very similar to H
 

What is Oracle Net Service

Oracle Net Services provides enterprise-wide connectivity solutions in distributed, heterogeneous computing environments. Oracle Net Services eases the complexities of network configuration and management, maximizes performance, and improves network diagnostic capabilities. Oracle Net, a component
 

About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape