Teradata:如何从数据库表动态创建视图
Teradata: How to create views dynamically from database tables
我使用的是 Teradata BTEQ 15.00 版。我有以下 SQL 代码。动态的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
这是结果,但是"locking row for access"被截断了,所以我在编译时出错SQL。
Replace View VIEWS_TEST.VW_LOCATION As loc
ID
LOC_TYPE_ID
NAME
LATITUDE
LONGITUDE
ADDR1
ADDR2
CITY
STATE
COUNTRY
) From TABLES_TEST.LOCATION ;
可以看到"As Loc"被截断了,所以报错如下:
*** Failure 3707 Syntax error, expected something like a 'SELECT' keyword o
r a 'LOCK' keyword or '(' or a 'TRANSACTIONTIME' keyword between the 'As' key word and the word 'loc'.
Statement# 1, Info =81
*** Total elapsed time was 1 second.
我尝试了不同的方法来让它工作,但都失败了。
有什么建议吗?
.Export Report File = CViews.sql
.set width 200
.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
这是 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
我使用的是 Teradata BTEQ 15.00 版。我有以下 SQL 代码。动态的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
这是结果,但是"locking row for access"被截断了,所以我在编译时出错SQL。
Replace View VIEWS_TEST.VW_LOCATION As loc
ID
LOC_TYPE_ID
NAME
LATITUDE
LONGITUDE
ADDR1
ADDR2
CITY
STATE
COUNTRY
) From TABLES_TEST.LOCATION ;
可以看到"As Loc"被截断了,所以报错如下:
*** Failure 3707 Syntax error, expected something like a 'SELECT' keyword o
r a 'LOCK' keyword or '(' or a 'TRANSACTIONTIME' keyword between the 'As' key word and the word 'loc'.
Statement# 1, Info =81
*** Total elapsed time was 1 second.
我尝试了不同的方法来让它工作,但都失败了。
有什么建议吗?
.Export Report File = CViews.sql
.set width 200
.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
这是 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