当前位置:网站首页>数据库高级学习笔记--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;
边栏推荐
- [BMZCTF-pwn] 12-csaw-ctf-2016-quals hungman
- API learning of OpenGL (2001) gltexgen
- 35 is not a stumbling block in the career of programmers
- Redis的基础使用
- QT creator design user interface
- 解决:log4j:WARN Please initialize the log4j system properly.
- Ansible practical series I_ introduction
- [recommended by bloggers] background management system of SSM framework (with source code)
- The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
- Global and Chinese market of wafer processing robots 2022-2028: Research Report on technology, participants, trends, market size and share
猜你喜欢
Swagger, Yapi interface management service_ SE
[Li Kou 387] the first unique character in the string
机器学习--人口普查数据分析
MySQL20-MySQL的数据目录
连接MySQL数据库出现错误:2059 - authentication plugin ‘caching_sha2_password‘的解决方法
[reading notes] rewards efficient and privacy preserving federated deep learning
Deoldify项目问题——OMP:Error#15:Initializing libiomp5md.dll,but found libiomp5md.dll already initialized.
windows无法启动MYSQL服务(位于本地计算机)错误1067进程意外终止
[download app for free]ineukernel OCR image data recognition and acquisition principle and product application
CSDN问答标签技能树(五) —— 云原生技能树
随机推荐
MySQL flush operation
项目实战-后台员工信息管理(增删改查登录与退出)
Basic use of redis
Some problems in the development of unity3d upgraded 2020 VR
Windows cannot start the MySQL service (located on the local computer) error 1067 the process terminated unexpectedly
[free setup] asp Net online course selection system design and Implementation (source code +lunwen)
Postman uses scripts to modify the values of environment variables
Dotnet replaces asp Net core's underlying communication is the IPC Library of named pipes
CSDN blog summary (I) -- a simple first edition implementation
Invalid global search in idea/pychar, etc. (win10)
C语言标准的发展
API learning of OpenGL (2002) smooth flat of glsl
The virtual machine Ping is connected to the host, and the host Ping is not connected to the virtual machine
[BMZCTF-pwn] 11-pwn111111
CSDN问答标签技能树(五) —— 云原生技能树
Swagger, Yapi interface management service_ SE
安全测试涉及的测试对象
CSDN问答模块标题推荐任务(二) —— 效果优化
csdn-Markdown编辑器
Have you mastered the correct posture of golden three silver four job hopping?