通常我通过脚本创建 Oracle 表,然后通过
SQL> @script_name
从 SQL*Plus 命令行运行该脚本。foo_partitioned.sql
CREATE TABLE foo
(
x NUMBER NOT NULL
)
PARTITION BY RANGE (x)
(
PARTITION part_1 VALUES LESS THAN (10),
PARTITION part_2 VALUES LESS THAN (20),
PARTITION part_3 VALUES LESS THAN (30)
);
ALTER TABLE foo ADD CONSTRAINT pk_foo PRIMARY KEY (x);
foo_not_partitioned.sql
CREATE TABLE foo
(
x NUMBER NOT NULL
);
ALTER TABLE foo ADD CONSTRAINT pk_foo PRIMARY KEY (x);
然后,在一个单独的脚本中,我根据 Oracle 版本调用上述其中一个:
create_foo.sql
DECLARE
vEnterprise NUMBER;
BEGIN
SELECT COUNT(*)
INTO vEnterprise
FROM v$version
WHERE banner LIKE 'Oracle%Enterprise Edition%';
IF (vEnterprise = 0) THEN
@foo_not_partitioned.sql;
ELSE
@foo_partitioned.sql;
END IF;
END;
/
这给了我错误:
ORA-06550:第 11 行,第 1 列:
PLS-00103:遇到符号“CREATE”......
当然会发生这种情况,因为“CREATE TABLE”在 PL/SQL 中无效。
我可以用 create 语句组成一个字符串,然后调用
EXECUTE IMMEDIATE
但我想知道这是否可以在不更改 2 个原始脚本的情况下实现。VARIABLE vEnterprise NUMBER;
BEGIN
:vEnterprise := 0;
-- Check again and set variable for SQL*Plus
SELECT COUNT(*)
INTO :vEnterprise
FROM v$version
WHERE banner LIKE 'Oracle%Enterprise Edition%';
END;
/
-- Now conditionally execute the appropriate script based on vEnterprise
COLUMN vEnterprise NEW_VALUE edition_var;
SELECT CASE WHEN :vEnterprise = 0 THEN 'foo_not_partitioned.sql' ELSE 'foo_partitioned.sql' END AS script_name FROM dual;
@&script_name