473,395 Members | 1,936 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

whats wrong with my cursor?

21
Hi all,

I have a cursor declared as:
DECLARE dispCSR CURSOR FOR
SELECT JOB_I
FROM ALI.TDSP_JOB_XRF
WHERE ORD_I = :wsOrdI
AND DSP_NBR = 1
WITH UR;

but i keep getting the error:
SQL0104N An unexpected token ":" was found following "WHERE ORD_I =". Expected tokens may include: "<space>". LINE NUMBER=33. SQLSTATE=42601

but if i do remove the ":", i get the error: "WSORDI" is not valid in the context where it is used. LINE NUMBER=30. SQLSTATE=42703

how do i fix this?
May 3 '07 #1
11 3351
frozenmist
179 Expert 100+
Hi all,

I have a cursor declared as:
DECLARE dispCSR CURSOR FOR
SELECT JOB_I
FROM ALI.TDSP_JOB_XRF
WHERE ORD_I = :wsOrdI
AND DSP_NBR = 1
WITH UR;

but i keep getting the error:
SQL0104N An unexpected token ":" was found following "WHERE ORD_I =". Expected tokens may include: "<space>". LINE NUMBER=33. SQLSTATE=42601

but if i do remove the ":", i get the error: "WSORDI" is not valid in the context where it is used. LINE NUMBER=30. SQLSTATE=42703

how do i fix this?
Hi,

I understand u are using a dynamic cursor.
I need some more info...
Is WSORDI a variable?
Cheers
May 3 '07 #2
cuddles
21
yes, WSORDI is an INTEGER variable. The cursor will fetch data depending on the value of WSORDI (this will be form user input)

thanks!

Hi,

I understand u are using a dynamic cursor.
I need some more info...
Is WSORDI a variable?
Cheers
May 4 '07 #3
frozenmist
179 Expert 100+
Hi,
yes, WSORDI is an INTEGER variable. The cursor will fetch data depending on the value of WSORDI (this will be form user input)

thanks!
I think you have used the oracle syntax.
You can have a dynamic statement in the cursor declaration
The code block can be like:

declare v_variable integer;
declare v_sql varchar(500);

declare v_cursor cursor for v_sql_stmt;

set v_sql='Select * from table where col1 = ?';

Prepare v_sql_stmt from v_sql;
open v_cursor using v_variable;

fetch loop.....
close v_cursor;

Hope this post was helpful..
Cheers
May 4 '07 #4
cuddles
21
will this work for multiple variables in the where clause as well?


Hi,

I think you have used the oracle syntax.
You can have a dynamic statement in the cursor declaration
The code block can be like:

declare v_variable integer;
declare v_sql varchar(500);

declare v_cursor cursor for v_sql_stmt;

set v_sql='Select * from table where col1 = ?';

Prepare v_sql_stmt from v_sql;
open v_cursor using v_variable;

fetch loop.....
close v_cursor;

Hope this post was helpful..
Cheers
May 4 '07 #5
frozenmist
179 Expert 100+
will this work for multiple variables in the where clause as well?
Yes it will..
In the using clause, give the variables corresponding to the question mark positions

Cheers
May 4 '07 #6
cuddles
21
Hi,

The above cursor is supposed to fetch the data for a specific row, how can i fetch it into a variable?

here's how its supposed to go:

DECLARE vStmt Varchar (250);
DECLARE wsJobI INTEGER
DECLARE wsOrdI INTEGER

DECLARE dispCSR CURSOR FOR v_SQL_stmt;
SET vStmt = 'SELECT JOB_I FROM "ALI.TDSP_JOB_XRF" WHERE ORD_I = ? AND DSP_NBR = 1 WITH UR;';

PREPARE v_SQL_stmt FROM vStmt;
OPEN dispCSR USING wsOrdI;

FETCH dispCSR
INTO wsJobI;
I get this error--> WSJOBI" is not valid in the context where it is used. SQLSTATE=42703

CLOSE dispCSR;

how do i fetch data and store it in a variable for future use?
also dont i have to declare v_SQL_stmt as well? what would it be declared as?

Thanks for all the help!


Yes it will..
In the using clause, give the variables corresponding to the question mark positions

Cheers
May 5 '07 #7
frozenmist
179 Expert 100+
Hi,

The above cursor is supposed to fetch the data for a specific row, how can i fetch it into a variable?

here's how its supposed to go:

DECLARE vStmt Varchar (250);
DECLARE wsJobI INTEGER
DECLARE wsOrdI INTEGER

DECLARE dispCSR CURSOR FOR v_SQL_stmt;
SET vStmt = 'SELECT JOB_I FROM "ALI.TDSP_JOB_XRF" WHERE ORD_I = ? AND DSP_NBR = 1 WITH UR;';

PREPARE v_SQL_stmt FROM vStmt;
OPEN dispCSR USING wsOrdI;

FETCH dispCSR
INTO wsJobI;
I get this error--> WSJOBI" is not valid in the context where it is used. SQLSTATE=42703

CLOSE dispCSR;

how do i fetch data and store it in a variable for future use?
also dont i have to declare v_SQL_stmt as well? what would it be declared as?

Thanks for all the help!
That error is usually raised when you dont declare the variable. Just check that.
Edit you following statement as
SET vStmt = 'SELECT JOB_I FROM ALI.TDSP_JOB_XRF WHERE ORD_I = ? AND DSP_NBR = 1 WITH UR';

To fetch the statement you have wriiten is enough.
How ever is more than 1 row is returned , u need to have a loop to read each value.

see the example

CREATE PROCEDURE SAMPLE_PROC"
language sql
begin
declare v_var varchar(100);
declare col1 varchar(100);
declare v_flag integer default 1;
declare v_sql_stmt varchar(500);

declare v_cursor cursor for v_sql;
declare continue handler for not found
begin
set v_flag=-1;
end;
set v_var='name';
set v_sql_stmt='select * from table1 where col1 = ?';
prepare v_sql from v_sql_stmt ;

open v_cursor using v_var;
while(v_flag=1)
do
fetch v_cursor into col1;
if(v_flag!=-1) then
insert into table2(col1) values(col1);
end if;
end while;
close v_cursor;

end


The datatype v_sql variable is taken as STATEMENT.
Hope this was what u needed.

Cheers
May 5 '07 #8
cuddles
21
Thanks a bunch! this really helps!



That error is usually raised when you dont declare the variable. Just check that.
Edit you following statement as
SET vStmt = 'SELECT JOB_I FROM ALI.TDSP_JOB_XRF WHERE ORD_I = ? AND DSP_NBR = 1 WITH UR';

To fetch the statement you have wriiten is enough.
How ever is more than 1 row is returned , u need to have a loop to read each value.

see the example

CREATE PROCEDURE SAMPLE_PROC"
language sql
begin
declare v_var varchar(100);
declare col1 varchar(100);
declare v_flag integer default 1;
declare v_sql_stmt varchar(500);

declare v_cursor cursor for v_sql;
declare continue handler for not found
begin
set v_flag=-1;
end;
set v_var='name';
set v_sql_stmt='select * from table1 where col1 = ?';
prepare v_sql from v_sql_stmt ;

open v_cursor using v_var;
while(v_flag=1)
do
fetch v_cursor into col1;
if(v_flag!=-1) then
insert into table2(col1) values(col1);
end if;
end while;
close v_cursor;

end


The datatype v_sql variable is taken as STATEMENT.
Hope this was what u needed.

Cheers
May 7 '07 #9
cuddles
21
Hi,

I have another question concerning my cursor, suppose i fetch data into a variable as shown below

SET CsrStmt = 'SELECT DSP_D' ||
'FROM table1' ||
'WHERE JOB_ID = ?' ||
'WITH UR';

PREPARE v_SQL_stmt FROM CsrStmt;
OPEN dispDateCsrW USING wsJobI;
FETCH cursor1
INTO :var1_D:var1_DNi;

** DSP_D is of DATE data type

the character : is not recognized in IBM DB2 Development Center. I keep getting the error an unexpected token ":" was found. removing the : results in the stored procedure being successfully built.

How does the syntax work for the above FETCH statement? var1_DNi is a NULL indicator, right? So if there is no value fetched for a record's DSP_D, a null value will be stored in var1_DNi? Is that right?

if so whats the syntax equivalent to the above? how do i check for the value of NULL? is it IS NULL or IS NOT NULL?


Thanks for all the help
May 7 '07 #10
frozenmist
179 Expert 100+
Hi cuddles,

If u want to check if the returned value is NULL and need to change it to some other value then u can use value function.
select value(col1,1) from table.
By using this if col1 is null then it will be given a default value 1.

If you want to check if the value of col1 in the fetch
FETCH V_CURSOR INTO COL1
is null then you can use a
IF (COL1 IS NOT NULL) then else end if

I hope this was your question.
Cheers
May 7 '07 #11
cuddles
21
Thanks for all the help frozenmist!!!


Hi cuddles,

If u want to check if the returned value is NULL and need to change it to some other value then u can use value function.
select value(col1,1) from table.
By using this if col1 is null then it will be given a default value 1.

If you want to check if the value of col1 in the fetch
FETCH V_CURSOR INTO COL1
is null then you can use a
IF (COL1 IS NOT NULL) then else end if

I hope this was your question.
Cheers
May 8 '07 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: cricketunes | last post by:
Hi folks, this one's had me stumped! $conn=ora_logon("cricketunes@pickles","j8j3kf"); if ($conn != TRUE) die("Unable to connect to oracle, exiting...\n"); $cursor = ora_open($conn); if...
1
by: cricketunes | last post by:
Hi Folks, I have this code segment: $query = "Select * from Payments order by CustID"; .. .. /* execute the query */ .. if (ora_exec($cursor)) { $recordset=array();
3
by: Chris Geerdink | last post by:
combo with PHP. what is wrong with the Javascript? else { include("mysql.php"); $query1 = mysql_query("INSERT INTO gbook (naam, email, text) VALUES ('".$_POST."', '".$_POST."',...
4
by: asdf | last post by:
Hello! Can someone tell me whats wrong with this piece of code: Option Compare Database Option Explicit Sub retrieve() Dim rst As ADODB.Recordset Dim i As Integer
28
by: mooreit | last post by:
The purpose for my questions is accessing these technologies from applications. I develop both applications and databases. Working with Microsoft C#.NET and Microsoft SQL Server 2000 Production and...
18
by: **Developer** | last post by:
If e.Button = MouseButtons.Left Then also from a Dim Answer As DialogResult = MessageBox.Show.. Select Case Answer Case DialogResult.Yes
7
by: Mike Barnard | last post by:
It's a simple test... VERY SIMPLE. But... In an external stlyesheet some attributes don't show. With the same styles cut and pasted to the test internally it works as expected. Anyone tell...
2
by: Benzine | last post by:
I am running MS SQL 2000. I recently ran a procedure in Query Analyzer from the Master db to clear out all replication information so I could start/recreate it again. After I ran this...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.