当前位置:网站首页>Study notes 22/1/19 and 22/1/20
Study notes 22/1/19 and 22/1/20
2022-06-28 07:44:00 【Zeiyalo】
Learning notes
One 、 The cursor
(1) Cursor without parameters
Cursor declaration format :
DECLARE
CURSOR You name
IS
SELECT sentence ;--- The result set of the query to which the cursor points
BEGIN
-- Open cursor Logical body ( The use of cursors / perform Put it here )
END;
The cursor automatically uses the format :
eg: take EMP Employee job number of the table 、 Print out the name together ;
DECLARE
CURSOR C_EMP IS
SELECT EMPNO
,ENAME
FROM EMP ;
BEGIN
FOR I IN C_EMP LOOP -- Call cursor format
DBMS_OUTPUT.put_line(I.EMPNO||' '||I.ENAME);
END LOOP;
END;
(2) Cursors with parameters
Declaration format :
DECLARE
CURSOR You name ( Parameters 1 data type , Parameters 2 data type .. .. )
IS
SELECT sentence ;--- The result set of the query to which the cursor points
BEGIN
-- Open cursor Logical body ( The use of cursors / perform Put it here )
END;
eg: Pass in a department number , Print out the names of the employees in this department ;
DECLARE
CURSOR C_EMP(P_DEPTNO NUMBER ) IS
SELECT T.ENAME
FROM EMP T
WHERE T.DEPTNO = P_DEPTNO ;
V_DEPTNO NUMBER;
V_JOB VARCHAR2(30);
BEGIN
V_DEPTNO:= & Enter the department number you want to print ;
FOR I IN C_EMP(V_DEPTNO) LOOP
DBMS_OUTPUT.put_line(I.ENAME);
END LOOP;
END;
eg: Update according to business requirements EMP_0118 Pay for
Pass in a department number , Update the salary of employees in this department
1. If the employee's basic salary is lower than 1500, Just give this employee 1000 The basic salary of
2. If the employee's basic salary is higher than 1500, And the bonus is empty or equal to 0, Just give this employee 100 A dollar bonus
DECLARE
CURSOR C_EMP(P_DEPTNO NUMBER) IS
SELECT T.EMPNO
,T.SAL
,T.COMM
FROM EMP_0118 T
WHERE T.DEPTNO = P_DEPTNO;
V_DEPTNO NUMBER;
BEGIN
V_DEPTNO := 10;
FOR I IN C_EMP(V_DEPTNO) LOOP
IF I.SAL <1500 THEN
UPDATE EMP_0118 T SET T.SAL = T.SAL + 1000
WHERE T.EMPNO = I.EMPNO;
ELSIF I.SAL >= 1500 AND NVL(I.COMM,0) = 0 THEN
UPDATE EMP_0118 T SET T.COMM = 100
WHERE T.EMPNO = I.EMPNO;
END IF;
END LOOP;
COMMIT;
END;
We need to pay attention to :
- The data type of the parameter should not have length , Just define the type ;
- Parameters are defined , You have to pass parameters in ;
- What data type is the defined parameter , When transferring parameters, you must transfer the value of the data type .
The difference between parameters and variables :
- Data type cannot be added to parameter declaration 【 length 】 , Variables need to be specified in length ;
- Variable After the statement , It can be unassigned perhaps After the assignment It can also be done without , But the parameters
Just make a statement , Must use .
I'm sorry for cursor This will be some of the views of ,cursor This would look to me like a pointer to a table that can store all types of tables , Point to the table we need , It feels like a function , But it's different , Later, I learned about stored procedures 、 After the functions and views, come back and summarize the differences between the four !
Two 、 Character splicing and dynamic SQL
(1) Character splicing
This part is about mastering two things :
- || Connector
- Various escape characters
First of all, we need to know what we need , In fact, strings and variables can be connected with connectors , I also learned to use when learning simple query statements || Connect the percent sign , Just know that it is a connector ;
Then there is the matter of escape characters , First , We know that oracle The part enclosed in single quotation marks is the data of a character type , But when I first started learning single quotation marks, I thought about how to use single quotation marks to represent a simple string , Is to output a string , There are actually two ways to do this ;
1. Escape character
First we need to know what the escape character means , seeing the name of a thing one thinks of its function , That is to say, the special meaning of the special symbol behind the symbol is converted to it , Make it output as a normal character , If you are interested in this aspect, you can take a look at the compilation principle ;
There are two kinds of escape characters
(2) dynamic SQL
1. Use dynamic SQL Perform simple DDL sentence :
eg: Execute table creation statement ;
BEGIN
--CREATE TABLE EMP_0120 AS SELECT * FROM EMP;
EXECUTE IMMEDIATE 'CREATE TABLE EMP_0120 AS SELECT * FROM EMP';
END;
eg: Execute delete statement ;
DECLARE
V_JOB VARCHAR2(20);
V_SQL VARCHAR2(4000);
BEGIN
V_JOB := &INPUT_JOB;
--DELETE FROM EMP_0103 T WHERE T.JOB = V_JOB;
----- Single quotation marks are special characters , Represents a single quotation mark in a string , You need two single quotation mark escapes to represent a single quotation mark
--chr(39) It also means a single quotation mark
--V_SQL := 'DELETE FROM EMP_0103 T WHERE T.JOB ='||'''' ||V_JOB ||'''';
V_SQL := 'DELETE FROM EMP_0120 T WHERE T.JOB =' ||CHR(39)||V_JOB||CHR(39);
DBMS_OUTPUT.put_line(V_SQL);
EXECUTE IMMEDIATE V_SQL;
--COMMIT;
END;
2. Parameterized SQL sentence
- If the dynamic statement is SELECT sentence , You can save the query results to INTO In the following variables .
If there are parameters in the dynamic statement ,USING Pass values for parameters in the statement .
dynamic SQL The parameter format in is : Parameter name , Parameters need to be used at run time USING Pass value .
eg: Based on the incoming empno and deptno Find out the name and salary of the corresponding employee ;
- Directly in the program block select sentence
DECLARE
V_SQL VARCHAR2(4000);
V_ENAME VARCHAR2(40);
V_SAL NUMBER;
V_EMPNO NUMBER;
V_DEPTNO NUMBER;
BEGIN
V_EMPNO :=&INPUT_EMPNO;
V_DEPTNO := &INPUT_DEPTNO;
SELECT T.ENAME
,T.SAL
INTO V_ENAME
,V_SAL
FROM EMP T
WHERE T.EMPNO = V_EMPNO
AND T.DEPTNO = V_DEPTNO;
DBMS_OUTPUT.put_line(V_ENAME ||' '|| V_SAL);
END;
- Use dynamic with parameters SQL sentence
DECLARE
V_SQL VARCHAR2(4000);
V_ENAME VARCHAR2(40);
V_SAL NUMBER;
V_EMPNO NUMBER;
V_DEPTNO NUMBER;
BEGIN
V_EMPNO :=&INPUT_EMPNO;
V_DEPTNO := &INPUT_DEPTNO;
V_SQL := 'SELECT T.ENAME ,T.SAL FROM EMP T WHERE T.EMPNO = :P_EMPNO AND T.DEPTNO = :P_DEPTNO'; ---- use : Parameter name
EXECUTE IMMEDIATE V_SQL
INTO V_ENAME --- dynamic SQL The data found should be given to variables
,V_SAL
USING V_EMPNO,V_DEPTNO; --- Pay attention to the order of parameters
-- Put the variable V_EMPNO Pass to dynamic SQL Parameters of P_EMPNO
DBMS_OUTPUT.put_line(V_ENAME ||' '|| V_SAL);
END;
eg: Pass in a job number with dynamic parameters SQL Methods Or use String concatenated without parameters Put this job number The employee's name and salary can be found out , Print name and salary on the output window ;
- With parameters SQL
DECLARE
V_SQL VARCHAR(3000);
V_ENO NUMBER;
V_EN VARCHAR2(20);
V_SAL NUMBER;
BEGIN
V_ENO := &INPUT_EMPNO;
V_SQL := 'SELECT ENAME ,SAL FROM EMP WHERE EMPNO = : P_EMPNO ';
EXECUTE IMMEDIATE V_SQL
INTO V_EN
,V_SAL
USING V_ENO;
COMMIT;
DBMS_OUTPUT.put_line(V_EN||' '||V_SAL);
END;
- Splicing without parameters
DECLARE
V_SQL VARCHAR2(3000);
V_ENO NUMBER := &INPUT_EMPNO;
V_EN VARCHAR2(10);
V_SAL NUMBER;
BEGIN
V_SQL := ' SELECT ENAME ,SAL FROM EMP WHERE EMPNO = '||V_ENO;
EXECUTE IMMEDIATE V_SQL
INTO V_EN
,V_SAL;
DBMS_OUTPUT.put_line(V_EN||' '||V_SAL);
END;
3、 ... and 、 recursive
It's too late today , The following is a supplement to the story
边栏推荐
猜你喜欢
随机推荐
ES6 use of return in arrow function
A single node obtains the lock lock of the order number
flex布局
asp. Net error "/" server error in the application. String or binary data would be truncated. The statement...
Helloword routine for ROS
打新债注册开户靠谱吗?安全吗?
数字藏品市场“三大套路”
"Three routines" of digital collection market
Unity UI shadow component
Redis one master multi slave cluster setup
kubernetes部署thanos ruler的发送重复告警的一个隐秘的坑
Kubernetes deploys a secret pit where thanos ruler sends repeated alarms
HJ score ranking
Online WPS tool
flex布局
Redis implements distributed locks
软件测试与质量期末复习
基金的投资交易与结算
[thanos source code analysis series]thanos query component source code analysis
Makefile








