如何使用存储过程将一个 table 数据移动到另一个 table

How to move one table data to another table using stored procedure

有一个 table 表示存在数据的部门。

create table department(deptno number, deptname varchar2(50), deptloc varchar2(50));

insert into department values(1,'A','X');
insert into department values(2,'B','Y');
insert into department values(3,'C','Z');

select * from department;

我想使用存储过程将此数据移动到另一个 table 比如说 Department_SP。基本上,我想知道我们如何使用存储过程执行的特定条件或过滤器将数据从一个 table 移动到另一个。我只是想知道这将如何完成。如果你们有这样的例子,我将不胜感激,请分享您对此的看法。

像这样?

... all your code...
-- create table department_cp with same structure as department but empty.
CREATE TABLE department_cp AS SELECT * FROM department WHERE 1 = 2;

SELECT * FROM department_cp;

-- nothing

CREATE OR REPLACE procedure copy_data
AS
BEGIN
  INSERT INTO department_cp SELECT * FROM department;
END;
/

exec copy_data;

SELECT * FROM department_cp;

-- same as department.

那么你的实际问题是什么?

创建客户table

CREATE TABLE customers
(customer_id number(10) NOT NULL, customer_name varchar2(50),city varchar2(50));

向客户插入数据 table

insert into customers (customer_id, customer_name, city) values (1,'Adam','Washington');
insert into customers (customer_id, customer_name, city) values (2,'Ben','NY');
insert into customers (customer_id, customer_name, city) values (3,'Carl','Baltimore');

为城市table创建

CREATE TABLE cities
(city_id number(10) NOT NULL, city_name varchar2(50));

向城市插入数据table

insert into cities (city_id, city_name) values (1, 'Washington');
insert into cities (city_id, city_name) values (2, 'Los Angeles');
insert into cities (city_id, city_name) values (3, 'Chicago');

创建客户副本table(空)

CREATE TABLE customers_cp AS SELECT * FROM customers where 1=0;

如果此人的城市在城市 table 列表

中,则将数据插入客户副本
INSERT INTO CUSTOMERS_CP SELECT * FROM customers where city in (select city_name from cities);

现在 CUSTOMERS_CP table 只包含 1 行,即 Adam, Washington。