473,414 Members | 1,772 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,414 software developers and data experts.

dynamic SQL in stored procedure. Need Help

I am getting SQL0519N. What am I doing wrong.
Creare procedure get_table_count
language sql
specific get_table_count
begin

declare v_table_count integer;
declare v_table_name varchar(30);
declare sqlcode integer;
declare v_stmt statement;
declare v_sql varchar(200);

declare c0 cursor for select tabname from syscat.tables where tabschema
= 'db2inst11';

declare c1 cursor for v_stmt;

open c0;
fetch c0 into v_table_name;
while sqlcode <> 100 do

set v_sql ='SELECT COUNT(*) FROM DB2INST11.'||v_table_name
prepare v_stmt from v_sql;
open c2;
fetch from c2 into v_table_count
insert into get_table_count(table_name,table_count) values
(v_table_name,v_table_count);
fetch c0 into v_table_name;
end while;
close c0;

END @

Nov 12 '05 #1
4 8988
db*****@yahoo.com wrote:
I am getting SQL0519N. What am I doing wrong.
Creare procedure get_table_count
language sql
specific get_table_count
begin

declare v_table_count integer;
declare v_table_name varchar(30);
declare sqlcode integer;
declare v_stmt statement;
declare v_sql varchar(200);

declare c0 cursor for select tabname from syscat.tables where tabschema
= 'db2inst11';

declare c1 cursor for v_stmt;

open c0;
fetch c0 into v_table_name;
while sqlcode <> 100 do

set v_sql ='SELECT COUNT(*) FROM DB2INST11.'||v_table_name
prepare v_stmt from v_sql;
open c2;
fetch from c2 into v_table_count
insert into get_table_count(table_name,table_count) values
(v_table_name,v_table_count);
fetch c0 into v_table_name;
end while;
close c0;

END @

What about closing c2? ;-) The error message was dead on. It fails in
the second iteration.

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
db*****@yahoo.com wrote:
I am getting SQL0519N. What am I doing wrong.


Correcting several syntax errors renders stored procedure usable:

Create procedure get_table_count
language sql
specific get_table_count
begin

declare v_table_count integer;
declare v_table_name varchar(30);
declare sqlcode integer;
-- declare v_stmt statement;
declare v_sql varchar(200);

declare c0 cursor for select tabname from syscat.tables where tabschema
= 'DB2ADMIN';

declare c2 cursor for v_stmt;

open c0;
fetch c0 into v_table_name;
while sqlcode <> 100 do
set v_sql ='SELECT COUNT(*) FROM DB2ADMIN.' || v_table_name;
prepare v_stmt from v_sql;
open c2;
fetch from c2 into v_table_count;
close c2;
insert into get_table_count(table_name,table_count) values
(v_table_name,v_table_count);
fetch c0 into v_table_name;
end while;
close c0;

END @
Jan M. Nelken
Nov 12 '05 #3
Thanks a lot! that was a silly mistake

One more question, instead of opening and closing C2, can I simply use
EXECUTE after the prepared statement?

When I use it, it gives me SQL0519N

Nov 12 '05 #4
db*****@yahoo.com wrote:
Thanks a lot! that was a silly mistake

One more question, instead of opening and closing C2, can I simply use
EXECUTE after the prepared statement?

When I use it, it gives me SQL0519N

No, you need the cursor. In a future release of DB2 you may be able to
execute SELECT INTO dynamically, but not currently.

The only thing you could do to improve the procs readability is to use a
FOR loop instead of the outer cursor. A matter of taste.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Robert Scheer | last post by:
Hi. I have a stored procedure on a Oracle 8.1.6 database that generates a dynamic sql statement. This stored procedure has an output parameter that needs to return a count from a view. I can...
1
by: Guinness Mann | last post by:
When you guys talk about "dynamic SQL," to what exactly are you referring? Is dynamic SQL anything that isn't a stored procedure? Specifically, I use ASP.NET to communicate with my SQL Server...
5
by: Ralph | last post by:
Hi all, I'm a newbie to MS-SQL UDFs and seem to have a real big problem. I need to implement a logic to receive an adress build out of various user definable fields from various user defined...
4
by: MD | last post by:
I am trying to create a dynamic SQL statement to create a view. I have a stored procedure, which based on the parameters passed calls different stored procedures. Each of this sub stored procedure...
2
by: Caro | last post by:
I have a stored procedure spGetAccessLogDynamic and when I try to call it I get the following error: Server: Msg 2812, Level 16, State 62, Line 1 Could not find stored procedure 'S'. I dont...
1
by: Todd Peterson | last post by:
I'm a newbie to DB2 and am trying to figure out how to write a stored procedure, using dynamic SQL statements to return a result set. I believe the majority of the hurdles I have been facing might...
3
by: CSDunn | last post by:
Hello, I have a situation with MS Access 2000 in which I need to display report data in spreadsheet orientation (much like a datasheet view for a form). If you think of the report in terms of what...
5
by: alingsjtu | last post by:
Hello, every body. When execute dynamic generated multiple OPENQUERY statements (which linkes to DB2) in SQLServer, I always got SQL1040N The maximum number of applications is already connected...
23
by: valentin tihomirov | last post by:
Hello, -=PREHISTORY=- Occasionally, I wanted to rewrite my UPDATE code to prevent SQL injection. I generated the SQL query UPDATE tt SET @p1_name = @p1_val
2
by: IuliaS | last post by:
Hello everyone! I want to create a stored procedure, so I can more easily, and transparent retrieve data from db2. Long story short: when a user wants to put some data in the DB, he also creates...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.