Select query in FOR loop does not works in Netezza - plpgsql

I am trying to run a simple plsql in Netezza, to run a loop on a result set of select query,
here is the below code I am trying to run
CREATE OR REPLACE PROCEDURE UPDATE_SUSPECT_GROUP_ID()
RETURNS CHARACTER VARYING(ANY) EXECUTE AS CALLER
LANGUAGE NZPLSQL AS
BEGIN_PROC
DECLARE
VAL BIGINT := 100000000000000;
BEGIN
RAISE NOTICE 'lets start';
FOR r IN
select
suspect_id as suspect_id
from apollo_customer_analysis.ASHKUMAR.REFINED_SUSPECT
LOOP
RAISE NOTICE 'val: %',r.suspect_id;
END LOOP;
END;
END_PROC;
EXECUTE UPDATE_SUSPECT_GROUP_ID();
When try to run the code, I get the below error
ERROR [01000] NOTICE: plpgsql: ERROR during compile of UPDATE_SUSPECT_GROUP_ID near line 15
ERROR [01000] NOTICE: line 1 at execute statement
ERROR [HY000] ERROR: syntax error, unexpected ';', expecting LOOP at or near ";"
Can someone help me how this error could be resolved?

Related

Why am I getting an error near semi colon (:)?

I have no idea but this is throwing an error i think it is on line "set #al = 2020". I have read the documentation and not seeming to translate over.
begin
declare #al int;
set #al = 2020;
exec dbo.get_egus_trdg_desk_rpt_no_clrg_broker(#al int);
end
Error message:
SQL Error [102] [42000]: Incorrect syntax near ';'.
You do not need to use ';' after every line of code. I can successfully generate the following script in SQL Server 2019.
Sample Output

Is that able to output error messages to a file in psql interactive mode?

I'm writing my own regression test, and I'm using \o command to output my query result to a temporary file. For example
create or replace function aaaa()
returns integer as
$$
BEGIN
RAISE NOTICE 'ssss';
RETURN 1;
END;
$$ language plpgsql;
\o a.tmp
select aaaa();
With those commands, I'm able to output the return value (1 in this case) to file a.tmp. However, the message NOTICE: ssss is print directly to my psql client.
I'm wondering if there is a way to print the NOTICE: ssss also to a.tmp.

showing some values generated from the body of a psql function as done with console.log in ES2016

In ES2016 we can use console.log to print some values while we execute a function. Is there something similar for psql functions?
Never heard of that thing called ES2016, but I assume that you are really talking about PL/pgSQL functions in PostgreSQL. psql is the command line client for that database.
To print output to the console in PL/pgSQL code, use RAISE NOTICE:
DO
$$BEGIN
RAISE NOTICE '%, do you hear me?', current_user;
END;$$;

Keywords that only exist in PL/pgSQL functions and DO blocks?

Can we use SQL to find out which keywords only mean something to Postgres inside a DO block or a PL/pgSQL function? And if not, can somebody perhaps tell me whether my list is complete or if there are words that shouldn't be on this list:
continue, exit, foreach, loop, return, return next, return query,
slice, while, alias, begin, constant, declare, exception, execute, get
(stacked) diagnostics, perform, raise, message, detail, hint, errcode,
debug, log, info, notice, warning, found, sqlerrm, sqlstate, new, old,
tg_name, tg_when, tg_level, tg_op, tg_relid, tg_relname,
tg_table_name, tg_table_schema, tg_nargs, tg_argv, tg_event, tg_tag
You can find the list in src/pl/plpgsql/src/pl_scanner.c:
Reserved keywords:
ALL
BEGIN
BY
CASE
DECLARE
ELSE
END
EXECUTE
FOR
FOREACH
FROM
IF
IN
INTO
LOOP
NOT
NULL
OR
STRICT
THEN
TO
USING
WHEN
WHILE
“Non-reserved” keywords:
ABSOLUTE
ALIAS
ARRAY
ASSERT
BACKWARD
CLOSE
COLLATE
COLUMN
COLUMN_NAME
CONSTANT
CONSTRAINT
CONSTRAINT_NAME
CONTINUE
CURRENT
CURSOR
DATATYPE
DEBUG
DEFAULT
DETAIL
DIAGNOSTICS
DUMP
ELSEIF
ELSIF
ERRCODE
ERROR
EXCEPTION
EXIT
FETCH
FIRST
FORWARD
GET
HINT
IMPORT
INFO
INSERT
IS
LAST
LOG
MESSAGE
MESSAGE_TEXT
MOVE
NEXT
NO
NOTICE
OPEN
OPTION
PERFORM
PG_CONTEXT
PG_DATATYPE_NAME
PG_EXCEPTION_CONTEXT
PG_EXCEPTION_DETAIL
PG_EXCEPTION_HINT
PRINT_STRICT_PARAMS
PRIOR
QUERY
RAISE
RELATIVE
RESULT_OID
RETURN
RETURNED_SQLSTATE
REVERSE
ROW_COUNT
ROWTYPE
SCHEMA
SCHEMA_NAME
SCROLL
SLICE
SQLSTATE
STACKED
TABLE
TABLE_NAME
TYPE
USE_COLUMN
USE_VARIABLE
VARIABLE_CONFLICT
WARNING
Special variables like TG_RELID or FOUND are not keywords, even though they play a special role in PL/pgSQL.

DDL Create Trigger script fails

A DDL script to create a trigger (source below) fails with 2 errors:
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Unexpected end of command - line 3, column 44
After line 0 in file C:\CRMDemo\Database\DDL\Trigger_Orders.sql
Statement failed, SQLSTATE = 42000
Dynamic SQL Error
-SQL error code = -104
-Token unknown - line 1, column 1
-end
At line 14 in file C:\CRMDemo\Database\DDL\Trigger_Orders.sql
(line 3, column 44 looks like it may be the closing parthesis).
I can't find any information about errors 42000 or -104.
The trigger is designed to assign a record number from a generator, which does exist. This trigger works properly in Interbase from the same script.
The only thing I can think of is that the column size, Integer, is incorrect for the value returned. But the documentation says the value may be truncated but should work for the expected value (1).
CREATE TRIGGER ORDERS_GENERATE_KEY FOR ORDERS ACTIVE BEFORE INSERT POSITION 95 AS
begin
NEW.ORDER_NR = GEN_ID(NEW_ORDER_NUMBER, 1);
end;
Firebird is ver 2.5.2, just downloaded. Windows 7. Database should be 32bit.
If you run your statement using isql utility take sure that SET TERM operators are used:
SET TERM ^ ;
CREATE TRIGGER ORDERS_GENERATE_KEY FOR ORDERS
ACTIVE
BEFORE INSERT
POSITION 95
AS
begin
NEW.ORDER_NR = GEN_ID(NEW_ORDER_NUMBER, 1);
end
^
SET TERM ; ^