473,406 Members | 2,390 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,406 software developers and data experts.

EXECUTE SQL-string from StoredProc

How can I execute a dynamically constructed SQL-SELECT-string within a
Stored Procedure on DB2, like the "sp_executesql"-command on SQL Server?

From the DB2 Information Center I got the following information:
"The EXECUTE-command requires a statement-name which must identify a
statement that was previously prepared, and cannot be a SELECT
statement."
http://publib.boulder.ibm.com/infoce...n/r0000948.htm
Thanx in advance,

Twan Kennis
SKB Vragenlijst Services
Amsterdam, The Netherlands
Nov 12 '05 #1
3 3937
Twan Kennis wrote:
How can I execute a dynamically constructed SQL-SELECT-string within a
Stored Procedure on DB2, like the "sp_executesql"-command on SQL Server?


db2 -td@

CREATE PROCEDURE sp_executesql(sqltxt CLOB(2M))
MODIFIES SQL DATA
DYNAMIC RESULT SETS 1
BEGIN
DECLARE prefix VARCHAR(6);
DECLARE stmt STATEMENT;
PREPARE stmt FROM sqltxt;
SET prefix = UPPER(VARCHAR(SUBSTR(sqltxt, 1, 6)));
IF prefix = 'SELECT' OR
prefix = 'VALUES' OR
SUBSTR(prefix, 1, 4) = 'WITH'
THEN
BEGIN
DECLARE res CURSOR WITH RETURN TO CALLER
FOR stmt;
OPEN res;
END;
ELSE
EXECUTE stmt;
END IF;
END
@

CALL sp_executesql('CREATE TABLE T(c1 INT)')
@
CALL sp_executesql('INSERT INTO T VALUES 5, 6, 7')
@
CALL sp_executesql('SELECT * FROM T')
@

Enjoy
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Dear Serge,

It works; thank you very much!!!
(Besides the STATEMENT-type was an unknown UDT, but that was a minor
problem to fix.)

Twan Kennis

==================================================

"Serge Rielau" <sr*****@ca.ibm.com> wrote in message
news:3c*************@individual.net...
Twan Kennis wrote:
How can I execute a dynamically constructed SQL-SELECT-string within a Stored Procedure on DB2, like the "sp_executesql"-command on SQL
Server?
db2 -td@

CREATE PROCEDURE sp_executesql(sqltxt CLOB(2M))
MODIFIES SQL DATA
DYNAMIC RESULT SETS 1
BEGIN
DECLARE prefix VARCHAR(6);
DECLARE stmt STATEMENT;
PREPARE stmt FROM sqltxt;
SET prefix = UPPER(VARCHAR(SUBSTR(sqltxt, 1, 6)));
IF prefix = 'SELECT' OR
prefix = 'VALUES' OR
SUBSTR(prefix, 1, 4) = 'WITH'
THEN
BEGIN
DECLARE res CURSOR WITH RETURN TO CALLER
FOR stmt;
OPEN res;
END;
ELSE
EXECUTE stmt;
END IF;
END
@

CALL sp_executesql('CREATE TABLE T(c1 INT)')
@
CALL sp_executesql('INSERT INTO T VALUES 5, 6, 7')
@
CALL sp_executesql('SELECT * FROM T')
@

Enjoy
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #3
Twan Kennis wrote:
Dear Serge,

It works; thank you very much!!!
(Besides the STATEMENT-type was an unknown UDT, but that was a minor
problem to fix.)

That's odd. I actually ran this script on FP8.

Cheers
Serge

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

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

Similar topics

2
by: Tim Williams | last post by:
I'm trying to write a simple python program to access a MySQL database. I'm having a problem with using MySQLdb to get the results of a SQL command in a cursor. Sometimes the cursor.execute works,...
7
by: William Gill | last post by:
I have been trying to pass parameters as indicated in the api. when I use: sql= 'select * from %s where cusid = %s ' % name,recID) Cursor.execute(sql) it works fine, but when I try : sql=...
5
by: Gustavo Randich | last post by:
Hello, I'm writing an automatic SQL parser and translator from Informix to DB2. Now I'm faced with one of the most difficult things to translate, the "foreach execute procedure" functionality...
2
by: Norman Fritag | last post by:
Hi there The below code executes some queries. As newbie I was wondering weather you are better of using connection execute or command execute to execute queries? I am asking as...
1
by: Jack Bauer | last post by:
How can I execute a query which prompt the user for input? Something like SELECT FROM Table WHERE Name= In Oracle PL/SQL, you use & before variable names to have the SQL*Plus ask for input....
0
by: franjorge | last post by:
Hi, I have created two stored procedures via VB using this code: sql = "CREATE PROC " & nombre_proc & " AS SELECT *" & _ " From MBM_PUNTOS_SCE_SIN_COINCIDIR_SIEGE_FALTA_PM_NE_" & mes & _ "...
1
by: shubhangi | last post by:
Hi, Please check following code. In below code I don't want to display results of "execute (@sql)", but want @cntA to be displayed. Is there any command to stop display of execute command as per...
2
by: Ian Boyd | last post by:
We're encountering a situation where we're encountering a deadlock, and someone's been made the deadlock victim. But after that, DB2 refuses to run any SQL, and instead we get the error message: ...
7
by: ChaosKCW | last post by:
Hi I am trying to use pymssql, and have an issue where by the execute (not the fetch) is appearing to load all records into memory. if I execute con = pymssql.connect(...) cur =...
7
by: Roger | last post by:
This is the vba code Set db = CurrentDb On error goto fErr …. loop strSql = "UPDATE tblInvAnalysisWhse" & _ " SET ltDeviation = " & Sqr(dblError / intPeriods) & _ " WHERE item = '" &...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.