博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Teradata各种类型Show 语句
阅读量:4208 次
发布时间:2019-05-26

本文共 3093 字,大约阅读时间需要 10 分钟。

根据上一篇中的TableKind类型可以写相应的show table 语句。

Teradata中TableKind与show语句对应
TableKind 类型 show语句
T SET Table show table 表名;
O MULTISET Table show table 表名;
V View show view 视图名;
M Macro show macro 名;
P SQL Procedure show procedure 名;
E External Stored Procedure show procedure 名;
D JAR  
R Table function show function 名;
F Standard function show function 名;
G TRIGGER SHOW TRIGGER trigger_name;
I Join Index SHOW JOIN INDEX join_index_name;
N Hash Index SHOW HASH INDEX hash_index_name;
其他    

下面是一段示例代码,代码实现使用show语句获取各种Table(Object)的DDL(Create语句等)以及对应的Drop语句:

[java] 
  1. /** 
  2.  *  
  3.  * @param TableName : The name of Production DB's table 
  4.  * @param TableKind : The kind of table  
  5.  * @return 
  6.  * @throws SQLException 
  7.  */  
  8. private String showTable(String TableName, String TableKind)   
  9.         throws SQLException {  
  10.     String sqlCREATE = "";  
  11.     PreparedStatement ps = null;  
  12.     ResultSet rs = null;  
  13.     String sql = null;  
  14.     switch(TableKind) {  
  15.     case "T":  
  16.         sql = CommonConfig.sqlShowTable + "\"" + TableName + "\"";  
  17.         this.sqlListDrop.add(CommonConfig.sqlDropTable +   
  18.                 "\"" + TableName + "\"");  
  19.         break;  
  20.     case "V":  
  21.         sql = CommonConfig.sqlShowView + "\"" + TableName + "\"";  
  22.         this.sqlListDrop.add(CommonConfig.sqlDropView +   
  23.                 "\"" + TableName + "\"");  
  24.         break;  
  25.     case "M":  
  26.         sql = CommonConfig.sqlShowMacro + "\"" + TableName + "\"";  
  27.         this.sqlListDrop.add(CommonConfig.sqlDropMacro +   
  28.                 "\"" + TableName + "\"");  
  29.         break;  
  30.     case "P":  
  31.     case "E":  
  32.         sql = CommonConfig.sqlShowProcedure + "\"" + TableName + "\"";  
  33.         this.sqlListDrop.add(CommonConfig.sqlDropProcedure +   
  34.                 "\"" + TableName + "\"");  
  35.         break;  
  36.     case "D":  
  37.         logger.info(" -- TableKind is D, SKIP. -- ");  
  38.         break;  
  39.     case "R":  
  40.     case "F":  
  41.         sql = CommonConfig.sqlShowFunction + "\"" + TableName + "\"";  
  42.         this.sqlListDrop.add(CommonConfig.sqlDropFunction +   
  43.                 "\"" + TableName + "\"");  
  44.         break;  
  45.     default:  
  46.         break;  
  47.     }  
  48.     logger.info(sql);  
  49.     if(sql == nullreturn null;  
  50.     ps = conn.prepareStatement(sql);  
  51.     //logger.info(sql);  
  52.     rs = ps.executeQuery();  
  53.     while(rs.next()) {  
  54.         // Be careful, replace = with +=  
  55.         sqlCREATE += rs.getString(1);  
  56.     }  
  57.       
  58.     // Remove the DB name in the CREATE statement  
  59.     sqlCREATE = sqlCREATE.replace(DBConn.getDatabase() + ".""");  
  60.     sqlCREATE = sqlCREATE.replace(DBConn.getDatabase().toLowerCase() + ".""");  
  61.     sqlCREATE = sqlCREATE.replace(DBConn.getDatabase().toUpperCase() + ".""");  
  62.     sqlCREATE = sqlCREATE.replace("\"" + DBConn.getDatabase() + "\".""");  
  63.     sqlCREATE = sqlCREATE.replace("\"" + DBConn.getDatabase().toLowerCase() + "\".""");  
  64.     sqlCREATE = sqlCREATE.replace("\"" + DBConn.getDatabase().toUpperCase() + "\".""");  
  65.     rs.close();  
  66.     ps.close();  
  67.       
  68.     return sqlCREATE;  
  69. }  

注意:

1. CommonConfig.sqlShowTable是字符串"show table ",其他类似。

2. 最值得注意的情况

[java] 
  1. sqlCREATE += rs.getString(1);  

这句必须使用+=,而不能使用=;因为存在show procedure的结果集(ResultSet)有可能是当作多行返回的。即如下图的情况(使用=时的返回值):

[plain] 
  1.  INFO [main] (DDLTransfer.java:93) - show procedure "wikiproc"  
  2. **************************************  
  3. replace procedure   
  4. **************************************  
  5. wikiproc  
  6. **************************************  
  7. (IN RUNID INTEGER)  
  8. dynamic result sets 1  
  9. main:begin  
  10.   
  11.   declare cur_report cursor with return only for  
  12.   sel *  
  13.   from pct_run r   
  14.   where r.run_id=RUNID;  
  15.   
  16.   open cur_report;  
  17. end;  

每行用一行星号隔开,可见返回了三行。

转载地址:http://xurli.baihongyu.com/

你可能感兴趣的文章
【React Native】Invariant Violation: Application AwesomeProject has not been registered
查看>>
【ReactNative】真机上无法调试 could not connect to development server
查看>>
【XCode 4.6】常用快捷键 特别是格式化代码ctrl+i
查看>>
【iOS游戏开发】icon那点事 之 实际应用(二)
查看>>
【iOS游戏开发】icon那点事 之 图标设计(三)
查看>>
【IOS游戏开发】之测试发布(Distribution)
查看>>
【IOS游戏开发】之IPA破解原理
查看>>
【一天一道LeetCode】#45. Jump Game II
查看>>
【一天一道LeetCode】#46. Permutations
查看>>
【一天一道LeetCode】#47. Permutations II
查看>>
【一天一道LeetCode】#48. Rotate Image
查看>>
【一天一道LeetCode】#56. Merge Intervals
查看>>
【一天一道LeetCode】#57. Insert Interval
查看>>
【一天一道LeetCode】#58. Length of Last Word
查看>>
【一天一道LeetCode】#59. Spiral Matrix II
查看>>
【一天一道LeetCode】#30. Substring with Concatenation of All Words
查看>>
【一天一道LeetCode】#60. Permutation Sequence.
查看>>
【一天一道LeetCode】#113. Path Sum II
查看>>
【一天一道LeetCode】#114. Flatten Binary Tree to Linked List
查看>>
【unix网络编程第三版】阅读笔记(二):套接字编程简介
查看>>