如何使用创建或替换?

发布于 2024-07-25 15:48:51 字数 390 浏览 6 评论 0原文

我对 CREATE OR REPLACE 的理解是否正确?基本上意味着“如果对象存在,则删除它,然后以任何一种方式创建它?”

如果是这样,我做错了什么? 这有效:

CREATE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')

而这不起作用(ORA-00922:缺少或无效选项):

CREATE OR REPLACE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')

我在做一些愚蠢的事情吗? 我似乎无法找到有关此语法的太多文档。

Am I correct in understanding that CREATE OR REPLACE basically means "if the object exists, drop it, then create it either way?"

If so, what am I doing wrong? This works:

CREATE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')

And this doesn't (ORA-00922: missing or invalid option):

CREATE OR REPLACE TABLE foo (id NUMBER,
title VARCHAR2(4000) DEFAULT 'Default Title')

Am I doing something stupid? I don't seem to be able to find much documentation about this syntax.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(14

淑女气质 2024-08-01 15:48:51

这适用于函数、过程、包、类型、同义词、触发器和视图。

更新:

第三次更新帖子后,我将重新表述:

这不适用于表格:)

是的,有 关于此语法的文档,并且 CREATE TABLE 没有 REPLACE 选项。

This works on functions, procedures, packages, types, synonyms, trigger and views.

Update:

After updating the post for the third time, I'll reformulate this:

This does not work on tables :)

And yes, there is documentation on this syntax, and there are no REPLACE option for CREATE TABLE.

彩虹直至黑白 2024-08-01 15:48:51

该语法的好处之一是,您可以确保 CREATE OR REPLACE 永远不会导致您丢失数据(您丢失的最多的是代码,希望您将其存储在源代码控制某处)。

表的等效语法是 ALTER,这意味着您必须显式枚举所需的确切更改。

编辑:
顺便说一句,如果您需要在脚本中执行 DROP + CREATE,并且您不关心虚假的“对象不存在”错误(当 DROP 找不到表时),您可以这样做:

BEGIN
  EXECUTE IMMEDIATE 'DROP TABLE owner.mytable';
EXCEPTION
  WHEN OTHERS THEN
    IF sqlcode != -0942 THEN RAISE; END IF;
END;
/

One of the nice things about the syntax is that you can be sure that a CREATE OR REPLACE will never cause you to lose data (the most you will lose is code, which hopefully you'll have stored in source control somewhere).

The equivalent syntax for tables is ALTER, which means you have to explicitly enumerate the exact changes that are required.

EDIT:
By the way, if you need to do a DROP + CREATE in a script, and you don't care for the spurious "object does not exist" errors (when the DROP doesn't find the table), you can do this:

BEGIN
  EXECUTE IMMEDIATE 'DROP TABLE owner.mytable';
EXCEPTION
  WHEN OTHERS THEN
    IF sqlcode != -0942 THEN RAISE; END IF;
END;
/
等待圉鍢 2024-08-01 15:48:51

Oracle 中没有创建或替换表。

你必须:

DROP TABLE foo;
CREATE TABLE foo (....);

There is no create or replace table in Oracle.

You must:

DROP TABLE foo;
CREATE TABLE foo (....);
心房的律动 2024-08-01 15:48:51

CREATE OR REPLACE 只能用于函数、过程、类型、视图或包 - 它不适用于表。

CREATE OR REPLACE can only be used on functions, procedures, types, views, or packages - it will not work on tables.

病女 2024-08-01 15:48:51

以下脚本应该可以在 Oracle 上实现这一目的:

BEGIN
  EXECUTE IMMEDIATE 'drop TABLE tablename';
EXCEPTION
  WHEN OTHERS THEN
    IF sqlcode != -0942 THEN RAISE; 
    END IF;
END;

Following script should do the trick on Oracle:

BEGIN
  EXECUTE IMMEDIATE 'drop TABLE tablename';
EXCEPTION
  WHEN OTHERS THEN
    IF sqlcode != -0942 THEN RAISE; 
    END IF;
END;
晨光如昨 2024-08-01 15:48:51
-- To Create or Replace a Table we must first silently Drop a Table that may not exist
DECLARE
  table_not_exist EXCEPTION;
  PRAGMA EXCEPTION_INIT (table_not_exist , -00942);
BEGIN
   EXECUTE IMMEDIATE('DROP TABLE <SCHEMA>.<TABLE NAME> CASCADE CONSTRAINTS');
   EXCEPTION WHEN table_not_exist THEN NULL;
END;
/
-- To Create or Replace a Table we must first silently Drop a Table that may not exist
DECLARE
  table_not_exist EXCEPTION;
  PRAGMA EXCEPTION_INIT (table_not_exist , -00942);
BEGIN
   EXECUTE IMMEDIATE('DROP TABLE <SCHEMA>.<TABLE NAME> CASCADE CONSTRAINTS');
   EXCEPTION WHEN table_not_exist THEN NULL;
END;
/
蓬勃野心 2024-08-01 15:48:51

不适用于表格,仅适用于函数等。

这是一个包含一些示例的网站。

Does not work with Tables, only functions etc.

Here is a site with some examples.

俯瞰星空 2024-08-01 15:48:51

对于 Oracle 数据库来说,一个有用的过程,无需使用例外(在某些情况下,您必须将 user_tables 替换为 dba_tables 和/或限制查询中的表空间):

create or replace procedure NG_DROP_TABLE(tableName varchar2)
is
   c int;
begin
   select count(*) into c from user_tables where table_name = upper(tableName);
   if c = 1 then
      execute immediate 'drop table '||tableName;
   end if;
end;

A usefull procedure for oracle databases without using exeptions (under circumstances you have to replace user_tables with dba_tables and/or constrain the tablespace in the query):

create or replace procedure NG_DROP_TABLE(tableName varchar2)
is
   c int;
begin
   select count(*) into c from user_tables where table_name = upper(tableName);
   if c = 1 then
      execute immediate 'drop table '||tableName;
   end if;
end;
夜空下最亮的亮点 2024-08-01 15:48:51

如果您在代码中执行操作,请首先检查数据库中的表
通过使用查询
选择表名
来自用户表
WHERE table_name = 'XYZ'

如果找到记录,则截断表,否则创建表

工作,如创建或替换。

If you are doing in code then first check for table in database
by using query
SELECT table_name
FROM user_tables
WHERE table_name = 'XYZ'

if record found then truncate table otherwise create Table

Work like Create or Replace.

夏夜暖风 2024-08-01 15:48:51

您可以使用 CORT (www.softcraftltd.co.uk/cort)。 该工具允许在 Oracle 中创建或替换表。
它看起来像:

create /*# or replace */ table MyTable(
  ... -- standard table definition
);

它保存数据。

You can use CORT (www.softcraftltd.co.uk/cort). This tool allows to CREATE OR REPLACE table in Oracle.
It looks like:

create /*# or replace */ table MyTable(
  ... -- standard table definition
);

It preserves data.

櫻之舞 2024-08-01 15:48:51

所以我一直在使用它并且它运行得很好: - 它的工作方式更像是 DROP IF EXISTS 但完成了工作

DECLARE
       VE_TABLENOTEXISTS EXCEPTION;
PRAGMA EXCEPTION_INIT(VE_TABLENOTEXISTS, -942);


    PROCEDURE DROPTABLE(PIS_TABLENAME IN VARCHAR2) IS
              VS_DYNAMICDROPTABLESQL VARCHAR2(1024);
                    BEGIN
                       VS_DYNAMICDROPTABLESQL := 'DROP TABLE ' || PIS_TABLENAME;  
                    EXECUTE IMMEDIATE VS_DYNAMICDROPTABLESQL;

                    EXCEPTION
                        WHEN VE_TABLENOTEXISTS THEN
                             DBMS_OUTPUT.PUT_LINE(PIS_TABLENAME || ' NOT EXIST, SKIPPING....');
                        WHEN OTHERS THEN
                             DBMS_OUTPUT.PUT_LINE(SQLERRM);
                    RAISE;
                    END DROPTABLE;

    BEGIN
      DROPTABLE('YOUR_TABLE_HERE');
END DROPTABLE;
/   

希望这会有所帮助
另参考:
PL/SQL Developer 中出现 PLS-00103 错误

So I've been using this and it has worked very well: - it works more like a DROP IF EXISTS but gets the job done

DECLARE
       VE_TABLENOTEXISTS EXCEPTION;
PRAGMA EXCEPTION_INIT(VE_TABLENOTEXISTS, -942);


    PROCEDURE DROPTABLE(PIS_TABLENAME IN VARCHAR2) IS
              VS_DYNAMICDROPTABLESQL VARCHAR2(1024);
                    BEGIN
                       VS_DYNAMICDROPTABLESQL := 'DROP TABLE ' || PIS_TABLENAME;  
                    EXECUTE IMMEDIATE VS_DYNAMICDROPTABLESQL;

                    EXCEPTION
                        WHEN VE_TABLENOTEXISTS THEN
                             DBMS_OUTPUT.PUT_LINE(PIS_TABLENAME || ' NOT EXIST, SKIPPING....');
                        WHEN OTHERS THEN
                             DBMS_OUTPUT.PUT_LINE(SQLERRM);
                    RAISE;
                    END DROPTABLE;

    BEGIN
      DROPTABLE('YOUR_TABLE_HERE');
END DROPTABLE;
/   

Hope this helps
Also reference:
PLS-00103 Error in PL/SQL Developer

臻嫒无言 2024-08-01 15:48:51

“创建或替换表”是不可能的。 正如其他人所说,您可以编写一个过程和/或使用立即开始执行(...)。 因为我没有看到如何(重新)创建表的答案,所以我添加了一个脚本作为答案。

PS:正如 jeffrey-kemp 提到的那样:下面的脚本不会保存您要删除的表中已存在的数据。 由于存在丢失数据的风险,我们公司只允许更改生产环境上现有的表,不允许删除表。 通过使用 drop table 语句,迟早你会让公司警察站在你的办公桌前。

--Create the table 'A_TABLE_X', and drop the table in case it already is present
BEGIN
EXECUTE IMMEDIATE 
'
CREATE TABLE A_TABLE_X
(
COLUMN1 NUMBER(15,0),
COLUMN2  VARCHAR2(255 CHAR),
COLUMN3  VARCHAR2(255 CHAR)
)';

EXCEPTION
WHEN OTHERS THEN
  IF SQLCODE != -955 THEN -- ORA-00955: object name already used
     EXECUTE IMMEDIATE 'DROP TABLE A_TABLE_X';
  END IF;
END;

'Create or replace table' is not possible. As others stated, you can write a procedure and/or use begin execute immediately (...). Because I don't see an answer with how to (re)create the table, I putted a script as an answer.

PS: in line of what jeffrey-kemp mentioned: this beneath script will NOT save data that is already present in the table you are going to drop. Because of the risk of loosing data, at our company it is only allowed to alter existing tables on the production environment, and it is not allowed to drop tables. By using the drop table statement, sooner or later you will get the company police standing at your desk.

--Create the table 'A_TABLE_X', and drop the table in case it already is present
BEGIN
EXECUTE IMMEDIATE 
'
CREATE TABLE A_TABLE_X
(
COLUMN1 NUMBER(15,0),
COLUMN2  VARCHAR2(255 CHAR),
COLUMN3  VARCHAR2(255 CHAR)
)';

EXCEPTION
WHEN OTHERS THEN
  IF SQLCODE != -955 THEN -- ORA-00955: object name already used
     EXECUTE IMMEDIATE 'DROP TABLE A_TABLE_X';
  END IF;
END;
深巷少女 2024-08-01 15:48:51

我会做这样的事情

  begin
     for i in (select table_name from user_tables where table_name = 'FOO') loop
        execute immediate 'drop table '||i.table_name;
     end loop;
  end;

  execute immediate 'CREATE TABLE FOO (id NUMBER,
                                       title VARCHAR2(4000)) ';

I would do something like this

  begin
     for i in (select table_name from user_tables where table_name = 'FOO') loop
        execute immediate 'drop table '||i.table_name;
     end loop;
  end;

  execute immediate 'CREATE TABLE FOO (id NUMBER,
                                       title VARCHAR2(4000)) ';
深海夜未眠 2024-08-01 15:48:51

如果这是针对 MS SQL.. 无论表是否存在,以下代码都将始终运行。

if object_id('mytablename') is not null //has the table been created already in the db
Begin
     drop table mytablename
End

Create table mytablename (...

If this is for MS SQL.. The following code will always run no matter what if the table exist already or not.

if object_id('mytablename') is not null //has the table been created already in the db
Begin
     drop table mytablename
End

Create table mytablename (...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文