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

Host Variables and Table names = Dynamic SQL ?

People,

I've ventured into the wonderful world of Stored Procedures. My first
experience has been relatively successful however I am stuck on using
host variables to specifiy actualy table or column names in a FROM
clause. After many hours or reading all manner of manuals I've
discovered it appears this is not possible and that in order to so I
need to further venture into dynamic SQL.

My present procedure is based on all static SQL and specifies a numer
of host variables used in IF and WHILE clauses and cursor WHERE
statements. If I have to now use dynamic SQL to get around this nasty
TABLE restriction, is there a way to include such in the same
procedure under different declare statements or am I going to have to
start a completely new learning curve here ?

I guess what I am asking is for a very basic example of how I might go
about using a variable to specify a table name under whatever
circumstances you wish to provide (if at all .. heh).

The variables I use presently are usually set by SELECT INTO
statements, or as the result of a calculation and the reason I need a
vaiable for the table name is it depends on the result of a query in
the proc, therefore not being known at compile time. I take it this
rule also applies to UDFs as well ?

Any rules and limitations providing a brief example would be very much
appreciated. Id very much like to NOT have to completly re-write the
whole thing as it is getting somewhat lengthy.

Procedure is written in a simple SQL CLP script and debugged using
Development Center. System is Windows 2000 server, DB2 v8.1.4 ESE

Many thanks in anticipation,

Tim
Nov 12 '05 #1
4 13031
Tim,

There is not much magic to dynamic SQL in SQl Procedures.

Simply look up the EXECUTE IMMEDIATE as well as PREPARE and EXECUTE
statements.
Essentially you glue together any string you please, associate alues to
the parameter markers (in place of SQL variables and host variables) and
of you go.

BTW, this "nasty restriction" fundametal to the working of SQL.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #2
Tim.D wrote:
People,

I've ventured into the wonderful world of Stored Procedures. My first
experience has been relatively successful however I am stuck on using
host variables to specifiy actualy table or column names in a FROM
clause. After many hours or reading all manner of manuals I've
discovered it appears this is not possible and that in order to so I
need to further venture into dynamic SQL.

My present procedure is based on all static SQL and specifies a numer
of host variables used in IF and WHILE clauses and cursor WHERE
statements. If I have to now use dynamic SQL to get around this nasty
TABLE restriction, is there a way to include such in the same
procedure under different declare statements or am I going to have to
start a completely new learning curve here ?

I guess what I am asking is for a very basic example of how I might go
about using a variable to specify a table name under whatever
circumstances you wish to provide (if at all .. heh).

The variables I use presently are usually set by SELECT INTO
statements, or as the result of a calculation and the reason I need a
vaiable for the table name is it depends on the result of a query in
the proc, therefore not being known at compile time. I take it this
rule also applies to UDFs as well ?

Any rules and limitations providing a brief example would be very much
appreciated. Id very much like to NOT have to completly re-write the
whole thing as it is getting somewhat lengthy.

Procedure is written in a simple SQL CLP script and debugged using
Development Center. System is Windows 2000 server, DB2 v8.1.4 ESE

Many thanks in anticipation,

Tim


Here is an example of using dynamic SQL inside
a SQL procedure:

CREATE PROCEDURE myprepopen
(
IN p_name VARCHAR(32)
)

SPECIFIC myprepopen
DYNAMIC RESULT SETS 1
LANGUAGE SQL

BEGIN
DECLARE v_sql VARCHAR(256);
DECLARE v_stmt STATEMENT;
DECLARE c_staff CURSOR WITH RETURN TO CLIENT FOR v_stmt;

/*
SQL select vorbereiten
*/
SET v_sql = 'SELECT id, name, salary FROM staff WHERE name LIKE ? FOR
READ ONLY';
PREPARE v_stmt FROM v_sql;

/*
Cursor oeffnen, platzhalter durch hostvariable ersetzen
*/
OPEN c_staff USING p_name;

END@
HTH.

Best regards

Eric
--
IT-Consulting Herber
***********************************************
Download the IFMX Database-Monitor for free at:
http://www.herber-consulting.de/BusyBee
***********************************************
Nov 12 '05 #3
Hi serge, long time no see.

I've managed to figure out most of it through more reading,
experimenting and trial and error, and it seems to work very nicely
for the most part. What I am having trouble with is piecing some
elements together into a variable for the PREPARE statement. I have
everything added using the concat || to contruct the statement but
cannot get a last piece to add properly.

It is the result of UDF that returns a varchar(12) character string. I
want to use this piece in the WHERE COLUMN = string statement then
order by the column. Unfortunately the UDF returns fine but without
apostrophie. I need to encase this result in 'string' ie: apostrophies
inorder to be syntactically correct.

For the life of me I have tried all I know but come up short on this
last bit.

ie:

set stmt = 'select ID from ' || tb_name || ' where ' || col_name || '
= ' || getudf(value) || ' order by ' || col_name ;

where tb_name and col_name are variables set in previous statement.
The result of getudf(value) must be something like 'ABC' rather than
ABC as col_name is a varchar column. btw the stmt is used in a cursor.
Ive even tried setting the result to another variable then using that
inlieu of the UDF but it still will not populate the value with the
required apostrophies. The PREPARE actually works of course, but when
the cursor opens it fails as the value requires the damned 's. Ive
tried multiple ''s but that will not build then. [mutter]

Arrrghhh .. heh

Any ideas on how this can be done and a quick example would save my
entire weekend ! .. many thanks again sir.
Nov 12 '05 #4
Tim,

In principle I'd suggest to use parameter markres (as described by
Eric). Thsi way you have a chance that DB2 recognizes multiple prepares
as being teh same and hence will exploit the dynamic statement cache.
To answer you question about quotes, you need to escape the quote with
another quote:
e.g values '''Hello'''
would return: 'Hello'
Ths can be done inside your UDF or outside.

'...WHERE ... = ''' || UDF() || '''';

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

2
by: Chris | last post by:
I have tableA, defined as: field1 varchar2(10), field2 varchar2(10), field3 varchar2(10) I have host variables defined as: v1 pic x(10) varying v2 pic x(10) varying
5
by: Ross A. Finlayson | last post by:
Hi, I'm scratching together an Access database. The development box is Office 95, the deployment box Office 2003. So anyways I am griping about forms and global variables. Say for example...
12
by: scott | last post by:
Is there a way to create dynamic variables when looping through a recordset? For example below, after the 1st loop I'd have myVarA1 and myVarB1, after 2nd loop, I'd get myVarA2 and myVarB2. CODE...
5
by: adi | last post by:
Hi all, Seems like a fundamental question to me but I dont have a definite answer for it, Gurus please enlighten me. I have a table 'Table1' whose structure changes dynamically based on some...
12
by: Tom | last post by:
Hi everyone, I don't know if anyone can help me, I've got roughly 25 forms in a site, with between 10 and 70 fields on each form. Now, these fields need to be inserted into a DB, each form...
7
by: misha | last post by:
Hello. I was wandering if someone could explain to me (or point to some manual) the process of mapping the addresses of host variables by DB2. Especially I would like to know when DB2 decides to...
5
by: Patrick | last post by:
Hi all, In my SQL procedures in DB2 8.2, I current declare several cursors that return select statements with 10 columns in it. As a result, assigning host variables becomes a coding mess, as...
1
by: Fix_Metal | last post by:
Hello all. I'm new to this group :) I have a problem with javascript language. I'm making an .asp page with some integrated Javascript functions. The page consists of some HTML selects and a...
111
by: Nate | last post by:
Hello, I am looking for a method to automatically declare variables in C. I'm not sure if there is a good way to do this, but I had something like this in mind... int i; for(i = 1; i < 4;...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
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...

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.