oracle之--动态sql(execute immediate ‘ ‘)

动态sql--execute immediate ' '

原因:ddl语句,truncate语句 不能直接使用,需要封装起来

sql 复制代码
--动态sql--execute immediate ' '====因为ddl,truncate 不能直接使用,需要封装起来
--1.TRUNCATE table 
declare 
BEGIN
--truncate table emp_4;
	execute immediate 'truncate table emp_4';--立即执行清除表数据(工作中经常使用)
end;

--2.create table emp_4 as selcet * from emp;--备份表(工作中经常使用)
declare 
BEGIN
	execute immediate 'create table emp_4 as selcet * from emp';
end;

--3.alter table emp_4 ADD aaa number;---添加aaa表字段
declare 
BEGIN
	execute immediate 'alter table emp_4 ADD aaa number';
end;

--4.alter table emp_4 modify aaa varchar2(10);---修改aaa的字段类型
declare 
BEGIN
	execute immediate 'alter table emp_4 modify aaa varchar2(10)';
end;

--5.alter table emp_4 drop COLUMN aaa;--删除aaa表字段
declare 
BEGIN
	execute immediate 'alter table emp_4 drop COLUMN aaa';
end;

--6.alter table emp_4 rename column aaa to bbb;--修改字段名
declare 
BEGIN
	execute immediate 'alter table emp_4 rename column aaa to bbb';
end;

--7.alter table emp_4 rename to emp_8;--修改表名
declare 
BEGIN
	execute immediate 'alter table emp_4 rename to emp_8';
end;

--8.drop table emp_4 ;--删除表
declare 
BEGIN
	execute immediate 'drop table emp_4';
end;
相关推荐
阳光明媚sunny1 小时前
Room持久化库中,@Transaction注解的正确使用场景是?
android·数据库
北极糊的狐1 小时前
MySQL常见报错分析及解决方案总结(15)---Can’t connect to MySQL server on ‘localhost‘ (10061)
数据库·mysql
濑户川1 小时前
Django5 与 Vue3 表单交互全解析:从基础到实战
数据库
weixin_438077491 小时前
langchain官网翻译:Build a Question/Answering system over SQL data
数据库·sql·langchain·agent·langgraph
-雷阵雨-2 小时前
MySQL——数据库操作攻略
数据库·mysql
krielwus2 小时前
Oracle ORA-01653 错误检查以及解决笔记
数据库·oracle
Wadli2 小时前
csdn| MySQL
数据库·mysql
程序员水自流3 小时前
MySQL InnoDB存储引擎关键核心特性详细介绍
java·数据库·mysql
-雷阵雨-3 小时前
MySQL——表的操作
数据库·mysql
阿巴~阿巴~3 小时前
Ubuntu 20.04 安装 Redis
linux·服务器·数据库·redis·ubuntu