Teradata - 作为参数传递的运行查询

时间:2018-03-02 08:28:07

标签: sql teradata query-parameters named-parameters

我正在尝试学习Teradata并尝试简化将数据从生产数据库复制到测试数据库进行测试的方式。

对于这个过程,我们需要填写以下详细信息并发送给我们的TDBA: -

  • 产品表DbName
  • 产品表名称
  • 产品表Perm_Size
  • 产品表GB尺寸
  • 产品记录计数
  • 过滤SQL查询以从Prod DB获取数据
  • 过滤记录计数

我试图创建一个简单的SQL实用程序,它接受上面列表中的参数( Italics和Bold )并输出剩余的字段。

我有点开始但却陷入了创建和运行作为参数传递的查询的困境。我还尝试使用'?dbName'等参数在运行时接受值。但是也无法解决这个问题。任何指导都会很棒。

WITH ParamInp(dbName, tblName, fltrQry) AS
(SELECT 'PRDDB', 'EMPL', 'SELECT * FROM PRDVIEWS.EMPL WHERE ID IN (1,2,3)') -- We have select access only on PRDVIEWS schema
SELECT 
    Upper(Trim(ParamInp.dbName)) AS DATABASENAME,
    Substr(Upper(Trim(ParamInp.dbName)), 1, Length(Trim(ParamInp.dbName))-2) || 'VIEWS' AS VIEWNAME, -- Creating View DB schema name
    Upper(Trim(ParamInp.tblName)) AS TABLENAME,
    fltrQry AS FILTER_QUERY,  -- do not want to execute fltrQry here. It is only to include in the excel
    Sum(currentperm) AS PERM_SIZE,
    Sum(currentperm)/1024**3 AS TOTAL_SIZE, -- GigaByte
    (SELECT Cast(Count(*) AS BIGINT) FROM (Substr(Upper(Trim(dbName)), 1, Length(Trim(dbName))-2) || 'VIEWS').Upper(Trim(tblName)) ) 
        AS TOTAL_COUNT,     -- Unable to get this working
    (SELECT Cast(Count(*) AS BIGINT) FROM (ParamInp.fltrQry)) 
        AS FILTERED_COUNT   -- This is where fltrQry should run
FROM dbc.allspace, ParamInp 
WHERE TABLENAME = ParamInp.tblName   
AND databasename = ParamInp.dbName   
GROUP BY 1,2,3
ORDER BY 1,2;

我想我无法在一个查询中执行此操作。在那种情况下,我应该如何处理这个问题。我在Teradata SQL助手中运行查询,有时会获取要从生产中加载的表的列表。

我希望输出为

DATABASENAME  | VIEWNAME  | TABLENAME  | FILTER_QUERY                       | PERM_SIZE  | TOTAL_SIZE  | TOTAL_COUNT  | FILTERED_COUNT  |
--------------------------------------------------------------------------------------------------------------------------
PRDDB         | PRDVIEWS  | EMPL       | SELECT * FROM PRDVIEWS.EMPL WHERE ID IN (1,2,3)  | 1111111    | 2.2         | 333333       | 444             |

1 个答案:

答案 0 :(得分:0)

除了存储过程之外,您可以在bteq中使用两步法进行。

  1. 使用select连接所需的sql-command(s),在需要的地方插入参数。 结果将导出到文件
  2. 从文件
  3. 运行这些创建的sql-commands

    我没有测试以下内容,它旨在显示一般的想法。 我确信需要进行一些额外的调整才能使生成的命令的语法正确。

    .logon tdpid/user,pass
    .set format off
    .set titledashes off
    .export file /tmp/myQuery.bteq
    select 'SELECT 
        Upper(Trim('||dbName||')) AS DATABASENAME,
        Substr(Upper(Trim('||dbName||')), 1, Length(Trim('||dbName||'))-2) || 'VIEWS AS VIEWNAME,
        Upper(Trim('||tblName||')) AS TABLENAME,
        '||fltrQry||' AS FILTER_QUERY,
        Sum(currentperm) AS PERM_SIZE,
        Sum(currentperm)/1024**3 AS TOTAL_SIZE,
        (SELECT Cast(Count(*) AS BIGINT) FROM (Substr(Upper(Trim('||dbName||')), 1, Length(Trim('||dbName||'))-2) || 'VIEWS).Upper(Trim('||tblName||')) ) 
            AS TOTAL_COUNT,
        (SELECT Cast(Count(*) AS BIGINT) FROM ('||fltrQry||')) 
            AS FILTERED_COUNT 
    FROM dbc.allspace 
    WHERE TABLENAME = '||tblName||'   
    AND databasename = '||dbName||'   
    GROUP BY 1,2,3
    ORDER BY 1,2;'  (TITLE '')
    from (
    SELECT 'PRDDB' as dbName, 'EMPL' as tblName, 'SELECT * FROM PRDVIEWS.EMPL WHERE ID IN (1,2,3)' as fltrQry
    ) as commands;
    .export reset
    .run file = /tmp/myQuery.bteq