Error while creating procedure in db2 - sql

I got error while creating procedure in db2.
Error is - Expected tokens may include: psm_semicolon.. SQLCODE=-104
Help Me....
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END;

In whatever tool you're using, change the statement terminator to something other than semicolon and put that terminator at the end of the CREATE PROCEDURE statement.
For example, if using the command line processor, save this to a file (note the "#" symbol at the end:
CREATE PROCEDURE update_new()
LANGUAGE SQL
BEGIN
CREATE TABLE TEMP(METADATA_KEY varchar(40),NEW_METADATA_KEY varchar(40));
END#
then execute the file: db2 -td# -f myproc.sql
The reason for doing this is that semicolons are always used as terminators within the procedure code, so you must use something else to terminate the CREATE PROCEDURE statement.

Related

DB2 stored procedure gave syntax errors

I am creating a stored procedure for db2. But it is giving an error saying that
"SQL Error [42601]: An unexpected token "END-OF-STATEMENT" was found
following "SS_TOKEN_BAK". Expected tokens may include: " END IF"..
SQLCODE=-104, SQLSTATE=42601, DRIVER=4.23.42".
Following is my stored procedure.
CREATE OR REPLACE PROCEDURE TOKEN_CLEANUP_SP
BEGIN
DECLARE batchSize INTEGER;
-- ------------------------------------------
-- CONFIGURABLE ATTRIBUTES
-- ------------------------------------------
SET batchSize = 10000; -- SET BATCH SIZE FOR AVOID TABLE LOCKS [DEFAULT : 10000]
-- ------------------------------------------------------
-- BACKUP IDN_OAUTH2_ACCESS_TOKEN TABLE
-- ------------------------------------------------------
IF EXISTS (SELECT TABLE_NAME FROM TABLES WHERE TABLE_NAME = 'IDN_OAUTH2_ACCESS_TOKEN_BAK')
THEN
DROP TABLE IDN_OAUTH2_ACCESS_TOKEN_BAK;
END IF;
END/
Is anyone face this type of issue. Any help on this would be much appreciated.
Verify that you have the end-of-statement delimiter configured correctly for whatever tool submits the 'CREATE OR REPLACE' procedure. Different tools have different ways to configure the block terminator (alternatively known end of statement delimiter). For command line scripts, use --#SET TERMINATOR / at the start of the file, other ways are possible.
Your code sample shows / as the block terminator, so you might want to use that character as the block delimiter. The semi-colon terminates statements inside the block.
Separately you should see that your code won't compile if the specified table does not exist in the implied schema at compilation time, because you are using static SQL. You may want to use dynamic SQL instead for the drop table statement (search for 'EXECUTE IMMEDIATE' examples).

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

How to pass the CHAR as the input to the IDENTIFIED BY inside CREATE USER

I am attempting to create a database user from within a procedure in MariaDB 5.5.40
I can successfully do the following:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
CREATE USER sDbUser#'%';
END;
//
DELIMITER ;
So the procedure has no problems with using the parameterised user name, however, when i try to do the same with the password, like so:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
CREATE USER sDbUser#'%' IDENTIFIED BY PASSWORD PASSWORD(sDbPass);
END;
//
DELIMITER ;
I get the error:
ERROR 1064 (42000) at line 3: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'PASSWORD(sDbPass);
Yes, i have tried to change it to IDENTIFIED BY sDbPass and other variations i could think of.
The GRANT generates the exact same error if i attempt to add IDENTIFIED BY with the variable in it.
The question is: Is it possible, and if so how, to pass the char procedure variable into the IDENTIFIED BY clause inside the CREATE USER.
Ok, i guess that for whatever reason it's impossible. If somebody else will be looking for it, you will have to manually edit the mysql.user (and maybe mysql.db) table:
DROP PROCEDURE IF EXISTS testUserCreate;
DELIMITER //
CREATE PROCEDURE testUserCreate
(IN sDbUser CHAR(16), IN sDbPass CHAR(40))
BEGIN
INSERT INTO mysql.user (Host, User, Password) VALUES('%', sDbUser, PASSWORD(sDbPass));
FLUSH PRIVILEGES;
END;
//
DELIMITER ;

Compound Statement unable to change the delimiter

I am attempting to run a simple compound statement within the Query Editor of DB Solo 4.2.2
It appears I am unable to properly change the end of line delimiter. I am using DB2. Here is a simple example that gives the error:
--#SET TERMINATOR #
BEGIN ATOMIC
DECLARE id INT;
SET id = 10;
END #
--#SET TERMINATOR ;
Error is:
An unexpected token "INT" was found following "N ATOMIC DECLARE id". Expected tokens may include: "END-OF-STATEMENT"
Thanks in advance
DB2 only allows the semicolon to be used as a delimiter in Compound SQL. The syntax you are using appears to only be valid when using the db2batch utility (which comes with DB2 Linux/Unix/Windows).
Here is some relevant information from the Information Center (this is from the z/OS IC):
How to code multiple statements in an SQL procedure
Use a semicolon
character to separate SQL statements within an SQL procedure.
The procedure body has no terminating character. Therefore, if the
procedure contains only one statement, you do not need to put a
semicolon after that statement. If the procedure consists of a set of
nested statements, you do not need to put a semicolon after the
outermost statement.

oracle - run stored procedure from script

I've a script that I am using to build/drop tables and basically setting up the entire schema.
After googling, I still can't figure out how to run a stored procedure.
The script is a .txt file, and I run it using Apex SQL Oracle.
If I write only this line in a script:
execute procedurename(1); --where 1 is paramter.
You have requested to run a script that does not contain any runnable
statements.
SQL>create or replace procedure procedurename(p_num number)
as
begin
null;
end;
/
Procedure created.
SQL>execute procedurename(1);
PL/SQL procedure successfully completed.
everything seems ok on SQLPLUS with oracle 11.
so it must be an apex thing.
Since execute is a sqlplus statement ,try calling the procedure using begin-end PLSQL block in Apex SQL
BEGIN
procedurename(1);
END;
/
save this in a file proc_call.sql and then call it in your script like
#C:\proc_call.sql
where C: is the sample path
For some information refer the below link
https://forums.oracle.com/forums/thread.jspa?threadID=618393