check if volatile table exists in teradata

风格不统一 提交于 2020-01-11 06:01:06

问题


Volatile table are great and somewhat comparable to temp tables in sql server (my background). Is there a way to check if a volatile table exists already? This code won't work when it is run for the first time:

DROP TABLE SomeVolatileTable;
CREATE VOLATILE TABLE SomeVolatileTable AS
(
    SELECT 
        TOP 10 *
    FROM  SomeSourceTable
) WITH DATA ON COMMIT PRESERVE ROWS;

In sql server you can check if a temporary table exists:

IF OBJECT_ID('tempdb..#SomeTempTable') IS NOT NULL DROP TABLE #SomeTempTable

Does something similar exist in Teradata?


回答1:


There's no way to check if a specific Volatile Table exists besides HELP VOLATILE TABLE which returns all VT.

But you might create a Stored Procedure like the following:

/*
   Drop a table ignoring 3807 error (Table doesn't exist)
*/

REPLACE PROCEDURE drop_table_if_exists
(
  IN db_name VARCHAR(128) CHARACTER SET Unicode,
  IN tbl_name VARCHAR(128) CHARACTER SET Unicode,
  OUT msg VARCHAR(400) CHARACTER SET Unicode
) SQL SECURITY INVOKER
BEGIN
   DECLARE full_name VARCHAR(361)  CHARACTER SET Unicode;

   DECLARE sql_stmt VARCHAR(500)  CHARACTER SET Unicode;
   DECLARE exit HANDLER FOR SqlException
   BEGIN
      IF SqlCode = 3807 THEN SET msg = full_name || ' doesn''t exist.';
      ELSE
        RESIGNAL;
      END IF;
   END;

   SET full_name = '"' || Coalesce(db_name,DATABASE) || '"."' || tbl_name || '"';

   SET sql_stmt = 'DROP TABLE ' || full_name || ';';

   EXECUTE IMMEDIATE sql_stmt;

   SET msg = full_name || ' dropped.';
END;

It will only ignore the Table doesn't exist error, but still fail on invalid rights, etc.

If you call it with your own user as database it also works for Volatile Tables:

CALL drop_table_if_exists(USER,'SomeVolatileTable', msg);



回答2:


Rather than dropping before you try and create them, drop them later in the script after you are done with them. This will always work, will allow you to rerun without issue and will free up the spool space used by the volatile table.




回答3:


You can use this stored procedure to check if a volatile table exists:

REPLACE PROCEDURE test_db.VOLATILE_EXISTS(
  IN volatile_table_name varchar(30),
  OUT exists_ind integer -- 1 if it exists, else 0
)
SQL SECURITY INVOKER
BEGIN
  DECLARE ignored_condition varchar(100) default '';
  DECLARE table_does_not_exist CONDITION FOR SQLSTATE '42000';
  DECLARE sql_string varchar(100);

  DECLARE CONTINUE HANDLER FOR table_does_not_exist
     SET ignored_condition = 'table does not exist';

  SET exists_ind = 0;
  SET sql_string =  'select 1 from '||volatile_table_name||' where 1=0';
  BEGIN
    DECLARE c1 CURSOR FOR s1;
    PREPARE s1 FROM sql_string;
    OPEN c1;
  END;

  IF ignored_condition = '' THEN
    SET exists_ind = 1;
  END IF;
END;

Be aware that if the VT does not exist and you call this SP within a BEGIN TRANSACTION / END TRANSACTION, the "table does not exist exception", although handled, will cause your transaction to roll back as a side effect. I don't know of a way to prevent that.



来源:https://stackoverflow.com/questions/39769966/check-if-volatile-table-exists-in-teradata

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!