Oracle Plsql Exercises

Embed Size (px)

DESCRIPTION

perfect exercises

Citation preview

  • 5/22/2018 Oracle Plsql Exercises

    1/15

    Guess Paper 2010Class XII

    Subject Informatics Practices

    PL/SQL

    PL/SQL is a Procedural Language. It is a procedural extension of Oracle-SQL that offers language constru

    similar to those in imperative programming languages. PL/SQL allows its users to develop complex databapplications that reuire the usage of control structures and procedural elements such as procedures! functi

    modules etc

    .

    Limitations of SQL:

    ". It doesn#t offers procedural capabilities.

    $. %o error handling procedures are supported b& SQL.

    '. Processing in SQL is (ime-)onsuming.*. %etwor+ traffic is high in SQL.

    ,. SQL commands can#t be shared b& multiple applications.

    Advantages of PL/SQL over SQL:

    ". It has procedural capabilities.

    $. It supports error-handling procedures.'. Processing in PL/SQL is less time consuming.

    *. se of PL/SQL reduces the networ+ traffic.

    ,. PL/SQL promotes sharing of programs b& multiple applications.

    Block: PL/SQL bloc+ is a set of related procedural SQL statements! enclosed within the reserved words 0and %1. PL/SQL supports two t&pes of bloc+s.

    1. Anonymous Block:these are the bloc+ without header. (he& are not stored in the database.

    2. Named Block: these are the bloc+ having headers or labels. (he& are stored in the database. %amed bloc

    can be procedures! functions or triggers.

    eserved !ords:are the predefined words that are assigned some specific and special meaning. (hese wo

    can#t be used as identifiers 2variable! function name! procedure name etc.3 in PL/SQL. .g 1eclare! egin! 4

    Loop! nd! nd Loop! nd If! (hen tc.

    "omment:comments are the non-executable statements in PL/SQL. (hese are used to increase the readabilof the program. (here are two t&pes of comments supported b& PL/SQL.

    1. Single line comment: these comments start with the sign 5- -5

    2. #ulti$le Line "omment:these comments start with /6 and end with 6/.

    %dentifier: refers to the name of the ob7ect in PL/SQL. .g 8ariable! )ursors! Procedures! 4unctions etc.

    &elimiters9 are the s&mbols that have special meaning in PL/SQL. .g assignment operator2 9: 3! concatenat

    operator2 ;; 3! range2 .. 3!

    &ataty$es specif& the storage format and valid range of value for a variable. PL/SQL provides follow

    datat&pes9". Scalar ty$e:it is atomic means it is not made up of other datat&pes. e.g )=>?! 8>?)=>?$! %@

    1>(! OOL>%! LO%0.

    $. "om$osite ty$e: it is made up of other datat&pes. it has some internal components that can be manipulaindividuall&. .g. (>L! ?)O?1.

    '. eference ty$e:it holds values! called pointers that designate other program items.

  • 5/22/2018 Oracle Plsql Exercises

    2/15

    'aria(le: is a named storage location in memor& that is used to store the values that can be changed during texecution of the program. PL/SQL supports three t&pes of variables.

    ". Local 'aria(le:these t&pes of variables are declared in the 1)L>? bloc+ of PL/SQL code.

    $. Su(stitution 'aria(le: these t&pe of variables get automaticall& declared b& prefixing 5A# sign to

    variable. (hese variables are used to input the data at the run time.'. Bind 'aria(le:these t&pes of variables can be declared in SQL prompt and can be used with PL/SQL co

    b& prefix the 59# sign.

    Anc)ored &eclaration:when a variable is declared with reference to another variable#s datat&pe +nown

    anchored declaration. PL/SQL uses B(CP A B?OD(CP declaration attributes for this. .g.%um" number2,3E

    %um$ %um"Bt&peE /6declare variable %um$ having same datat&pe as that of %um". 6/

    sal emp.salBt&peE /6 declare variable sal with the datat&pe similar to sal column of @P table6/

    Literal:is a value! which is not represented b& an identifier. It is simpl& a value. .g "F! $G" etc.

    "onstant9 is that variable whose value is assign at the time of declaration and doesn#t change during the time

    execution. .g. pi constant number 9: '."*

    Printing in PL/SQL Block: to print the output with in the same line stateme

    1@SHO(P(.P(2stringJ3.

    (o print the output in different line statement9 1@SHO(P(.P(HLI%2S(?I%0J3

    S*+ S*'*,-+P-+ ,N is an environment variable with two possible values O% or O44. (he out

    generated b& the 1@SHO(P( pac+age is visible onl& when S?8?O(P( variable is O%.

    PL/SQL BL," &%A#:

    &*"LA*

    /6this bloc+ is used for variable declaration! cursor/record definition 6/

    B*%N

    /6this bloc+ is used to define the SQL-PL/SQL statements6/

    *0"*P+%,N

    /6 all the exception handling statements written in this bloc+ 6/

    *N&

    ",N+,L ",NS+-"+S %N PL/SQL:

    1. Seuence:means the statements are being executed seuentiall&. (his is the default follow of statements.

    3rite a PL/SQL $rogram to in$ut t!o no4s and dis$lay t)e total and average of t)ese no.

    1)L>?

    %O" %@?E --LO)>L 8>?I>L%O$ %@?E

    (O( %@?EP? %@?E

    0I%

    %O" 9: A%"E --A%" substitution variable%O$ 9: A%$E --A%$ substitution variable

    (O( 9: %O" < %O$E

    P? 9: (O( /$E1@SHO(P(.P(HLI%25(O(>L 5 ;; (O(3E

    1@SHO(P(.P(HLI%25>8?>0 5 ;; P?3E

  • 5/22/2018 Oracle Plsql Exercises

    3/15

    %1E

    3rite a PL/SQL $rogram to calculate t)e sim$le interest.

    1)L>?

    P %@?E? %@?E

    ( %@?ESI %@?E

    0I%

    P 9: AP?I%)IP>LE

    ? 9: A?>(E( 9: A(I@E

    SI 9: 2P 6 ? 6 (3/"GGE

    1@SHO(P(.P(HLI%25I%(?S( 5 ;; SI3E%1E

    2. Selection:means the execution of statements depending upon a condition test. If condition evaluates to trset of statements is followed otherwise different set of statements is followed. (his is also +nown as decisconstruct.

    %f..end if:this is the simplest form of the I4 statement. If the condition between I4 and (=% us true then

    set of statements between the (=% and %1 I4 gets executed otherwise not executed.

    %f 5condition6 t)en

    .

    .

    end if

    3rite a $rogram to in$ut t)e salary and !orking e7$ of em$ and calculate t)e (onus as 189 of sala

    ive 88 S. *7tra (onus to t)ose !)ose !orking e7$. #ore t)an 18 years.

    1)L>?S>L %@?E

    O%S %@?E

    C>?S %@?E0I%

    S>L 9: AS>L>?CE

    C>?S 9: ADO?KI%0HC>?SE

    O%S 9: S>L 6 ."EI4 C>?S J "G (=%

    O%S 9: O%S < ,GGE

    %1 I4E

    1@SHO(P(.P(HLI%25O%S9 5 ;; O%S3E%1E

    %f..else..end if:in this construct If the condition between I4 and (=% us true then the set of stateme

    between the (=% and LS gets executed otherwise the set of statements between LS and %1 I4 g

    executed.

    %f 5condition6 t)en

    .

    else

    .

    end if

  • 5/22/2018 Oracle Plsql Exercises

    4/15

    3rite a PL/SQL $rog to in$ut t!o no4s and $rint t)e ma7imum no (et!een t)em.

    1)L>?

    %" %@?E

    %$ %@?E0I%

    %" 9: A%O"E%$ 9: A%O$E

    I4 %" J %$ (=%

    1@SHO(P(.P(HLI%25%O" IS 0?>(?#3E

    LS1@SHO(P(.P(HLI%25%O$ IS 0?>(?#3E

    %1 I4E

    %1E

    #ulti$le %;:this is last and most complex form of I4 statement. It executes the code following a conditiothat evaluates to (?. If no condition evaluates to true the code following LS gets executed.

    %f 5condition6 t)en

    .

    els%f 5condition6 t)en

    .

    else

    .

    end if

    3rite a PL/SQL $rogram to in$ut t)e Basic Salary and calculate t)e 9: >SI) 6 ."$E1> 9: >SI) 6 .GFE

    LSI4 >SI) J "$GGG (=%

    =?> 9: >SI) 6 ."E1> 9: >SI) 6 .GE

    LSI4 >SI) J MGGG (=%

    =?> 9: >SI) 6 ..GNE

    1> 9: >SI) 6 .G*ELS

  • 5/22/2018 Oracle Plsql Exercises

    5/15

    =?> 9: >SI) 6 .G,E1> 9: >SI) 6 $GGE

    %1 I4E

    %( 9: >SI) < =?> < 1>E

    1@SHO(P(.P(HLI%25>SI)9 5 ;; >SI)3E1@SHO(P(.P(HLI%25=?>9 5 ;; =?>3E

    1@SHO(P(.P(HLI%251>9 5 ;; 1>3E1@SHO(P(.P(HLI%25%(9 5 ;; %(3E

    %1E

    Nested %fs:when I4 statement can contain another I4 inside it is called %S(1 Ifs.

    %; ",N&%+%,N +

  • 5/22/2018 Oracle Plsql Exercises

    6/15

    ctr 9: ctr < "Eexit when ctr J "GE

    end loopE

    endE

    Numeric ;or Loo$:this looping construct is used to repeat a set of instructions for a fixed no of times. S&nta

    ;or ctr in IreverseJ start .. end

    Loo$

    CCstatements to (e e7ecuted

    end loo$

    3rite t)e code to $rint 1 to 18.

    0I%

    4O? I I% ".."G LOOP1@SHO(P(.P(HLI%2I3E

    %1 LOOPE%1E

    3%+* +

  • 5/22/2018 Oracle Plsql Exercises

    7/15

    tot 9: tot < remEno 9: no remE

    no 9: no /"GE

    end loopE

    dbmsHoutput.putHline2tot3EendE

    N*S+*& L,,P:when a loop exists inside another loop is called nested loop.

    Loo$

    .

    Loo$

    .

    end loo$

    end loo$

    3%+* A PL/SQL P, +, P%N+ + 1? H2.

    Program to in$ut t!o no and c)eck !)et)er no1 is divisi(le (y no2 or not.

    Program to in$ut H no4s and find out t)e ma7imum no.

    Program to in$ut $rinci$al amount= time. %f time more t)an 18 yrs= calculate t)e sim$le interest !

    >9 interest ot)er!ise !it) ?9. Program to $rint first n odd num(ers in descending order.

    &A+ABAS* %N+*A"+%,N %N PL/SQL

    De can use an& or the following SQL statements for SQL ueries in PL/SQL code9

    S*L*"+9 for extracting data.

    %NS*+9 for add new data.

    -P&A+*9 for change in existing data.

    &*L*+*9 for remove existing data.

  • 5/22/2018 Oracle Plsql Exercises

    8/15

    S*L*"+ %N+, S+A+*#*N+9 in SQL select statement is used to displa& the results on the screen. utPL/SQL SL)( I%(O statement is used to fetch the single record from the database and store it into anoth

    variables for further processing. S&ntax9

    S*L*"+ 5L%S+6 %N+, 5'AA%BL*S6 ;,# 5+ABL*KNA#*6 I3?%O %@?E

    @PH%>@ @P.%>@B(CPE

    S>L>?C @P.S>LB(CPE

    0I%%O 9: A@P%OE

    SL)( %>@! S>L I%(O @PH%>@! S>L>?C 4?O@ @P D=? @P%O:%OE

    1@SHO(P(.P(HLI%2@PH%>@3E1@SHO(P(.P(HLI%2S>L>?C3E

    %1E

    3rite t)e "ode to in$ut t)e de$t4s no and $rint t)e total em$4s and sum of salary !it) in t)at de$t.

    1)L>?

    1%O %@?E(O(>L %@?

    S@HS>L %@?E

    0I%

    1%O 9: A1P(%OESL)( )O%(263! S@2S>L3 I%(O (O(>L! S@HS>L 4?O@ @P D=? 1P(%O : 1%OE

    1@SHO(P(.P(HLI%2(O(>L3E

    1@SHO(P(.P(HLI%2S@HS>L3E%1E

    3rite t)e PL/SQL code to in$ut t)e em$4s no and increase t)e salary (y 189 if )is salary 5 8

    ot)er!ise delete t)e record.

    1)L>?

    %O %@?E

    S>L>?C %@?E0I%

    %O 9: A@P%OE

    SL)( S>L>?C I%(O S>L 4?O@ @P D=? @P%O : %OE

    I4 S>L>?C ,GGG (=%P1>( @P S( S>L : S>L < S>L 6." D=? @P%O : %OE

    1@SHO(P(.P(HLI%25S>L>?C P1>(#3ELS

    1L( 4?O@ @P D=? @P%O : %OE

    1@SHO(P(.P(HLI%25?)O?1 1L(1#3E%1 I4E

    %1E

    3rite PL/SQL code to insert t)e record in de$t ta(le.

    1)L>?

  • 5/22/2018 Oracle Plsql Exercises

    9/15

    1%O %@?E1P(H%>@ 1P(.1%>@B(CPE

    1LO) 1P(.LO)B(CPE

    0I%

    1%O 9: A1P(%OE1P(H%>@ 9: 5A1%>@#E

    1LO) 9: 5ALO)>(IO%#EI%S?( I%(O 1P( 8>LS21%O! 1P(H%>@! 1LO)3E

    1@SHO(P(.P(HLI%25?)O?1 I%S?(#3E

    %1E

    ecords: PL/SQL ?ecord is a combination of fields. ?ecords are used to represent complete row in a tab

    ecord is a grou$ of multi$le $iece of information= related to one anot)er= called fields4.

    PL/SQL su$$orts t)ree ty$es of records:

    +a(le Based ecordrepresents each field in the table. 4ields in the record have the same name and datat&pe

    the columns in the table have. (able ased ?ecord is declared with an anchored declaration-usB?OD(CP.

    3rite t)e PL/SQL code to dis$lay t)e ename and sal of em$ !)ose em$no is in$ut (y user= using ta(

    (ased record.

    1)L>?

    ?S @PB?OD(CPE0I%

    SL)( 6 I%(O ?S 4?O@ @P D=? @P%O:A%OE

    1@SHO(P(.P(HLI%2?S.%>@ ;;?S.S>L3E%1E

    Programmer &efined ecord is declared and defined explicitl& b& the programmer as per reuirement. 4irsa ?)O?1 t&pe is defined and then variable of that ?)O?1 t&pe is declared.

    3rite t)e PL/SQL code to dis$lay t)e ename and sal of em$ !)ose em$no is in$ut (y user= usi

    $rogrammerC(ased record.

    1)L>?

    (CP ?SH@P IS ?)O?12

    @PH%>@ @P.%>@B(CP!

    @PHS>L @P.S>LB(CP3E

    ?S ?SH@PE

    0I%SL)( %>@! S>L I%(O ?S 4?O@ @P D=? @P%O:A%OE1@SHO(P(.P(HLI%2?S.@PH%>@3E

    1@SHO(P(.P(HLI%2?S.@PHS>L3E

    %1E

    "ursor Based ecord is composite variable that represents each field in a cursor. (he fields of record h

    same name and datat&pe as the fields of the cursor on which the record is based. It is declared usiB?OD(CP

  • 5/22/2018 Oracle Plsql Exercises

    10/15

    *7ce$tion:are some undesired situations when PL/SQL program terminated unexpectedl&.)P(IO% =>%1LI%0 is used to handle run-time errors. >ll the xception =andling code is written in

    )P(IO% section of a PL/SQL program.

    &*"LA*

    /6this bloc+ is used for variable declaration! cursor/record definition 6/B*%N

    /6this bloc+ is used to define the SQL-PL/SQL statements6/

    *0"*P+%,N

    3ttempt to insert duplicate value.

    I%8>LI1H)?SO? Illegal cursor operation occurred

    3rite PL/SQL scri$t to in$ut sal amt and dis$lay t)e *m$ Name earning same sal amt. -

    N,K&A+AK;,-N& and +,,K#ANM ,3S *7ce$tion.

    1)L>?

    S>L>?C %@?E

    H%>@ @P.%>@B(CPE

    0I%S>L>?C 9: AS>LE

    SL)( %>@ I%(O H%>@ 4?O@ @P D=? S>L:S>L>?CE

    1@SHO(P(.P(HLI%2H%>@3 E)P(IO%

    D=% %OH1>(>H4O%1 (=%

    1@SHO(P(.P(HLI%25I%8>LI1 8>L#3ED=% (OOH@>%CH?ODS (=%

    1@SHO(P(.P(HLI%25return multiple value#3E%1E

    2. -ndefined *7ce$tions:these are the less common errors that have not given predefined names. 4or therrors O?> error code i.e.! the s&stem compilation error messages are displa&ed.

    H. -serCdefined *7ce$tions:errors that do not cause a run-time error! but will violate business rules. Su

    errors are handled through programmer created custom error messages. ser-defined xception is raised w?>IS )P(IO%H%>@J.

    3rite PL/SQL scri$t t)at tra$s *,K&%'%&* e7ce$tion !)en a no is divided (y ot)er no. also rais

    user define e7ce$tion if no2 greater t)an no1.

  • 5/22/2018 Oracle Plsql Exercises

    11/15

    1)L>?%O" %@?E

    %O$ %@?E

    %$H0(H%" )P(IO%E --user-defined exception

    0I%%O" 9: A%"E

    %O$ 9: A%$EI4 %O$ J %O" (=%

    ?>IS %$H0(H%"E --user-defined exception raised.

    %1 I4E

    1@SHO(P(.P(HLI%2%O" / %O$3E)P(IO%

    D=% R?OH1I8I1 (=%

    1@SHO(P(.P(HLI%25not divide b& ero#3ED=% %$H0(H%" (=%

    1@SHO(P(.P(HLI%25no$ greater than no"#3E%1E

    *0""%S*:

    Program to in$ut t)e de$tno and $rint t)e de$t name along !it) de$tno (ased u$on de$t ta(le.

    3rite a PL/SQL (lock to $rint t)e )ig)est $aid and lo!est $aid em$loyee from em$ ta(le.

    3rite a PL/SQL (lock to delete all t)e ro!s of a $articular de$artment from t)e ta(le em$ !)e

    de$tno is acce$ted from t)e user.

    3rite PL/SQL code to calculate t)e commission (ased u$on t)e sale in$ut (y user.

    Sale Amt "omm.

    518888 88 O 189of sale

    18888 28888 1888 O 19 of sale

    6 28888 188 O 289 of sale

  • 5/22/2018 Oracle Plsql Exercises

    12/15

    +ransaction:us a logical unit of wor+ that is composed of one or more 1@L or 11L statements. > transactimust be either saved or rolled bac+.

    ",##%+9 when &ou manipulate the data all the 1@L operation affected in the temporar& area and datab

    remains unaffected from all these changes. If &ou want to store these changes to the database! &ou need

    )O@@I( the transaction.,LLBA": means undoing an& changes that have occurred in the current transaction.Oracle undoes so

    or all changes made b& &our session to the database in the current transaction.

    SA'*P,%N+9 is used to mar+ a specific point in current transaction. S>8POI%( gives a name to and mar

    point in the processing of current transaction. It allows &ou to ?OLL>)K (O that point.

    "-S,

    )?SO? provides &ou a wa& to select multiple records from the database and process each record individuainside PL/SQL program. (he set of rows returned b& multi-uer& is called result set of active set. (here are tw

    t&pes of cursors supported b& PL/SQL.

    1. %m$licit "ursor 2. *7$licit "ursor

    S+*PS *Q-%*& ;, *0PL%"%+ "-S,

    1. &eclare t)e cursor: in this process! se

    statement associated with cursor is defined. S&ntax

    "ursor 5curKname6 is select statement

    2. ,$en t)e cursor:in this process when curso

    opened the select statement associated with cursoexecuted. S&ntax9

    ,$en 5curKname6IF$arameterKlistGJ

    H. ;etc) ro!s from t)e cursor:in this step! one ris retrieved from the active set and stores it

    variables for further processing. S&ntax9;etc) 5curKname6 into 5varia(leKlist6

    . "lose t)e cursor:in this step the cursor is clos(he close statement disables the cursor. Cou

    reopen it. S&ntax9 close 5curKname6

    "-S, A++%B-+*S9 cursor has fattributes9

    ". 9N,+;,-N& Attri(ute:rows are fetched from the cursor#s active set one at a time. If the last fe

    returned a row B%O(4O%1 evaluates to 4>LS. If the last fetch failed to return a row then B%O(4O%evaluates to (?.

    $. 9;,-N& Attri(ute:is logical opposite of B%O(4O%1. B4O%1 evaluates to (? if the last fereturned a row or 4>LS if no row was returned.

    '. 9,3",-N+ Attri(ute: when cursor is opened! Browcount is ero. Dhen the records are fetched fractive set it is incremented b& one each time. It returned how man& records are fetched from the active set.

    *. 9%S,P*N Attri(ute:evaluates to (? if cursor is open! otherwise it evaluates to 4>L.

    3rite t)e PL/SQL scri$t to dis$lay t)e ename= o(= sal and de$tno from t)e em$ ta(le.

    1)L>?)?SO? )?H@P IS SL)( %>@! O! S>L! 1P(%O 4?O@ @PE

    ?S )?H@PB?OD(CPE --cursor-based record

    %m$licit "ursor *7$licit "ursor

    (hese cursors aremaintained internall& b&

    PL/SQL. (hese areopened and closed

    automaticall& when the

    uer& is executed.

    (hese cursors are definedwith a name and are

    opened and closedexplicitl& in a PL/SQL

    program.

    (he cursor attributes are

    prefixed with SQL. 2e.g.

    SQLB4O%13. ecauseSQL is default name of

    implicit cursor.

    (he cursor attributes are

    prefixed with the cursor

    name. 2e.g. curBfound3where cur is the name of

    explicit cursor.

    (he cursor attributeBISOP% is alwa&s false.

    ecause cursor getsclosed automaticall& after

    the statement is over.

    (he BISOP% attributeholds the value

    2(?/4>LS3 of thestatus of the cursor.

    Onl& one row can beprocessed using the

    SL)( I%(O statement.

    >n& number of rows canbe processed.

  • 5/22/2018 Oracle Plsql Exercises

    13/15

    0I%OP% )?H@PE

    LOOP

    4()= )?H@P I%(O ?SE

    I( D=% )?H@PB%O(4O%1E1@SHO(P(.P(HLI%2?S.%>@ ;; 5 5 ;; ?S.S>L ;; 5 5 ?S.O ;; 5 5;; ?S.1P(%O3E

    %1 LOOPE)LOS )?H@PE

    %1E

    3rite a PL/SQL scri$t to increase t)e salary as $er follo!ing criteria:

    SALAM A#+ %N"*#*N+*& BM

    51288 >9

    5288 129

    588 19

    ,+?)?SO? )?H@P IS SL)( @P%O! S>L 4?O@ @PE

    ?S )?H@PB?OD(CPE

    0I%OP% )?H@PE

    LOOP

    4()= )?H@P I%(O ?SE

    I4 ?S.S>L "$GG (=%P1>( @P S( S>L : S>L 6".GF D=? @P%O:?S.@P%OE

    LSI4 ?S.S>L $,GG (=%

    P1>( @P S( S>L : S>L 6"."$ D=? @P%O:?S.@P%OELSI4 ?S.S>L *,GG (=%

    P1>( @P S( S>L : S>L 6".", D=? @P%O:?S.@P%OE

    LSP1>( @P S( S>L : S>L 6".$ D=? @P%O:?S.@P%OE

    %1 I4E

    )LOS )?H@PE

    %1E

    ParameteriRed "ursors: a cursor can receive values provided b& &ou! such cursors are +nown

    Parameteried cursors.

    "ursor curKname F$arameter dataty$e=..G is select statementDe can provide value for these parameters at the time of opening cursor as per following s&ntax9

    ,$en curKnameFvalue= G

    3rite t)e PL/SQL scri$t to dis$lay t)e ename= o(= sal of $articular de$t t)at is in$ut (y user usi

    $arameter.

    1)L>?

    )?SO? )?H@P21%O %@?3 IS SL)( %>@! O! S>L 4?O@ @P D=? 1P(%O

    1%OE?S )?H@PB?OD(CPE --cursor based record

    1P(H%O %@?E

  • 5/22/2018 Oracle Plsql Exercises

    14/15

    0I%1P(H%O 9: A1P(%OE

    OP% )?H@P21P(H%O3E

    LOOP

    4()= )?H@P I%(O ?SEI( D=% )?H@PB%O(4O%1E

    1@SHO(P(.P(HLI%2?S.%>@ ;; 5 5 ;; ?S.S>L ;; 5 5;; ?S.O3E%1 LOOPE

    )LOS )?H@PE

    %1E

    "-S, ;, L,,P:

    PL/SQL provides a special +ind of 4O? loop to process the rows returned in an explicit cursor. In a )ursor 4

    Loop! a declared cursor is Opened! 4etched and )losed automaticall& when all of the rows have been process

    S&ntax9

    ;, 5*",&K%N&*06 %N 5"-S,KNA#*6L,,P

    CCS+A+*#*N+S +, B* *0*"-+*&

    *N& L,,P

    3rite a PL/SQL scri$t to dis$lay t)e name= salary and (onus Fsalary T .12G for eac) em$ using cursor

    loo$.

    1)L>?)?SO? )?H@P IS SL)( %>@! S>L! S>L 6".$ 5O%S# 4?O@ @PE

    0I%

    4O? ?S I% )?H@P LOOP1@SHO(P(.P(HLI%2?S.%>@ ;; 5 5 ;; ?S.S>L ;;# 5 ;;?S.O%S3 E

    %1 LOOPE%1E

    "-S, ;, L,,P -S%N -SBCQ-*M:

    Driting a sub-uer& in place of cursor name in the cursor 4or Loop can do this. S&ntax9

    ;, 5*",&KNA#*6 %N 5S-BCQ-*M6 L,,P

    CCS+A+*#*N+S +, B* *0*"-+*&

    *N& L,,P

    3rite a PL/SQL scri$t to dis$lay t)e name= salary for eac) em$ using cursor for loo$.

    0I%4O? ?S I% 2SL)( %>@! S>L 4?O@ @P3

    LOOP1@SHO(P(.P(HLI%2?S.%>@ ;; 5 5 ;; ?S.S>L3 E

    %1 LOOPE%1E

    -NSL,'*& *0*"%S*:

    Drite a PL/SQL script that uses cursor 4or Loop to calculate bonus for emplo&ees as ,Bof salar& < $.,B

    comm. (he calculated bonus is store in a table bonuses2empno! bonus3. PL/SQL script to increment the salaries of emplo&ees as per following specifications9

    Salesmen If salar& < comm. J $GGG then "GB of salar& otherwise "$B of salar&

    >nal&sts $GB of salar&

  • 5/22/2018 Oracle Plsql Exercises

    15/15

    )ler+s "$B of salar&@anagers $,B of salar&