当前位置:网站首页>Database advanced learning notes -- SQL statement
Database advanced learning notes -- SQL statement
2022-07-06 11:09:00 【Pingguo stuffed with rice cakes】
sql The classification of
1. static state sql:select * from emp where empno=7788;
-- static state sql Referring to PL/SQL Block sqL Statements are explicit at compile time , It's about identifying objects
2. dynamic sql:select * from emp where empno=' Parameters ';
-- dynamic sql It means in PL/SQL Block compile time sql The statement is indeterminate , For example, different operations are performed according to the parameters entered by the user
( The parameter could be sql sentence , Numbers , written words )
------ dynamic sql-----
dynamic sql: Is to put sql Written in a string , Parse the string in the stored procedure to execute sql.
--(1) Execution dynamics sql Several ways to
(1)& Parameter input
(2) Use open-for,fetch and close sentence ( The cursor )
For processing dynamic multi row query operations , have access to oPEN-FOR Statement open cursor ,
Use FETCH Statement loop extract data , End use CLOSE Statement close cursor .
(3) Use batch dynamic statements
That is, in the dynamic sql Use in BULK Clause , Or when using cursor variables fetch Use in BULK, Or in the FORALL Use in statement
(4) Use what the system provides PL/soL package DBMs_sQL To achieve dynamic sQL
(5) Use EXECUTE IMMEDIATE sentence
Include DDL sentence ,DCL sentence ,DNL Statements and single line sELECT sentence . This method cannot be used to process multi line query statements
-- dynamic sql The role of
(1) Can support DDl sentence , static state sql Can only support DML sentence
(2) Support WEB The inquiry intention of the quoting program
(3) You can put the business logic in the table first , Then dynamically compile
-- Use EXECUTE IMMEDIATE Statement execution is dynamic sql-----------
static state sql Embed yourself in plsql In the sentence , And dynamic sql Statements produce different sql sentence .
grammar :
execute immediate Dynamic statement string
[into Variable list ]
[using parameter list ]
explain :
Dynamic statement string : Store the specified sql Statements or plsql String variable of block
into: Used to store query results
using: Parameters pass values
dynamic sql Format of parameters :[: Parameter name ], Parameters need to be used at run time using Pass value
-- Usage dynamics sql Operation to create a table -------------
— Sort PL/sQL In programming , stay DML And transaction control statements can be used directly sql,
however DDL Statement and system control statement can't be in PL/sql You can use ,
To achieve in pL/sql Use in DDL Statements and system control statements , You can use dynamic sql To achieve
-- The wrong way to write
begin
create tables test1000 as select * from emp;
end;
-- Correct writing 1( Neither pass parameters nor assign values )
begin
execute immediate 'create table test1000 as select * from emp ';
end;
select * from test1000;
-- Correct writing 2( Store the result set in variables and run dynamically )
declare
sqls varchar2(100);
begin
sqls:='create table test1001 as select * from emp ';
execute immediate sqls;
end;
select * from test1001;
-------- dynamic sql Transfer parameter and assignment -----------
using The ginseng
into assignment
Parameter format [: Parameters ]
--- Query salary according to employee number
--- Write three : Transfer parameter and assignment
declare
v_sal emp.sal%type;
begin
-- Execution dynamics sql
execute immediate 'select sal from emp where empno=: Parameters '
-- Variable assignment
into v_sal
-- Receiving parameters
using & Enter employee number ;
dbms_output.put_line(' Wages '||v_sal);
end;
--------- dynamic sql Statement does not pass parameters , Assign only --------------
--- example : Query salary according to employee number
----- Write four : Don't pass it on , Assign only
declare
v_sal emp.sal%type;
begin
execute immediate 'select sal from emp where empno=7788'
into v_sal;
dbms_output.put_line(v_sal);
end;
------ Example ----------
-- Create a stored procedure , By passing the table name , Delete the corresponding table ( Method 2)??????
create or replace procedure truncate_table(table_name in varchar2)
is
sqls varchar2(100);
begin
sqls:='truncate table'||table_name;
execute immediate sqls;
end;
begin
truncate_table('test1001');
end;
select * from test1001;
-- Syntax for creating tables ----------------------------
create table Table name (
Field name Field type )
-- Create table : Create a table according to the table name, field name and other parameters entered by the user
create or replace procedure create_table(
table_name in varchar2,---- Table name
field1 in varchar2,--- Field 1
field1type in varchar2,--- Field 1 Data type of
field2 in varchar2,--- Field 2
field2type in varchar2,--- Field 2 Data type of
field3 in varchar2,--- Field 3
field3type in varchar2--- Field 3 Data type of The last parameter is not followed by a comma
)
is
sqls varchar2(500);
begin
sqls:='create table'||' '||table_name||'('||field1||' '||field1type||','||field2||' '
||field2type||','||field3||' '||field3type||')';
execute immediate sqls;
end;
-- Call the stored procedure to create the table ??????--- Insufficient authority (sys user )
begin
create_table('test_table','id','varchar2(100)','name','varchar2(100)','age','varchar2(100)');
end;
-- insert data (sys user )---------------------------------------
create or replace procedure insert_table(
id in varchar2,
name in varchar2,
age in varchar2
)
is
sqls varchar2(500);
begin
sqls:='insert into test_table values(:1,:2,:3)';
execute immediate sqls using id,name,age;
end;
-- call
begin
insert_table('1',' Xiaohong ','18');
end;
select * from test_table;
边栏推荐
- Armv8-a programming guide MMU (2)
- [recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)
- 机器学习--人口普查数据分析
- Generate PDM file from Navicat export table
- 报错解决 —— io.UnsupportedOperation: can‘t do nonzero end-relative seeks
- 解决扫描不到xml、yml、properties文件配置
- MySQL other hosts cannot connect to the local database
- Postman environment variable settings
- A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
- Redis的基础使用
猜你喜欢
Why can't I use the @test annotation after introducing JUnit
Valentine's Day is coming, are you still worried about eating dog food? Teach you to make a confession wall hand in hand. Express your love to the person you want
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Navicat 導出錶生成PDM文件
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
【博主推荐】C#生成好看的二维码(附源码)
Basic use of redis
机器学习--人口普查数据分析
Mysql21 user and permission management
Copie maître - esclave MySQL, séparation lecture - écriture
随机推荐
There are three iPhone se 2022 models in the Eurasian Economic Commission database
@controller,@service,@repository,@component区别
Neo4j installation tutorial
Ansible practical Series II_ Getting started with Playbook
La table d'exportation Navicat génère un fichier PDM
MySQL19-Linux下MySQL的安装与使用
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
Record a problem of raspberry pie DNS resolution failure
Image recognition - pyteseract TesseractNotFoundError: tesseract is not installed or it‘s not in your path
Redis的基础使用
Invalid global search in idea/pychar, etc. (win10)
MySQL的一些随笔记录
QT creator design user interface
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
@Controller, @service, @repository, @component differences
PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
1. Mx6u learning notes (VII): bare metal development (4) -- master frequency and clock configuration
Solution: log4j:warn please initialize the log4j system properly
A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)