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



这是我的存储过程:

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变量"?

这里有一个类似于您的需求及其工作的示例。我使用了两个表"tab1"one_answers"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;

希望这会有所帮助!

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

看看文档中的示例。

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;

相关内容

  • 没有找到相关文章

最新更新