Simple PL/SQL syntax _ cursor

Source: Internet
Author: User

Case

Declare
I number: = 1;
J varchar2 (11 );
Begin
Case (I)
When 1 then J: = 'hahaha ';
When 2 then J: = 'xixi ';
Else J: = 'no find ';
End case;
Dbms_output.put_line (j );
End;

Cursor

Declare
Cursor cname is select * From sec_hrc_audit;
Var11 cname % rowtype;
Begin
Open cname;
Loop
Fetch cname into var11;
Dbms_output.put_line (var11.hrc _ Code );
Exit when cname % notfound;
End loop;
Dbms_output.put_line (rpad ('superman ', 20, 'ss '));
Close cname;

Loop1

Declare
Cursor cursor_name is select * From sec_hrc_audit;
Begin

For idx in cursor_name Loop
Dbms_output.put_line (idx. hrc_code );
End loop;

Loop2

Declare

Begin

For idx in (select * From sec_hrc_audit) loop
Dbms_output.put_line (idx. hrc_code | 'total rows ');
End loop;
End;

Cursor_count

Declare
Cursor cursor_name is select * From sec_hrc_audit;
Rowc number (22 );
Begin
For idx in cursor_name Loop
Dbms_output.put_line (idx. hrc_code | 'haha ');
Rowc: = cursor_name % rowcount;

End loop;
Dbms_output.put_line (rowc );
End;

Cursor_forupdate

Declare
Cursor cursor_name is select * From sec_hrc_audit for update of hrc_code;
Bighrc number (10 );
Begin

For idx in cursor_name Loop
Bighrc: = 3;
Update sec_hrc_audit set hrc_code = bighrc where current of cursor_name;
End loop;
Commit;

Cursor _ variable

Declare
Type RC is ref cursor return sec_hrc_audit % rowtype;
Var1 RC;
RT sec_hrc_audit % rowtype;
Type REC is record (hrc_code number (4), hrc_name varchar2 (2 ));
Type RC2 is ref cursor return REC;

Begin
Open var1 for select * From sec_hrc_audit;
Fetch var1 into RT;
Dbms_output.put_line (RT. hrc_code );
End;

Forloop

Declare
Line number: = 50;
Separator varchar2 (1): = ';
Actual varchar2 (150 );
I number: = 1;
Begin
For idx in 1 .. line Loop
Actual: = actual | separator;
I: = I + 1;
End loop;
Dbms_output.put_line (actual );
End;

Selectinto

Declare
Type REC is record (code number (2), num number (2 ));
Rec1 REC;
Begin
Select * into rec1 from ABC where hrc_code = 2;
NULL;
End;

Whileloop

Declare
Line number: = 50;
Separator varchar2 (1): = ';
Actual varchar2 (150 );
I number: = 1;
Begin
While (I <line) loop
Actual: = actual | separator;
I: = I + 1;
End loop;
Dbms_output.put_line (actual );
End;

Implicit cursor

Declare

Begin
-- Delete ABC where hrc_code = 1;
Insert into ABC (hrc_code) Select hrc_code from sec_hrc_audit where hrc_code = 1;
If (SQL % found) then
Dbms_output.put_line ('successful insert ');
End if;
Commit;
End;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.