[英]Teradata stored procedure with dynamic parameters
I'm trying to create a dunamic procedure that, given a specific dbname and tablename, creates a set of select statements using all column names available for that table. 我正在尝试创建一个动态过程,给定特定的dbname和表名,该过程使用该表可用的所有列名创建一组select语句。
I'm using the following code: 我正在使用以下代码:
REPLACE PROCEDURE sum_col
(
IN TABLENAME VARCHAR(50), DBNAME VARCHAR(286)
)
BEGIN
DECLARE SqlTxt VARCHAR(3000);
FOR cur AS
SELECT
TRIM(a.DatabaseName) AS DBNAME,
TRIM(a.TABLENAME) AS TABLENAME,
TRIM(b.ColumnName) AS ColumnName
FROM dbc.Tables a
INNER JOIN dbc.COLUMNS AS b
ON a.DatabaseName=b.DatabaseName AND a.TABLENAME = b.TABLENAME
WHERE a.DatabaseName = :DBNAME
AND a.TABLENAME = :TABLENAME
AND TableKind = 'T'
DO
SET SqlTxt = 'SELECT ' || '''' || TRIM(cur.ColumnName) || '''' || ', CASE
WHEN SUM(CNT) IS NULL THEN 0
ELSE SUM(CNT)
END AS CntNull
FROM (
SELECT 0 AS cnt
FROM ' || TRIM(cur.DBNAME) || '.' || TRIM(cur.TABLENAME) ||' )a ;';
CALL dbc.sysexecsql(:SqlTxt);
END FOR;
END;
But when I call the statement I get this error: CALL Failed. 但是,当我调用该语句时,出现以下错误:调用失败。 [5568] SUM_COL:SQL statement is not supported within a stored procedure.
[5568]存储过程不支持SUM_COL:SQL语句。
How can I fix this? 我怎样才能解决这个问题?
Explanation: This error occurs when in one of the following situations: a. 说明:在下列情况之一时,会发生此错误: An unsupported SQL statement is specified in a stored procedure using the dynamic SQL feature of stored procedures.
使用存储过程的动态SQL功能在存储过程中指定了不受支持的SQL语句。 For instance, DATABASE statement, SET SQL statements, multi statement requests, CREATE USER/DATABASE statement without FROM clause, SELECT - INTO SQL or CALL SQL.
例如,DATABASE语句,SET SQL语句,多语句请求,不带有FROM子句的CREATE USER / DATABASE语句,SELECT-INTO SQL或CALL SQL。 This is a run-time error.
这是运行时错误。
Looks like you have other syntax errors. 看起来您还有其他语法错误。 Declare your cursor, open your cursor, etc.. I think you can solve your goal in an easier way, what exactly are you trying to accomplish?
声明游标,打开游标等。我认为您可以通过一种更轻松的方式解决目标,您到底想实现什么目标?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.