可以在teradata中使用临时表

时间:2016-09-18 12:05:12

标签: sql-server teradata

teradata中是否存在此sql server tsql的等效项:

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

CREATE TABLE #SomeTempTable (Bla NVARCHAR(255));

INSERT INTO #SomeTempTable
SELECT N'a'
    UNION ALL
SELECT N'B'

1 个答案:

答案 0 :(得分:1)

第一个陈述没有等价物,可能会有这样的存储过程:

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 SQLCODE 'T3807'--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;

VOLATILE表仅存在于当前会话中(即,可能在不同会话中使用相同的名称表示不同的表),并在会话断开连接时自动删除。保持命名约定(临时名称以#开头)时,您可能不需要条件Drop(您应该知道您是否已在当前会话中创建此表):

CREATE VOLATILE TABLE #SomeTempTable( 
Bla VARCHAR(255) CHARACTER SET UNICODE)
ON COMMIT PRESERVE ROWS;

注意,如果您没有指定主索引,它将默认为第一列的NUPI。

Select有一个奇怪的限制,当您执行FROM / UNION / INTERSECT等设置操作时需要EXCEPT。解决方法是虚拟视图(类似于Oracle' DUAL表),如下所示:

replace view dummy as select 1 as x;

INSERT INTO #SomeTempTable
SELECT 'a' FROM dummy
    UNION ALL
SELECT 'B' FROM dummy

或类似的CTE:

INSERT INTO #SomeTempTable
WITH dummy AS (select 1 as x)
SELECT 'a' FROM dummy
    UNION ALL
SELECT 'B' FROM dummy