Teradata:如何从数据库表动态创建视图

时间:2015-10-28 03:25:00

标签: sql teradata dynamic-sql

我使用的是Teradata BTEQ 15.00版。我有以下SQL代码。动态SQL几乎就在那里,但formet有点不对。

var arr = [{"id":"123", "name":"Jyotirmoy"}];
var mapObj = {};

for(var  i=0; i < arr.length; i++){
  mapObj['id_' + arr[i].id] = arr[i];
}

console.log(myObj.id_123);

以下是结果,但&#34;锁定行以进行访问&#34;被切断了,所以我在编译SQL时遇到了错误。

.Export Report File = CViews.sql

.Rtitle ''
 .Foldline on
 .Format Off
 .set heading '';
 .set heading off;
 .set UNDERLINE OFF;
 .Omit On 4,5

Select
 CASE When ColNo = 1
 THEN 'Replace View
$view_db_name.VW_'||Tbl.Tablename||' As locking row for access
Select ('
 Else '' END (Title '')
 , Cols.Columnname (Title '')
 , CASE WHEN RevColNo = 1 THEN ')
From $db_name.'||Tbl.Tablename||';'
 Else '' END (Title '')
 , Row_Number () Over(Partition By Tbl.Tablename
 Order By Cols.ColumnId) As ColNo
 , Row_Number () Over(Partition By Tbl.Tablename
 Order By Cols.ColumnId Desc) As RevColNo
 From DBC.Tables Tbl
 Join DBC.Columns Cols
 On Tbl.Databasename = Cols.Databasename
 And Tbl.Tablename = Cols.TableName
 Where Tbl.Databasename = '$db_name'
 And Tbl.Tablekind = 'T'
 Order By Tbl.Tablename, ColNo
 ;
.Export Reset

.Run File CViews.sql

你可以看到&#34; As Loc&#34;被切断,所以我有以下错误:

Replace View  VIEWS_TEST.VW_LOCATION                   As loc
   ID
   LOC_TYPE_ID
   NAME
   LATITUDE
   LONGITUDE
   ADDR1
   ADDR2
   CITY
   STATE
   COUNTRY

   )  From TABLES_TEST.LOCATION                  ;

我尝试了不同的方法尝试使其工作,但失败了。

有什么建议吗?

2 个答案:

答案 0 :(得分:1)

{{1}}

答案 1 :(得分:0)

这是TD版本15.00的工作版本。  我添加了缺少“,”并更改为dbc.tablesV和dbc.ColumnsV。

.os rm CViews.sql
.Export Report File = CViews.sql
.set width 300
.Rtitle ''
 .Foldline on
 .Format Off
 .set heading '';
 .set heading off;
 .set UNDERLINE OFF;
 .Omit On 4,5
Select
 CASE When ColNo = 1
 THEN 'Replace View
$view_db_name.VW_'||Tbl.Tablename||' As locking row for access
Select '
 Else ', ' END (Title '')
 , Cols.Columnname (Title '')
 , CASE WHEN RevColNo = 1 THEN '
From $db_name.'||Tbl.Tablename||';'
 Else '' END (Title '')
 , Row_Number () Over(Partition By Tbl.Tablename
 Order By Cols.ColumnId) As ColNo
 , Row_Number () Over(Partition By Tbl.Tablename
 Order By Cols.ColumnId Desc) As RevColNo
 From DBC.TablesV Tbl
 Join DBC.ColumnsV Cols
   On Tbl.Databasename = Cols.Databasename
  And Tbl.Tablename = Cols.TableName
 Where Tbl.Databasename = '$db_name'
   And Tbl.Tablekind = 'T'
   and Tbl.Tablename not like 'WRK_%'
   and Tbl.Tablename not like 'ZZ_%'
 Order By Tbl.Tablename, ColNo
 ;
.Export Reset

.Run File CViews.sql