当前位置:网站首页>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;
边栏推荐
- 云原生:云计算技术再次升级 开启全面云开发时代
- A single node obtains the lock lock of the order number
- 腾讯下半年继续裁员,所有事业群至少缩减 10%,对此你怎么看?关注者
- 打新债注册开户靠谱吗?安全吗?
- asp. Net datalist when there are multiple data displays
- flutter 实现摇一摇功能
- 8 figures | analyze Eureka's first synchronization registry
- asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
- Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
- Analyze 5 indicators of NFT project
猜你喜欢

Section VI UART of zynq

Kubernetes cluster command line tool kubectl

Section 5: zynq interrupt

卸载重装最新版mysql数据库亲测有效

vite2.9 中指定路径别名

flex布局

In idea, the get and set methods may be popular because the Lombok plug-in is not installed

kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑

Software design of power control board

SOC timer and interrupt configuration
随机推荐
HJ21 简单密码
自动化测试的生命周期是什么?
卸载重装最新版mysql数据库亲测有效
R language ggmap
逆波兰表达式求值<难度系数>
PLC -- Notes
Open62541 import nodeset file directly
HJ prime factor
Unity-UI-shadow组件
Mysql57 zip file installation
7-1 understand everything
kubernetes集群命令行工具kubectl
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
Application of XOR. (extract the rightmost 1 in the number, which is often used in interviews)
kubernetes删除pod的流程的源码简析
asp. Net to search products and realize paging function
Software design of resistance test board
HJ整数与IP地址间的转换
linux下修改mysql用户名root
golang gin框架进行分块传输