import/export 如何将正文和规范打包到 .sql 或 .pkb 文件中
How import/export package body and spec into .sql or .pkb file
我需要 import/export 一个包 body/spec 到一个 .sql 或 .pkb 文件中,仅使用 sql 加命令。
我尝试执行标准 select 但这只会在控制台中显示它,但我需要修改文件。
我还需要做相反的事情,即导入 .sql/.pks/.pkb 文件到数据库以应用更改。
用于导出表格
exp <username>/<password> file=tables.dmp tables=(tab1,tab2)
用于导入表格
imp <username>/<password> file=tables.dmp tables=(tab1,tab2)
要在文件中打印屏幕输出,只需使用
spool <filename>
在所有命令之前将 sql 输出修改为报告,并在 sql 命令之后假脱机作为
Spool file.txt
SELECT * FROM TABLE
SPOOL OFF
如果您想使用 SQL*Plus 来做到这一点,那么 - 如您所知 - spool
似乎是一个 natural 选项。这是一个例子,看看是否有帮助。
首先,我将创建一个简单的包:
SQL> create or replace package pkg_test as
2 function f_today return date;
3 end;
4 /
Package created.
SQL> create or replace package body pkg_test as
2 function f_today return date is
3 begin
4 return sysdate;
5 end;
6 end;
7 /
Package body created.
SQL> select pkg_test.f_today from dual;
F_TODAY
-------------------
02.09.2019 22:28:56
SQL>
为了创建一个 nice 输出文件:
- 有一些 SQL*Plus 设置 应该设置
- 如果您要导出包,我们将查询
user_source
- 它不包含
create (or replace)
所以我会 select 它单独
- 终止斜线也是如此
/
所有这些都将存储在 .SQL
文件中。如果您直接 运行 这些命令,导出文件也将包含 select
语句,这是您要避免的事情。
Spool.sql
文件:
set heading off
set feedback off
set pagesize 0
set termout off
set trimout on
set trimspool on
set recsep off
set linesize 120
spool pkg_test.sql
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE'
order by line;
select '/' from dual;
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE BODY'
order by line;
select '/' from dual;
spool off;
让我们运行吧;由于所有这些 SET
命令,您实际上看不到任何东西; SQL>
提示将是全部,好像什么都没发生:
SQL> @spool
SQL>
但是,如果您检查 pkg_test.sql
文件中的内容,您会看到包:
SQL> $type pkg_test.sql
create or replace
package pkg_test as
function f_today return date;
end;
/
create or replace
package body pkg_test as
function f_today return date is
begin
return sysdate;
end;
end;
/
SQL>
看起来不错,所以 - 回答你的第二个问题(如何 导入 它回来) - 只是 运行 它。我先退出SQL*Plus;否则 - 再次因为 SET
命令 - 你将看不到任何东西:
SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
C:\Users\lf>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Pon Ruj 2 22:36:06 2019
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> @pkg_test
Package created.
Package body created.
SQL>
我需要 import/export 一个包 body/spec 到一个 .sql 或 .pkb 文件中,仅使用 sql 加命令。
我尝试执行标准 select 但这只会在控制台中显示它,但我需要修改文件。
我还需要做相反的事情,即导入 .sql/.pks/.pkb 文件到数据库以应用更改。
用于导出表格
exp <username>/<password> file=tables.dmp tables=(tab1,tab2)
用于导入表格
imp <username>/<password> file=tables.dmp tables=(tab1,tab2)
要在文件中打印屏幕输出,只需使用
spool <filename>
在所有命令之前将 sql 输出修改为报告,并在 sql 命令之后假脱机作为
Spool file.txt
SELECT * FROM TABLE
SPOOL OFF
如果您想使用 SQL*Plus 来做到这一点,那么 - 如您所知 - spool
似乎是一个 natural 选项。这是一个例子,看看是否有帮助。
首先,我将创建一个简单的包:
SQL> create or replace package pkg_test as
2 function f_today return date;
3 end;
4 /
Package created.
SQL> create or replace package body pkg_test as
2 function f_today return date is
3 begin
4 return sysdate;
5 end;
6 end;
7 /
Package body created.
SQL> select pkg_test.f_today from dual;
F_TODAY
-------------------
02.09.2019 22:28:56
SQL>
为了创建一个 nice 输出文件:
- 有一些 SQL*Plus 设置 应该设置
- 如果您要导出包,我们将查询
user_source
- 它不包含
create (or replace)
所以我会 select 它单独 - 终止斜线也是如此
/
所有这些都将存储在 .SQL
文件中。如果您直接 运行 这些命令,导出文件也将包含 select
语句,这是您要避免的事情。
Spool.sql
文件:
set heading off
set feedback off
set pagesize 0
set termout off
set trimout on
set trimspool on
set recsep off
set linesize 120
spool pkg_test.sql
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE'
order by line;
select '/' from dual;
select 'create or replace' from dual;
select text from user_source
where name = 'PKG_TEST'
and type = 'PACKAGE BODY'
order by line;
select '/' from dual;
spool off;
让我们运行吧;由于所有这些 SET
命令,您实际上看不到任何东西; SQL>
提示将是全部,好像什么都没发生:
SQL> @spool
SQL>
但是,如果您检查 pkg_test.sql
文件中的内容,您会看到包:
SQL> $type pkg_test.sql
create or replace
package pkg_test as
function f_today return date;
end;
/
create or replace
package body pkg_test as
function f_today return date is
begin
return sysdate;
end;
end;
/
SQL>
看起来不错,所以 - 回答你的第二个问题(如何 导入 它回来) - 只是 运行 它。我先退出SQL*Plus;否则 - 再次因为 SET
命令 - 你将看不到任何东西:
SQL> exit
Disconnected from Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
C:\Users\lf>sqlplus scott/tiger
SQL*Plus: Release 11.2.0.2.0 Production on Pon Ruj 2 22:36:06 2019
Copyright (c) 1982, 2014, Oracle. All rights reserved.
Connected to:
Oracle Database 11g Express Edition Release 11.2.0.2.0 - 64bit Production
SQL> @pkg_test
Package created.
Package body created.
SQL>