Why is the following query not running in my db2? - db2

I want to run the following query in by db2:
CALL spDrpObj('DE','T') ;
CREATE TABLE QTEMP.DE
(
Ano int
);
CREATE OR REPLACE VARIABLE g int;
set g=1;
Begin
WHILE(g<=100) DO
INSERT INTO QTEMP.DE
SELECT g
SET g=g+1;
END WHILE;
But it's showing me error like:-
SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token G was not valid. Valid tokens: , FROM INTO. Cause . . . . . :   A syntax error was detected at token G.  Token G is not a valid token.  A partial list of valid tokens is , FROM INTO.  This list assumes that the statement is correct up to the token.  The error may be earlier in the statement, but the syntax of the statement appears to be valid up to this point. Recovery  . . . :   Do one or more of the following and try the request again: -- Verify the SQL statement in the area of the token G. Correct the statement.  The error could be a missing comma or quotation mark, it could be a misspelled word, or it could be related to the order of clauses. -- If the error token is , correct the SQL statement because it does not end with a valid clause.

Related

Dynamic cursor variable in DB2

I want to open a dynamic cursor variable.
CREATE OR REPLACE PROCEDURE TTT ()
P1: BEGIN
Declare cID char(5) ;
declare c1 cursor for s1 ;
declare stmt varchar(1000) ;
set cid = 'a' ;
Set stmt = 'select * from aaa where a = ?' ;
prePare s1 from stmt ;
open c1 using cid ;
END P1
I get error :
A.TTT - Deploy started.
Create stored procedure returns SQLCODE: -104, SQLSTATE: 42601.
A.TTT: 6: An unexpected token "" was found following "". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.69.56
An unexpected token "" was found following "". Expected tokens may include: "".. SQLCODE=-104, SQLSTATE=42601, DRIVER=3.69.56
A.TTT - Deploy failed.
A.TTT - Roll back completed successfully.
The cause of the error is that the code ignores the documented rules for the order of statements in a compound-SQL block.
One of the rules is that the cursor declaration(s) must appear after all other declared variables and before the start of the SQL PL code block.
So in your question, the order of the variables should be:
Declare cID char(5) ;
declare stmt varchar(1000) ;
declare c1 cursor for s1 ;
For Db2-LUW at current version these rules for compound SQL blocks are here.
To return a resultSet to the caller or the client, remember to add with return to caller or with return to client in the declare c1 cursor line, along with adding dynamic result sets 1 in the procedure definition.
To consume the cursor (i.e. to fetch from it) inside the stored procedure code then you do not need these things.
Your jdbc driver is very old, so plan to upgrade it to a better supported version. You can download the latest versions via this site https://www.ibm.com/support/pages/db2-jdbc-driver-versions-and-downloads
You can also make the error messages more verbose by appending an additional attribute to the connection string URL ;retrieveMessagesFromServerOnGetMessage=true;
This can speed up your ability to solve trivial programming problems when learning.

Get Last 2 characters from column in DB2 AS400

I have a simple select query which is inside an openquery
SELECT
MyColumn,
RIGHT(MyColumn,2)
FROM MyTable
however this gives error
Cause . . . . . : A syntax error was detected at token ,2. Token ,2
is not a valid token. A partial list of valid tokens is ) ,.
In case someone has this issue.
RIGHT(MyColumn,2) gives token error
RIGHT(MyColumn, 2) is correct
Outcome of the actual query
SELECT * FROM OPENQUERY(linkServer, 'SELECT RIGHT(MyColumn, 2) FROM MyTable')

REGEXP_LIKE in DB2 v7r1 (7.1?)

From,
https://www.ibm.com/support/knowledgecenter/en/ssw_ibm_i_71/db2/rbafzregexp_like.htm
SELECT PID FROM PRODUCT
WHERE NOT REGEXP_LIKE(pid,'[0-9]{3}-[0-9]{3}-[0-9]{2}')
But when I run a similar command,
SELECT MYCOLUMN FROM MYTABLE
WHERE NOT REGEXP_LIKE(MYCOLUMN,'[0-9]{3}-[0-9]{3}-[0-9]{2}')
I get
SQL State: 42601 Vendor Code: -104 Message: [SQL0104] Token
was not valid. Valid tokens: < > = <> <= !< !> !=
= ¬< ¬> ¬= IN. Cause . . . . . : A syntax error was detected at token . Token is not a valid
token. A partial list of valid tokens is < > = <> <= !< !> != >= ¬<
¬> ¬= IN. This list assumes that the statement is correct up to the
token. The error may be earlier in the statement, but the syntax of
the statement appears to be valid up to this point. Recovery . . . :
Do one or more of the following and try the request again: -- Verify
the SQL statement in the area of the token .
Correct the statement. The error could be a missing comma or
quotation mark, it could be a misspelled word, or it could be related
to the order of clauses.
-- If the error token is , correct the SQL statement because it does not end with a valid clause.
Is there any reason why this example does not work? Is IBM v7r1 not the same as 7.1?
7.1 and v7r1 are the same thing...
The regular expression support was added as part of technology refresh (TR) level 9
From a command line, use the Work with PTF Groups (WRKPTFGRP) command
WRKPTFGRP PTFGRP(SF99707) PTFGRPLVL(*INSTALLED)
Also, you need to have 5770-SS1 Option #39 International Components for Unicode insallted. You can use the Display Software Resources (DSPSFWRSC) to check for that.
If that's not installed, you'd see a message:
SQL0204 - QQQSVREG in QSYS type *SRVPGM not found.

How to store the result of a select query into a variable(IBM DB2)?

I am trying to save the result of a query into a variable. I am using IBM DB2, but I can only store the result if I am declaring the variable inside a procedure.
My code is:
DECLARE #myvar INTEGER;
SET #myvar = (SELECT MAX(ID) FROM S0SCSQMS.S0SRPTCNAME);
and I receive the following errors:
For the first line:"SQL0104N An unexpected token "INTEGER" was found following "DECLARE #myvar ". Expected tokens may include: "END-OF-STATEMENT". LINE NUMBER=1. SQLSTATE=42601"
The error code does not tell me much. I looked for it on the IBM documentation.
Looking forward for an answer.
Thank you.
try this (work on iseries db2 v7r1)
CREATE OR REPLACE VARIABLE myvar INTEGER ;
SET myvar = (SELECT max( id_xp_dossier) FROM cilgprod.xp_dossier);
DROP VARIABLE myvar;

Why am I getting a syntax error when calling my stored procedure?

I am trying to call a stored procedure with Time variable as in parameter. But whenever i try to call the procedure i m getting error as:
db2 'call PASS_FAIL_CHECKDATE('2014-01-21','13:42:25','CSS1',Null,'4500096651','10',Null)'
SQL0104N An unexpected token ":42" was found following "CKDATE(2014-01-21,
13". Expected tokens may include: "+". SQLSTATE=42601
My Procedures input parameter are :
PASS_fail_checkdate (in post_date date,in post_time time,in destplant varchar(4), in destloc varchar(4), in transnum varchar(10), in translineitemnum varchar(6), in inboundconsignment varchar(35))
I am not sure if my declaration for time variable is correct in procedure or if i am calling the time variable correctly in the procedure.
Please give me suggestions on the same.
As already offered, in words, try the following example as a revision to what was noted in the OP as tried already but failing; i.e. change to use double-quote vs the apostrophe, specified as the delimiter for the DB2 SQL statement string:
db2 "call PASS_FAIL_CHECKDATE('2014-01-21','13:42:25','CSS1',Null,'4500096651','10',Null)"