当前位置:网站首页>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;
边栏推荐
- asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
- Is it reliable to open a new bond registration account? Is it safe?
- 2021 programming language ranking summary
- Hj21 simple password
- 安全培训是员工最大的福利!2022新员工入职安全培训全员篇
- R language ggmap
- The solution of "user account control to continue, please enter administrator user name and password" appears in win10 Professional Edition
- HJ prime factor
- HJ score ranking
- R language drawing ggplot2 seasonal graph
猜你喜欢

Recommended system series (Lecture 5): Optimization Practice of sorting model

Tencent continued to lay off staff in the second half of the year, and all business groups reduced by at least 10%. What do you think of this? Followers

Design of DSP image data stream

云原生:云计算技术再次升级 开启全面云开发时代

Section VI UART of zynq

Analyze 5 indicators of NFT project

SOC timer and interrupt configuration

kubelet垃圾(退出的容器和未使用的镜像)回收源码分析

Software design of resistance test board

Section VII starting principle and configuration of zynq
随机推荐
kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
Resizing node of rediscluster cluster cluster mode
R language ggmap visual cluster
goland IDE和delve调试位于kubernetes集群中的go程序
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
Implementation of commit message standardized control in large projects
Source code analysis of kubernetes' process of deleting pod
asp. Net to search products and realize paging function
GPIO configuration of SOC
R and RGL draw 3D knots
软件测试与质量期末复习
HJ character count
golang gin框架进行分块传输
Recommended system series (Lecture 5): Optimization Practice of sorting model
Hash slot of rediscluster cluster cluster implementation principle
Makefile
Conversion between HJ integer and IP address
open62541直接导入NodeSet文件
R language hitters data analysis
7-2 Finnish wooden chess structure Sorting