当前位置:网站首页>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 to search products and realize paging function
- vite2.9 中指定路径别名
- Es data export CSV file
- Practice of traffic recording and playback in vivo
- ZYNQ_ IIC read / write m24m01 record board status
- Can okcc call centers work without computers?
- Co process, asyncio, asynchronous programming
- R language ggmap visual cluster
- 2021 programming language ranking summary
- open62541直接导入NodeSet文件
猜你喜欢

Kubernetes cluster command line tool kubectl

SOC serial port configuration

Kubernetes theoretical basis

asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...

Online WPS tool

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

GoLand IDE and delve debug Go programs in kubernetes cluster

剑指Offer||:链表(简单)

Open62541 import nodeset file directly

ZYNQ_ IIC read / write m24m01 record board status
随机推荐
Cloud native: cloud computing technology is upgraded again to open an era of comprehensive cloud development
Hash slot of rediscluster cluster cluster implementation principle
HJ字符串排序
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
阿里云服务器创建快照、回滚磁盘
在idea中,get和set方法爆红可能是没有安装Lombok插件
Cloud native (to be updated)
Leetcode learning records
SOC timer and interrupt configuration
Design of DSP image data stream
Redis one master multi slave cluster setup
kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
Open62541 import nodeset file directly
本周二晚19:00战码先锋第8期直播丨如何多方位参与OpenHarmony开源贡献
What is EC blower fan?
自动化测试的生命周期是什么?
okcc呼叫中心没有电脑的坐席能不能开展工作?
pip 更新到最新的版本
ZYNQ_ IIC read / write m24m01 record board status
8 figures | analyze Eureka's first synchronization registry