如何访问Netezza存储过程中的参数?

时间:2016-04-29 18:09:05

标签: shell stored-procedures netezza nzsql

这是我的存储过程:

nzsql -u user -pw pass -c "CREATE OR REPLACE PROCEDURE INSERT_LOGIC(varchar(50),varchar(20),varchar(40)) RETURNS BOOL LANGUAGE NZPLSQL AS BEGIN_PROC
DECLARE
t1 ALIAS FOR $1;
t2 ALIAS FOR $2;
t3 ALIAS FOR $3;
BEGIN

INSERT INTO ABC..XYZ
(select '$t1','$t2','$t3' from ABC..PQR limit 10);

END;
END_PROC;"

ALIAS FOR是我在互联网上找到的唯一方法,但我收到以下错误:

NOTICE:  plpgsql: ERROR during compile of INSERT_LOGIC near line 3
ERROR:  syntax error, unexpected ERROR, expecting VARIABLE or WORD at or near "t1Stuff"

如何访问三个" varchar变量"我传递给内部存储过程?

2 个答案:

答案 0 :(得分:0)

您尝试通过在名称前放置一个$来引用变量,这是无效的。

查看示例in the docs

DECLARE
  logtxt ALIAS FOR $1;
  curtime timestamp;
BEGIN
  curtime := 'now()';
  INSERT INTO logtable VALUES (logtxt, curtime);
  RETURN curtime;
END

你应该试试

INSERT INTO ABC..XYZ
(select t1, t2, t3 from ABC..PQR limit 10);

虽然以这种方式使用时列值不会解决但可能。如果没有,请构建一个动态语句并执行它。

declare sql varchar;
sql := 'insert into abc..xyz select ' || t1 || ',' || t2 || ',' || t3 || ' from abc..pqr limit 10;'
execute immediate sql;

如果你传递值而不是列名作为参数:

declare sql varchar;
sql := 'insert into abc..xyz select ''' || t1 || ''',''' || t2 || ''',''' || t3 || ''' from abc..pqr limit 10;'
execute immediate sql;

答案 1 :(得分:0)

这是一个类似于您的要求及其工作的示例。我正在使用两个表'tab1'和'tab2',其中包含以下描述:



    $ nzsql -d test -c "\d tab1"
                         Table "TAB1"
     Attribute |     Type      | Modifier | Default Value
    -----------+---------------+----------+---------------
     COL1      | INTEGER       |          |
     COL2      | CHARACTER(10) |          |
     COL3      | INTEGER       |          |
    Distributed on hash: "COL1"

    $ nzsql -d test -c "\d tab2"
                         Table "TAB2"
     Attribute |     Type      | Modifier | Default Value
    -----------+---------------+----------+---------------
     C1        | INTEGER       |          |
     C2        | CHARACTER(10) |          |
     C3        | INTEGER       |          |
    Distributed on hash: "C1"

以下是我使用的存储过程代码:



    CREATE OR REPLACE PROCEDURE INSERT_LOGIC(varchar(50),varchar(20),varchar(40))
       RETURNS BOOL
       LANGUAGE NZPLSQL
       AS
    BEGIN_PROC
      DECLARE
        num_args int4;
        sql char(100);
        t1 ALIAS FOR $1;
        t2 ALIAS FOR $2;
        t3 ALIAS FOR $3;
      BEGIN
        num_args := PROC_ARGUMENT_TYPES.count;
        RAISE NOTICE 'Number of arguments: %', num_args;
        sql := 'INSERT INTO tab2 SELECT ' || t1 || ',' || t2 || ',' || t3 || ' FROM tab1 LIMIT 10 ';
        RAISE NOTICE 'SQL Statement: %', sql;
        EXECUTE IMMEDIATE sql;
      END;
    END_PROC;

希望这会有所帮助!