Problem on creating postgres stored procedure | Postgres 12 - sql

I'm kinda new on Postgres, so I need your help for this one.
postgres=# select version();
version
------------------------------------------------------------
PostgreSQL 12.3, compiled by Visual C++ build 1914, 64-bit
(1 row)
1. CREATE PROCEDURE test (INT,varchar(200))
2. LANGUAGE plpgsql
3. AS $$
4. BEGIN
5.
6. create table test1 as
7. select id,name from mst_user_mobile limit 5
8.
9. COMMIT;
10. END;
11. $$;
From what I read, Postgres version above 10, support PROCEDURE method. But when I execute the code it always error on line 1 (on word PROCEDURE)
here error that i got:
ERROR: syntax error at or near "PROCEDURE"
LINE 1: CREATE PROCEDURE test (INT,varchar(200))
^
SQL state: 42601
Character: 8
any helps are welcome

You report to be using Postgres 12.3. Yet, the reported error message is exactly what I see in Postgres 10 (or older):
ERROR: syntax error at or near "PROCEDURE"
LINE 1: CREATE PROCEDURE test (INT,varchar(200))
db<>fiddle here
CREATE PROCEDURE was introduced with Postgres 11.
I suspect you are connected to the wrong / a different database.
All that aside, your example could just be a function. The COMMIT is pointless as last command. See:
In PostgreSQL, what is the difference between a “Stored Procedure” and other types of functions?
Or, it's another missing semicolon before the CREATE PROCEDURE command. (You show another one of those in the question.) I can reproduce the error message this way, too:
db<>fiddle here

I guess the problem are the ; on END and the last $$. You shouldn't use them in this cases!

Related

How to use SET OPTION within a DB2 stored procedure

I read (and tried) that I cannot use WITH UR in DB2 stored procedures. I am told that I can use SET OPTION to achieve the same. However, when I implement it in my stored procedure, it fails to compile (I moved around its location same error). My questions are:
Can I really not use WITH UR after my SELECT statements within a procedure?
Why is my stored procedure failing to compile with the below error
message?
Here is a simplified version of my code:
CREATE OR REPLACE PROCEDURE MySchema.MySampleProcedure()
DYNAMIC RESULT SETS 1
LANGUAGE SQL
SET OPTION COMMIT=*CHG
BEGIN
DECLARE GLOBAL TEMPORARY TABLE TEMP_TABLE AS (
SELECT 'testValue' as "Col Name"
) WITH DATA
BEGIN
DECLARE exitCursor CURSOR WITH RETURN FOR
SELECT *
FROM SESSION.TEMP_TABLE;
OPEN exitCursor;
END;
END
#
Error Message:
SQL0104N An unexpected token "SET OPTION COMMIT=*CHG" was found
following " LANGUAGE SQL
Here is code/error when I use WITH UR
CREATE OR REPLACE PROCEDURE MySchema.MySampleProcedure()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
--#SET TERMINATOR #
BEGIN
DECLARE GLOBAL TEMPORARY TABLE TEMP_TABLE AS (
SELECT UTI AS "Trade ID" FROM XYZ WITH UR
) WITH DATA;
BEGIN
DECLARE exitCursor CURSOR WITH RETURN FOR
SELECT *
FROM SESSION.TEMP_TABLE;
OPEN exitCursor;
END;
END
#
line 9 is where the DECLARE GLOBAL TEMPORARY ... is
DB21034E The command was processed as an SQL statement because it was
not a valid Command Line Processor command. During SQL processing it
returned: SQL0109N The statement or command was not processed because
the following clause is not supported in the context where it is
used: "WITH ISOLATION USE AND KEEP". LINE NUMBER=9. SQLSTATE=42601
Specifying the isolation level:
For static SQL:
If an isolation-clause is specified in the statement, the value of that clause is used.
If an isolation-clause is not specified in the statement, the isolation level that was specified for the package when the package was bound to the database is used.
You need to bind the routine package with UR, since your DECLARE GTT statement is static. Before CREATE OR REPLACE use the following in the same session:
CALL SET_ROUTINE_OPTS('ISOLATION UR')
P.S.: If you want to run your routine not only 1 time in the same session without an error, use additional WITH REPLACE option of DECLARE.
If your Db2 server runs on Linux/Unix/Windows (Db2-LUW), then there is no such statement as SET OPTION COMMIT=*CHG , and so Db2 will throw an exception for that invalid syntax.
It is important to only use the matching Db2 Knowledge Centre for your Db2 platform and your Db2-version. Don't use Db2-Z/OS documentation for Db2-LUW development. The syntax and functionalities differ per platform and per version.
A Db2-LUW SQL PL procedure can use with ur in its internal queries, and if you are getting an error then something else is wrong. You have to use with ur in the correct syntax however, i.e in a statement that supports this clause. For your example you get the error because the clause does not appear to be valid in the depicted context. You can achieve the desired result in other ways, one of them being to populate the table in a separate statement from the declaration (e.g insert into session.temp_table("Trade ID") select uti from xyz with ur; ) and other ways are also possible.
One reason to use the online Db2 Knowledge Cenbtre documentation is that it includes sample programs, including sample SQL PL procedures, which are also available in source code form in the sample directory of your DB2-LUW server, in addition to being available on github. It is wise to study these, and get them working for you.

oracle sql developer first time user

I am new to plsql and trying to use oracle sql developer, I try to run a simple procedure with dbms output line and i get the following error,
ora-00904
, the code is
create or replace PROCEDURE proc_101 IS
v_string_tx VARCHAR2(256) := 'Hello World';
BEGIN
dbms_output.put_line(v_string_tx);
END;
whether i click the run(green colour) or debug(red colour) i get the same error.
You can see from the above code, procedure doesn't access any objects but still i get the same error.
Your procedure is fine. You may not have permissions to be able to Create a Procedure. If this is the case test your procedure/code without actually Creating it in the Database first. For example, when I'm testing code in my Production database my oracle user cannot Create Procedures, Packages, Tables etc... And so I test my Procedures within my Own PL/SQL Blocks. When the code is good to go I can get a database administrator to Create the Procedures and/or Packages for me.
The below screenshot is code that simply tests the Procedure:
The below screenshot is code that does much more and tests the Procedure from within a PL/SQL Block
For more advanced situations this allows you to do so much more as you can create all sorts of Procedures/Functions and/or Cursors and test them immediately without needing to CREATE these objects in your Oracle Database.
I'd say that there's some other code in the worksheet which raises that error, not just the CREATE PROCEDURE you posted. For example, something like this SQL*Plus example (just to show what's going on - you'd get the same result in SQL Developer):
SQL> select pixie from dual;
select pixie from dual
*
ERROR at line 1:
ORA-00904: "PIXIE": invalid identifier
SQL>
SQL> create or replace PROCEDURE proc_101 IS
2 v_string_tx VARCHAR2(256) := 'Hello World';
3 BEGIN
4 dbms_output.put_line(v_string_tx);
5 END;
6 /
Procedure created.
SQL>
See? The first part raised ORA-00904 as there's no PIXIE column in DUAL, while the procedure is created correctly.
So - remove code which fails and everything should be OK.
Check with your DBA to make sure the dbms_output package has been installed on your database, and that you have permissions on it.

How to "select" inside a stored procedure or a UDF in DB2 LUW?

I believe this question is very trivial. I' unable to select rows inside a stored procedure that I defined, nor inside a UDF. Here is the syntax that I used:
Create Or Replace Procedure GenerateSequence(
In InternalUnitID SmallInt,
In ObjectTypeID SmallInt)
Language SQL
Dynamic Result Sets 1
Not Deterministic
No External Action
Modifies SQL Data
Commit On Return Yes
Begin
Select Number
From Sequence
Where InternalUnit=InternalUnitID
And ObjectType=ObjectTypeID;
End
If I try to create the above procedure (by putting the definition in a SQL file and running it). I get the following error message:
DB21034E The command was processed as an SQL statement because it was not a valid Command Line Processor command. During SQL processing it returned:
SQL0104N An unexpected token "Select Number From Sequence Where Intern" was
found following "n Return Yes Begin ". Expected tokens may include: "".
LINE NUMBER=21. SQLSTATE=42601
Any clue what can be the cause of this problem?
My environment is DB2 10.5 Express on Windows.
My problem was that I needed to use a cursor in order to return the result set to the caller of the stored procedure.
References:
CREATE PROCEDURE (SQL) statement
Compound SQL (compiled) statement

Table,View Or Sequence reference 'SEQUENCE.NEXTVAL' not allowed in this context

I'm having this error while trying to compile the following trigger :
CREATE OR REPLACE TRIGGER INCREMENTER_ID_CONSISTANCE
BEFORE INSERT ON BD.CONSISTANCE
for each row
BEGIN
:new.code := ID_CONSISTANCE.nextval;
END;
**ERROR** : Table,View Or Sequence reference 'ID_CONSISTANCE.nextval' not allowed in
this context
What is the problem here ? and how can I fix this ?
This syntax is only allowed in Oracle 11 or later.
The (unsupported and outdated) 9i version did not support direct assignment of a sequence value.
You need to use a select into instead:
select ID_CONSISTANCE.nextval
into :new.code
FROM dual;
And you should really plan an upgrade to a current version of Oracle (11.x or 12.x)

Oracle 10gR2 trigger error

I have Oracle 10gR2. I am trying to create autoincrement trigger.
Here is the sample:
CREATE SEQUENCE TEST_SEQ
INCREMENT BY 1
START WITH 1
NOMAXVALUE
/
CREATE TABLE TESTER (
ID_TESTER INTEGER NOT NULL,
VAL VARCHAR2(20) NOT NULL
)
/
CREATE OR REPLACE TRIGGER TIB_TESTER BEFORE INSERT
ON TESTER FOR EACH ROW
BEGIN
SELECT TEST_SEQ.NEXTVAL
INTO :NEW.ID_TESTER
FROM DUAL;
END;
/
Trigger creation gives warning:
warning : ORA-24344: success with
compilation error
And when I get error value:
select OCI_SUCCESS_WITH_INFO;
/
It gives error:
error : ORA-00923: FROM keyword not
found where expected
What client are you using to issue these commands? ORA-24344 is a perculiar error.
In SQL*PLus we can get more information about compilation errors like this:
SQL> show errors
As for the ORA-00923 error, that is because in Oracle's version of SQL we always have to select from a table. So you should execute
select OCI_SUCCESS_WITH_INFO
from dual
/
I'm not sure how much sense that makes, but at least you won't get the error.
"It was Navicat problem"
That doesn't surprise me, as I ran your code against my database and it built without a hitch.
Maybe it will be useful for somebody:
If you are using Oracle 10g and OCI driver, ORA-24344 shows when trigger have carriage return sign (\r) in code eg. file was created with Windows end of line style.