ORACLE存储过程 把一个表数据插入另一个表
时间: 2024-10-19 20:17:28 浏览: 39
Oracle存储过程是一种预编译的SQL代码块,可以在数据库服务器上执行,通常用于封装复杂的业务逻辑。如果你想从一个表中提取数据并将其插入到另一个表中,你可以创建一个这样的存储过程:
```sql
CREATE OR REPLACE PROCEDURE INSERT_DATA (
source_table_name IN VARCHAR2,
target_table_name IN VARCHAR2,
condition_clause IN VARCHAR2 DEFAULT NULL -- 可选的WHERE条件
)
AS
BEGIN
-- 如果有条件,添加WHERE子句
IF condition_clause IS NOT NULL THEN
INSERT INTO target_table_name (column_list)
SELECT column_list FROM source_table_name WHERE condition_clause;
ELSE
INSERT INTO target_table_name (column_list)
SELECT * FROM source_table_name; -- 如果无条件,插入所有列
END IF;
COMMIT;
END;
/
```
在这个例子中,你需要替换`source_table_name`、`target_table_name`以及`column_list`为你实际的数据表名和列名。`condition_clause`是一个可选的SQL表达式,用于指定从源表中选择哪些行。
要调用这个存储过程,可以这样做:
```sql
EXECUTE INSERT_DATA('源表名', '目标表名');
```
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045021.png)
![-](https://img-home.csdnimg.cn/images/20241231044930.png)
![-](https://img-home.csdnimg.cn/images/20241231044937.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)