当前位置:网站首页>数据库高级学习笔记--SQL语句
数据库高级学习笔记--SQL语句
2022-07-06 09:13:00 【萍果馅是年糕】
sql的分类
1.静态sql:select * from emp where empno=7788;
--静态sql指在PL/SQL块中使用的sqL语句在编译时是明确的,执行的是确定对象
2.动态sql:select * from emp where empno='参数';
--动态sql是指在PL/SQL块编译时sql语句是不确定的,如根据用户输入的参数的不同而执行不同的操作
(参数可能是sql语句,数字,文字)
------动态sql-----
动态sql:就是把sql写在一个字符串里,在存储过程中解析字符串执行sql。
--(1)执行动态sql的几种方法
(1)&参数输入
(2)使用open-for,fetch和close语句(游标)
对于处理动态多行的查询操作,可以使用oPEN-FOR语句打开游标,
使用FETCH语句循环提取数据,最终使用CLOSE语句关闭游标。
(3)使用批量的动态语句
即在动态sql中使用BULK子句,或使用游标变量时在fetch中使用BULK,或在FORALL语句中使用
(4)使用系统提供的PL/soL包DBMs_sQL来实现动态sQL
(5)使用EXECUTE IMMEDIATE语句
包括DDL语句,DCL语句,DNL语句以及单行的sELECT语句。该方法不能用于处理多行查询语句
--动态sql的作用
(1)可以支持DDl语句,静态sql只能支持DML语句
(2)支持WEB引用程序的查询意愿
(3)可以将业务逻辑先放在表中,然后再动态编译
--使用EXECUTE IMMEDIATE语句执行动态sql-----------
静态sql为自己嵌入到plsql语句中,而动态sql语句在运行时根据不同的情况产生不同的sql语句。
语法:
execute immediate 动态语句字符串
[into 变量列表]
[using 参数列表]
解释:
动态语句字符串:存储指定的sql语句或者plsql块的字符串变量
into:用于存储查询结果
using:参数传递值
动态sql传参数的格式:[:参数名],参数在运行时需要使用using传值
--使用动态sql操作创建一张表-------------
—般的PL/sQL程序设计中,在DML和事务控制的语句中可以直接使用sql,
但是DDL语句及系统控制语句却不能在PL/sql中直接使用,
要想实现在pL/sql中使用DDL语句及系统控制语句,可以通过使用动态sql来实现
--错误的写法
begin
create tables test1000 as select * from emp;
end;
--正确的写法1(不传参也不赋值)
begin
execute immediate 'create table test1000 as select * from emp ';
end;
select * from test1000;
--正确的写法2(将结果集存储在变量中动态运行)
declare
sqls varchar2(100);
begin
sqls:='create table test1001 as select * from emp ';
execute immediate sqls;
end;
select * from test1001;
--------动态sql传参和赋值-----------
using 传参
into 赋值
参数格式[:参数]
---根据员工编号查询薪资
---写法三:传参和赋值
declare
v_sal emp.sal%type;
begin
--执行动态sql
execute immediate 'select sal from emp where empno=:参数'
--变量赋值
into v_sal
--接收参数
using &输入员工编号;
dbms_output.put_line('工资'||v_sal);
end;
---------动态sql语句不传参,只赋值--------------
---例:根据员工编号查询薪资
-----写法四:不传参,只赋值
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;
------例子----------
--创建一个存储过程,通过传递表名,对相应的表进行删除(方法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;
--创建表的语法----------------------------
create table 表名(
字段名 字段类型)
--创建表:根据用户输入的表名及字段名等参数创建表
create or replace procedure create_table(
table_name in varchar2,----表名
field1 in varchar2,---字段1
field1type in varchar2,---字段1的数据类型
field2 in varchar2,---字段2
field2type in varchar2,---字段2的数据类型
field3 in varchar2,---字段3
field3type in varchar2---字段3的数据类型 最后一个参数后面不加逗号
)
is
sqls varchar2(500);
begin
sqls:='create table'||' '||table_name||'('||field1||' '||field1type||','||field2||' '
||field2type||','||field3||' '||field3type||')';
execute immediate sqls;
end;
--调用存储过程创建表??????---权限不足(sys用户)
begin
create_table('test_table','id','varchar2(100)','name','varchar2(100)','age','varchar2(100)');
end;
--插入数据(sys用户)---------------------------------------
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;
--调用
begin
insert_table('1','小红','18');
end;
select * from test_table;
边栏推荐
- February 13, 2022-3-middle order traversal of binary tree
- A brief introduction to the microservice technology stack, the introduction and use of Eureka and ribbon
- 【博主推荐】C#生成好看的二维码(附源码)
- MySQL flush operation
- 【博主推荐】C# Winform定时发送邮箱(附源码)
- PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
- [ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
- Ansible实战系列二 _ Playbook入门
- Copy constructor template and copy assignment operator template
- Global and Chinese market of thermal mixers 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢

CSDN问答模块标题推荐任务(二) —— 效果优化

Breadth first search rotten orange

Installation and use of MySQL under MySQL 19 Linux

Navicat 导出表生成PDM文件

Opencv uses freetype to display Chinese

La table d'exportation Navicat génère un fichier PDM

A trip to Macao - > see the world from a non line city to Macao
![[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)](/img/b7/aae35f049ba659326536904ab089cb.png)
[recommended by bloggers] C MVC list realizes the function of adding, deleting, modifying, checking, importing and exporting curves (with source code)

QT creator shape
![[C language foundation] 04 judgment and circulation](/img/59/4100971f15a1a9bf3527cbe181d868.jpg)
[C language foundation] 04 judgment and circulation
随机推荐
CSDN问答标签技能树(二) —— 效果优化
35 is not a stumbling block in the career of programmers
PyCharm中无法调用numpy,报错ModuleNotFoundError: No module named ‘numpy‘
MySQL other hosts cannot connect to the local database
CSDN博文摘要(一) —— 一个简单的初版实现
CSDN question and answer module Title Recommendation task (I) -- Construction of basic framework
Kubesphere - deploy the actual combat with the deployment file (3)
Remember the interview algorithm of a company: find the number of times a number appears in an ordered array
虚拟机Ping通主机,主机Ping不通虚拟机
Global and Chinese markets of static transfer switches (STS) 2022-2028: Research Report on technology, participants, trends, market size and share
[ahoi2009]chess Chinese chess - combination number optimization shape pressure DP
Have you mastered the correct posture of golden three silver four job hopping?
CSDN Q & a tag skill tree (V) -- cloud native skill tree
Copy constructor template and copy assignment operator template
01项目需求分析 (点餐系统)
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
Other new features of mysql18-mysql8
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
【博主推荐】asp.net WebService 后台数据API JSON(附源码)