当前位置:网站首页>Study notes 22/1/17
Study notes 22/1/17
2022-06-28 07:44:00 【Zeiyalo】
Learning notes
One 、 Program code block
1. Grammatical structure
-- Grammatical structure
DECLARE
-- Place of declaration ( Declare variables 、 Constant 、 Cursors, etc ), This one can be omitted
BEGIN
-- Logical block of code execution
EXCEPTION
-- Logic for handling exceptions
END;
-- Program end statement
BEGIN
DBMS_OUTPUT.put_line('HELLO WORLD');
END;
All languages are from hello world Start ;
notes :1. Remember to add a semicolon after each line of code ;
2.end Finally, be sure to add a semicolon as the end .
2. Variable
(1) Query through a given variable
DECLARE
V_EMPNO NUMBER;
V_ENAME VARCHAR2(30);
V_SAL NUMBER;
BEGIN
V_EMPNO :=7369; -- Note the assignment syntax
SELECT T.ENAME,T.SAL
INTO V_ENAME,V_SAL
/* SELECT T.ENAME The inserted data of variables should correspond to INTO V_ENAME */
FROM EMP T
WHERE T.EMPNO = V_EMPNO;
DBMS_OUTPUT.put_line(V_EMPNO||' '||V_ENAME||' '||V_SAL);
END;
What you need to pay attention to when defining variables :
Variable naming rule , It's usually V_ start
1. Statement / When defining variables , If the definition is varchar Type of , That must be given length ,
If number Data of type , You can give it or not ,
If it is date Data of type , You don't have to give the length ;
2、 After defining the length of the variable , This variable cannot be assigned more than this length ;
3、 What types of variables are defined , What type of value should be assigned when assigning values ;
4、 Defines a variable , A semicolon is required at the end to indicate the end .
SELECT … INTO … Something to watch out for :
1、 The result of our query Must be a row of data , It can't be 0 One or more lines ;
2、INTO This is followed by variables , The use is to SELECT The fields found after the query / The columns are put into variables in turn ;
3、INTO The order and number of the following variables must be the same as SELECT The order and number of fields in the subsequent query are consistent .
eg: Receive an employee's number (7788), Name of the employee ,
Position , The entry date is printed (DBMS_OUTPUT.put_line)
DECLARE
V_EMPNO NUMBER;
V_ENAME VARCHAR2(10);
V_JOB VARCHAR2(9);
V_HIREDATE DATE;
BEGIN
V_EMPNO := 7788;
SELECT E.ENAME
,E.JOB
,E.HIREDATE
INTO V_ENAME
,V_JOB
,V_HIREDATE
FROM EMP E
WHERE V_EMPNO = E.EMPNO;
DBMS_OUTPUT.put_line(V_ENAME);
DBMS_OUTPUT.put_line(V_JOB);
DBMS_OUTPUT.put_line(V_HIREDATE); -- American date , The year of the sun and the moon
DBMS_OUTPUT.put_line(TO_CHAR(V_HIREDATE,'YYYY-MM-DD DAY'));
-- Limited format output , Add the day of the week
END;
(2) Query through input variables ( Use & Symbol )
DECLARE
V_EMPNO NUMBER;
V_ENAME VARCHAR2(10);
V_JOB VARCHAR2(9);
V_HIREDATE DATE;
BEGIN
--& The function of this symbol is to pop up a data transfer window ,
-- Text message followed “input_empno” As a reminder
V_EMPNO := &input_empno;
SELECT E.ENAME
,E.JOB
,E.HIREDATE
INTO V_ENAME
,V_JOB
,V_HIREDATE
FROM EMP E
WHERE V_EMPNO = E.EMPNO;
DBMS_OUTPUT.put_line(V_ENAME);
DBMS_OUTPUT.put_line(V_JOB);
DBMS_OUTPUT.put_line(V_HIREDATE); -- American date , The year of the sun and the moon
DBMS_OUTPUT.put_line(TO_CHAR(V_HIREDATE,'YYYY-MM-DD DAY'));
-- Limited format output , Add the day of the week
END;
SELECT * FROM EMP;
Be careful :
- When inputting data manually, you should pay attention to the input type
- Such as the input VARCHAR2 Type should be enclosed in single quotation marks ,
- Input date Type should be used to_date function , You can't type in number Data of type
eg:
DECLARE
V_STR VARCHAR2(30);
V_DATE DATE;
V_NUM NUMBER;
BEGIN
V_STR := &INPUT_STR; -- Input with single quotation marks
V_DATE := &INPUT_DATE; -- The date type should be converted when entering
--to_date() function
V_NUM := &INPUT_NUM;
DBMS_OUTPUT.put_line(V_STR);
DBMS_OUTPUT.put_line(V_DATE);
DBMS_OUTPUT.put_line(V_NUM);
END;
eg: According to the entered employee number , Output employee's bonus , If blank, output 0;
DECLARE
V_EMPNO VARCHAR2(4);
V_COMM NUMBER;
BEGIN
V_EMPNO := &input_empno;
SELECT E.COMM
INTO V_COMM
FROM EMP E
WHERE E.EMPNO = V_EMPNO;
DBMS_OUTPUT.put_line(NVL(V_COMM,0));
END;
(3) Variable declaration type
- %type
- %rowtype
- Constant constant
a.%type
- %type : Can be used to determine the data type
eg: According to the entered employee number , Output employee's bonus , If blank, output 0;
DECLARE
V_EMPNO EMP.EMPNO%TYPE;
V_COMM EMP.COMM%TYPE;
BEGIN
V_EMPNO := &input_empno;
SELECT E.COMM
INTO V_COMM
FROM EMP E
WHERE E.EMPNO = V_EMPNO;
DBMS_OUTPUT.put_line(NVL(V_COMM,0));
END;
b.%rowtype
- %rowtype: Refer to the data type and length of all fields in a table in the database ;
eg: According to the entered employee number , Output employee's bonus , If blank, output 0;
DECLARE
V_EMP EMP%ROWTYPE;
BEGIN
V_EMP.EMPNO := &input_empno;
SELECT E.COMM
INTO V_EMP.COMM
FROM EMP E
WHERE E.EMPNO = V_EMP.EMPNO;
DBMS_OUTPUT.put_line(NVL(V_EMP.COMM,0));
END;
Be careful :
- When using %rowtype when , It is equivalent to using the field name of an entire table as a variable to declare variables , When using variables, pay attention to using the variable name of the corresponding field ( That is to use V_EMP. Field name To call ), If you do not use this form to call or omit to write oneortwo characters, an error will be reported ;
eg: use %TYPE Declare variable type , The printed job number is 7698 The job of , name Department number ;
DECLARE
V_EMPNO EMP.EMPNO%TYPE := 7698;
V_ENAME EMP.ENAME%TYPE;
V_DEPTNO EMP.DEPTNO%TYPE;
BEGIN
SELECT E.ENAME
,E.DEPTNO
INTO V_ENAME
,V_DEPTNO
FROM EMP E
WHERE E.EMPNO = V_EMPNO;
DBMS_OUTPUT.put_line(V_ENAME);
DBMS_OUTPUT.put_line(V_DEPTNO);
END;
eg: use %ROWTYPE Declare variable type , The printed job number is 7698 The job of , name Department number ;
DECLARE
V_EMP EMP%ROWTYPE;
BEGIN
V_EMP.EMPNO := 7698;
SELECT E.JOB
,E.ENAME
,E.DEPTNO
INTO V_EMP.JOB
,V_EMP.ENAME
,V_EMP.DEPTNO
FROM EMP E
WHERE V_EMP.EMPNO = E.EMPNO;
DBMS_OUTPUT.put_line(V_EMP.JOB);
DBMS_OUTPUT.put_line(V_EMP.ENAME);
DBMS_OUTPUT.put_line(V_EMP.DEPTNO);
END;
c. Constant (CONSTANT)
Constants are given initial values when declared , No changes can be made throughout the life cycle ;
- Use... When using CONSTANT Keyword declaration constant ;
eg: Calculate the area of a circle ;
DECLARE
PI CONSTANT NUMBER := 3.14; -- PI length value ①
R NUMBER := 3; -- The default value for the radius of a circle is 3 ②
--R NUMBER := 3;
AREA NUMBER; -- area .
BEGIN
R := 5;
AREA := PI * R * R; -- Calculated area 78.5
DBMS_OUTPUT.PUT_LINE(AREA); -- The area of the output circle 78.5
R := 6;
AREA := PI * R * R; -- Calculated area 113.04
DBMS_OUTPUT.PUT_LINE(AREA); -- The area of the output circle
END;
边栏推荐
猜你喜欢

Cloud native (to be updated)

asp. Net upload image path and image name

Porting ucosiii to stm32f429

asp. Net datalist when there are multiple data displays

Hack the box:routerspace

Section 9: dual core startup of zynq

推荐系统系列精讲(第五讲): 排序模型的调优实践

Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)

分析 NFT 项目的 5 个指标

kubernetes删除pod的流程的源码简析
随机推荐
Kubernetes theoretical basis
Section 9: dual core startup of zynq
Code submission specification
Resizing node of rediscluster cluster cluster mode
kubernetes集群命令行工具kubectl
卸载重装最新版mysql数据库亲测有效
In idea, the get and set methods may be popular because the Lombok plug-in is not installed
Is it reliable to register and open an account for stock speculation? Is it safe?
SOC clock configuration
Ice - resources
HJ进制转换
8 张图 | 剖析 Eureka 的首次同步注册表
Cloud native (to be updated)
Online WPS tool
vite2.9 中指定路径别名
ABAP skill tree
HJ delete the character with the least number of occurrences in the string
At 19:00 on Tuesday evening, the 8th live broadcast of battle code Pioneer - how to participate in openharmony's open source contribution in multiple directions
Install haproxy
剑指Offer||:链表(简单)