SQL Foundation and branching loops for Oracle databases

Source: Internet
Author: User

first, the SQL base language
DECLARE -- Statement varchar2 (ten-- Variable or object BEGIN  A:=' Xiao Ming ') ; --   : =   denotes assigning a value to a variable -  - output with Dbms_output.put_line ()  END;
Second, branch
DECLARE  --StatementA Number(Ten); B Number(Ten);BEGINA:= 2; B:= 3; IFA<B ThenDbms_output. Put_Line ('A is less than B'); elsif A>B Then --pay attention to the wording of elsifDbms_output. Put_Line ('a greater than B'); ELSEDbms_output. Put_Line ('a equals b'); END IF; END;
Third, CirculationFirst Type: (Loop loop)
DECLAREM Number(5); BEGINM:= 5; LOOPEXIT  whenM< 0; Dbms_output.    Put_Line (M); M:=M- 1; ENDLOOP; END;

Execution Result:
The second type: (While loop)
DECLAREM Number(5);BEGINM:= 5;  whileM> 0LOOP--While LoopDbms_output.    Put_Line (M); M:=M- 1; ENDLOOP;END;

Execution Result:

Third type: (For loop)
DECLARE BEGIN   for inch 1 5 LOOP     -- A For loop can only traverse regular numbers such as the     Dbms_output. Put_Line (N);   END LOOP; END;

Execution Result:
Iv. Handling Exceptions
DECLAREM Number(Ten); SNAMEVARCHAR2(Ten);BEGINM:= 5; SELECTS.name intoSNAME fromZ_student SWHERES.id=M; Dbms_output. Put_Line ('Query Result:' ||SNAME);END;

Execution Result:

Then throw an exception exception

DECLAREM Number(Ten); SNAMEVARCHAR2(Ten);BEGINM:=  -;--change a value that does not exist  SELECTS.name intoSNAME fromZ_student SWHERES.id=M; Dbms_output. Put_Line ('Query Result:' ||SNAME); EXCEPTION--Throw Exception   whenNo_data_found ThenDbms_output. Put_Line ('no query to data'); END;

Execution Result:

Let me give you an example:

DECLARE  --StatementA Number(Ten); B Number(Ten); Nozero EXCEPTION; --define Nozero as Exception typeBEGINA:= 2; B:= 0; IFA= 0 ORB= 0  ThenRAISE Nozero;--RAISE throws an exception  END IF; IFA<B ThenDbms_output. Put_Line ('A is less than B'); elsif A>B Then    --pay attention to the wording of elsifDbms_output. Put_Line ('a greater than B'); ELSEDbms_output. Put_Line ('a equals b'); END IF; EXCEPTION whenNozero ThenDbms_output. Put_Line ('neither a nor B can be 0! ');  whenOTHERS ThenDbms_output. Put_Line ('Other exceptions! '); END;

The result of the execution is:

SQL Foundation and branching loops for Oracle databases

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.