检查表是否存在函数

发布于 2024-11-30 18:25:46 字数 512 浏览 0 评论 0原文

你能帮我纠正这段代码吗?

`CREATE OR REPLACE FUNCTION TABLE_EXISTS(name VARCHAR(50))
RETURNS BOOLEAN
AS
BEGIN
    DECLARE counttable INTEGER;

    SELECT COUNT(1) INTO counttable FROM USER_TABLES WHERE TABLE_NAME=name;

    if counttable=0 then
    return false
    else
    return true
    end if;
END;
/
IF (TABLE_EXISTS("LEADS_DELETED")) then
DROP TABLE LEADS_DELETED;
end if;
/
CREATE GLOBAL TEMPORARY TABLE LEADS_DELETED
(
    ID NUMBER(19),
    PRIMARY KEY (ID)
) ON COMMIT DELETE ROWS`

Can you help me rectify this block of code plz?

`CREATE OR REPLACE FUNCTION TABLE_EXISTS(name VARCHAR(50))
RETURNS BOOLEAN
AS
BEGIN
    DECLARE counttable INTEGER;

    SELECT COUNT(1) INTO counttable FROM USER_TABLES WHERE TABLE_NAME=name;

    if counttable=0 then
    return false
    else
    return true
    end if;
END;
/
IF (TABLE_EXISTS("LEADS_DELETED")) then
DROP TABLE LEADS_DELETED;
end if;
/
CREATE GLOBAL TEMPORARY TABLE LEADS_DELETED
(
    ID NUMBER(19),
    PRIMARY KEY (ID)
) ON COMMIT DELETE ROWS`

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

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

发布评论

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

评论(2

落墨 2024-12-07 18:25:46

当您想要创建或重新创建表时,可以使用这样的构造(尝试删除并捕获当对象不存在时抛出的 ORA-00942 异常):

DECLARE
   table_does_not_exist exception;
   pragma exception_init(table_does_not_exist, -942);
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
EXCEPTION
   WHEN table_does_not_exist THEN
      NULL;
END;
/

CREATE TABLE ...

You can use a construct like this when you want to create or recreate a table (try to drop and catch the ORA-00942 exception that gets thrown when the object doesn't exist):

DECLARE
   table_does_not_exist exception;
   pragma exception_init(table_does_not_exist, -942);
BEGIN
   EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
EXCEPTION
   WHEN table_does_not_exist THEN
      NULL;
END;
/

CREATE TABLE ...
献世佛 2024-12-07 18:25:46

表名“LEADS_DELETED”周围有双引号,应为“LEADS_DELETED”。

我还会用 UPPER(table_name) 将 table_name 包装在您的查询中。

您还需要将 DROP TABLE 命令放入 EXECUTE IMMEDIATE 包装器中。

您也在 wrog 位置声明变量,它需要在 BEGIN 子句之前声明。

CREATE OR REPLACE 
FUNCTION TABLE_EXISTS(name VARCHAR(50)) 
  RETURNS BOOLEAN 
AS 
   counttable INTEGER; 
BEGIN 
   SELECT COUNT(1) 
     INTO counttable 
     FROM USER_TABLES 
    WHERE TABLE_NAME=UPPER(name);      

    if counttable=0 
    then     
       return false     
    else     
       return true     
    end if; 
END; 
/ 

-- I suggest you use a bind variable instead of the literal table name.
IF TABLE_EXISTS('LEADS_DELETED') 
THEN
  EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
END IF;
/ 

-- Could be
IF table_exists(v_table_name)
THEN
   EXECUTE IMMEDIATE 'DROP TABLE :tablename'
   USING v_table_name;
END IF;

You have double quotes aroung your table name "LEADS_DELETED" should be 'LEADS_DELETED'.

I'd also wrap the table_name in your query with UPPER(table_name) too.

You also need to put the DROP TABLE command inside an EXECUTE IMMEDIATE wrapper.

You declare your variable in the wrog place too, it needs to be declared before the BEGIN clause.

CREATE OR REPLACE 
FUNCTION TABLE_EXISTS(name VARCHAR(50)) 
  RETURNS BOOLEAN 
AS 
   counttable INTEGER; 
BEGIN 
   SELECT COUNT(1) 
     INTO counttable 
     FROM USER_TABLES 
    WHERE TABLE_NAME=UPPER(name);      

    if counttable=0 
    then     
       return false     
    else     
       return true     
    end if; 
END; 
/ 

-- I suggest you use a bind variable instead of the literal table name.
IF TABLE_EXISTS('LEADS_DELETED') 
THEN
  EXECUTE IMMEDIATE 'DROP TABLE LEADS_DELETED';
END IF;
/ 

-- Could be
IF table_exists(v_table_name)
THEN
   EXECUTE IMMEDIATE 'DROP TABLE :tablename'
   USING v_table_name;
END IF;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文