SAS Enterprise Guide 如果存在任何不稳定的表,则删除。

huangapple go评论125阅读模式
英文:

SAS Enterprise Guide Drop Volatile Tables If Any Exist

问题

I'm attempting to create a SAS macro that can drop any/all volatile tables in a current Teradata session.

当前,唯一成功运行的方法是在宏的开头创建一个虚拟的临时表,该表允许使用Teradata中的HELP VOLATILE TABLE选项创建一个包含所有表名的表。

我目前遇到的问题是,如果我删除虚拟表的创建(XXXX),并且我的会话中没有任何临时表,那么HELP VOLATILE TABLE将不返回任何内容,并导致无法创建表。

当不存在临时表时的警告/错误。

警告:DROP、KEEP或RENAME列表中的变量'Table Dictionary Name'n从未被引用过。

错误:PROC SQL要求任何创建的表至少有1列。

是否有一种方法可以避免创建虚拟表的需要(如下所示)?

英文:

I'm attempting to create a SAS macro that can drop any/all volatile tables in a current Teradata session.

Currently the only way to have it run successfully is to create a dummy volatile table at the beginning of the macro that allows the creation of a table containing all table names found using the HELP VOLATILE TABLE option in Teradata.

The current issue I'm running into is if I remove the dummy table creation (XXXX) and I don't have any volatile tables in my session then HELP VOLATILE TABLE doesn't return anything and leads to the inability to create a table.

Warning/Errors when no volatile tables are present.

WARNING: The variable 'Table Dictionary Name'n in the DROP, KEEP, or RENAME list has never been referenced.

ERROR: PROC SQL requires any created table to have at least 1 column.

Is there a way around having the need to create a dummy table (like shown below)?

  1. %macro dropvts(DropALL=N,USE_COMMIT_WORK=N)/parmbuff;
  2. %put syspbuff contains: &syspbuff;
  3. RESET NOPRINT;
  4. EXECUTE(
  5. CREATE VOLATILE TABLE XXXX, LOG (
  6. XXXX CHAR(4)
  7. )
  8. PRIMARY INDEX ( XXXX )
  9. ON COMMIT PRESERVE ROWS) BY TERADATA;
  10. %IF %UPCASE(&USE_COMMIT_WORK)=Y %THEN %DO;
  11. EXECUTE(COMMIT WORK) BY TERADATA;
  12. %END;
  13. CREATE TABLE VTs (RENAME=('Table Dictionary Name'n=TableName) KEEP='Table Dictionary Name'n) AS
  14. SELECT * FROM CONNECTION TO TERADATA (HELP VOLATILE TABLE); /*** CHG PROJ ID 409 PLF ***/
  15. %IF %UPCASE(&DropALL)=N %THEN %DO;
  16. %local parm;
  17. %do parm=1 %to %sysfunc(countw(%sysfunc(compress(&syspbuff))));
  18. %let exists=0;
  19. SELECT 1 INTO :exists FROM VTs
  20. WHERE UPCASE(TableName)="%UPCASE(%SCAN(&syspbuff,&parm))";
  21. %IF &exists=1 %THEN %DO;
  22. EXECUTE(DROP TABLE %SCAN(%sysfunc(compress(&syspbuff)),&parm)) BY TERADATA;
  23. %IF %UPCASE(&USE_COMMIT_WORK)=Y %THEN %DO;
  24. EXECUTE(COMMIT WORK) BY TERADATA;
  25. %END;
  26. %PUT VT %SCAN(&syspbuff,&parm) has been dropped;
  27. %END;
  28. %ELSE %DO;
  29. %PUT VT %SCAN(&syspbuff,&parm) does not exist;
  30. %END;
  31. %end;
  32. CREATE TABLE VTs (RENAME=('Table Dictionary Name'n=TableName) KEEP='Table Dictionary Name'n) AS
  33. SELECT * FROM CONNECTION TO TERADATA (HELP VOLATILE TABLE);
  34. EXECUTE(DROP TABLE XXXX) BY TERADATA;
  35. %IF %UPCASE(&USE_COMMIT_WORK)=Y %THEN %DO;
  36. EXECUTE(COMMIT WORK) BY TERADATA;
  37. %END;
  38. %END;
  39. %ELSE %DO;
  40. SELECT TableName
  41. into :voltable1-:voltable999
  42. FROM VTs;
  43. %local table;
  44. %do table=1 %to &sqlobs;
  45. EXECUTE(DROP TABLE &&voltable&table) BY TERADATA;
  46. %IF %UPCASE(&USE_COMMIT_WORK)=Y %THEN %DO;
  47. EXECUTE(COMMIT WORK) BY TERADATA;
  48. %END;
  49. %end;
  50. %END;
  51. RESET PRINT;
  52. %mend;

Any help is greatly appreciated.

答案1

得分: 2

在这个宏 %TDEXIST() 中(用于测试特定名称的表、视图或临时表是否存在以及它的类型),我首先运行一个单独的查询来检查是否存在任何临时表。

所以,如果您已经连接到 TERADATA 并且想要测试宏变量 TABLE 中指定的表是否存在,您可以使用以下代码:

  1. *----------------------------------------------------------------------------;
  2. * 检查是否存在任何临时表 ;
  3. *----------------------------------------------------------------------------;
  4. %let exists=0;
  5. select '1' into :exists from connection to TERADATA (help volatile table);
  6. %if (&exists) %then %do;
  7. *----------------------------------------------------------------------------;
  8. * 检查这个临时表是否存在 ;
  9. *----------------------------------------------------------------------------;
  10. select 'VOLATILE TABLE' into :result
  11. from connection to TERADATA (help volatile table)
  12. %*----------------------------------------------------------------------------
  13. Set VARNAME based on VALIDVARNAME setting.
  14. -----------------------------------------------------------------------------;
  15. %if %sysfunc(getoption(validvarname))=ANY %then
  16. where upcase('table name'n) = "&table"
  17. ;%else
  18. where upcase(table_name) = "&table"
  19. ;
  20. ;
  21. %let exists=&sqlobs;
  22. %end;
英文:

What I did in this macro %TDEXIST() (which tests if a table, view or volatile table with a specific name exists and what type it is) is run a separate query first to check if there are any volatile tables or not.

So if you have a connection to TERADATA already existing and you want to test if a table with the name in the macro variable TABLE exists you could use this code.

  1. *----------------------------------------------------------------------------;
  2. * Check for any Volatile tables ;
  3. *----------------------------------------------------------------------------;
  4. %let exists=0;
  5. select '1' into :exists from connection to TERADATA (help volatile table) ;
  6. %if (&exists) %then %do;
  7. *----------------------------------------------------------------------------;
  8. * Check if this Volatile table exists ;
  9. *----------------------------------------------------------------------------;
  10. select 'VOLATILE TABLE' into :result
  11. from connection to TERADATA (help volatile table)
  12. %*----------------------------------------------------------------------------
  13. Set VARNAME based on VALIDVARNAME setting.
  14. -----------------------------------------------------------------------------;
  15. %if %sysfunc(getoption(validvarname))=ANY %then
  16. where upcase('table name'n) = "&table"
  17. ;%else
  18. where upcase(table_name) = "&table"
  19. ;
  20. ;
  21. %let exists=&sqlobs;
  22. %end;

huangapple
  • 本文由 发表于 2023年6月1日 05:59:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76377551.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定