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

Dynamic SQL in UDF

Hello

Is it possible using dynamic SQL in UDF?
In Sybase below example work but in DB2 UDB 8.1.3 I get error:
"SQL0104N Unexpected element "EXECUTE IMMEDIATE" found....."

CREATE FUNCTION DropTable(p_TableName VARCHAR(30) )
RETURNS VARCHAR(50)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE var_SqlStr VARCHAR(50);
SET var_SqlStr = 'DROP TABLE ' || p_TableName;

EXECUTE IMMEDIATE var_SqlStr;

RETURN var_SqlStr;
END@

Any other manners?

Thanks in advance
Yaro
Nov 12 '05 #1
7 8461

"Yaro" <ya*************@op.pl> wrote in message
news:cf**********@nemesis.news.tpi.pl...
Hello

Is it possible using dynamic SQL in UDF?
In Sybase below example work but in DB2 UDB 8.1.3 I get error:
"SQL0104N Unexpected element "EXECUTE IMMEDIATE" found....."

CREATE FUNCTION DropTable(p_TableName VARCHAR(30) )
RETURNS VARCHAR(50)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE var_SqlStr VARCHAR(50);
SET var_SqlStr = 'DROP TABLE ' || p_TableName;

EXECUTE IMMEDIATE var_SqlStr;

RETURN var_SqlStr;
END@

Any other manners?

It appears that what you are trying to do is not possible in a UDF in V8.1
of DB2. (I am assuming that you are on Windows, Linux, or Unix).

According to the SQL Reference Volume 2
(ftp://ftp.software.ibm.com/ps/produc...r/db2s2e80.pdf
), the CREATE FUNCTION (SQL Scalar, Table or Row) statement may only have
one of the following in the SQL function body: the keyword RETURN (which
doesn't help you) or a "dynamic-compound-statement". If you follow the link
for "Compound SQL (Dynamic)" at the bottom of the page, it says that the
only SQL statements you can use are: fullselect; searched UPDATE, searched
DELETE, INSERT, or SET. Apparently, DROP and EXECUTE IMMEDIATE are not
allowed in this context. You will have to drop tables in another way.

Luckily, there are many ways to drop tables in DB2:
- You can use the Control Center.
- You can issue DROP commands from the DB2 command window.
- You can code DROP statements within applications. For example, I just used
the Stored Procedure Builder to create a Java UDF that will drop any table
within the database to which I am currently connected. (I haven't installed
DB2 V8.1 yet so I used the Stored Procedure Builder within DB2 V7.2. You
should be able to create a similar Java UDF in DB2 V8.1 with the Development
Center.)

Rhino


Nov 12 '05 #2
See SQL Reference V2 for full details about UDF's.

My 8.1 manual says:
READS SQL DATA or CONTAINS SQL
Indicates what type of SQL statements can be executed. Because the SQL
statement supported is the RETURN statement, the distinction has to do
with whether or not the expression is a subquery.

READS SQL DATA
Indicates that SQL statements that do not modify SQL data can be
executed by the function (SQLSTATE 42985).

CONTAINS SQL
Indicates that SQL statements that neither read nor modify SQL data can
be executed by the function (SQLSTATE 42985).

I don't see any option to allow modifying SQL data in a UDF.

A stored procedure is allowed to update SQL data.

Phil Sherman



Yaro wrote:
Hello

Is it possible using dynamic SQL in UDF?
In Sybase below example work but in DB2 UDB 8.1.3 I get error:
"SQL0104N Unexpected element "EXECUTE IMMEDIATE" found....."

CREATE FUNCTION DropTable(p_TableName VARCHAR(30) )
RETURNS VARCHAR(50)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE var_SqlStr VARCHAR(50);
SET var_SqlStr = 'DROP TABLE ' || p_TableName;

EXECUTE IMMEDIATE var_SqlStr;

RETURN var_SqlStr;
END@

Any other manners?

Thanks in advance
Yaro


Nov 12 '05 #3
Yaro wrote:
Hello

Is it possible using dynamic SQL in UDF?
In Sybase below example work but in DB2 UDB 8.1.3 I get error:
"SQL0104N Unexpected element "EXECUTE IMMEDIATE" found....."

CREATE FUNCTION DropTable(p_TableName VARCHAR(30) )
RETURNS VARCHAR(50)
LANGUAGE SQL
BEGIN ATOMIC
DECLARE var_SqlStr VARCHAR(50);
SET var_SqlStr = 'DROP TABLE ' || p_TableName;

EXECUTE IMMEDIATE var_SqlStr;

RETURN var_SqlStr;
END@

Any other manners?

Thanks in advance
Yaro

Use a stored procedure as was mentioned previously. You can do it with
SQL/PL, almost exactly as you have above. If there is some reason it
must be a UDF, you'll have to use a different language to implement it.

You'll need the MODIFIES SQL DATA clause if you want to drop tables.

UNTESTED:

CREATE PROCEDURE DropTable(p_TableName VARCHAR(30))
MODIFIES SQL DATA
LANGUAGE SQL

BEGIN
DECLARE v_stmt_text VARCHAR(256);

SET v_stmt_text = 'DROP TABLE ' || p_TableName ;
EXECUTE IMMEDIATE v_stmt_text ;
RETURN 0;
END
@

--
Rob Wilson
rob_wilson_at_ameritech.net (replace _at_ with @)
Nov 12 '05 #4
Thanks everybody for reply.
I see that SQL UDFs has a lot of limitation.

Regards
Yaro
Nov 12 '05 #5
Yaro wrote:
Thanks everybody for reply.
I see that SQL UDFs has a lot of limitation.


Yes, and a lot of them are there for a good reason if you consider the
typical scenario where UDFs are used: in queries aka SELECT statements.
Running a query and having side effects like data modification is rather
far away from the relational model. (Nevertheless, such features make it
slowly into the products.)

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #6
Philip Sherman wrote:
See SQL Reference V2 for full details about UDF's.

My 8.1 manual says:
READS SQL DATA or CONTAINS SQL
Indicates what type of SQL statements can be executed. Because the SQL
statement supported is the RETURN statement, the distinction has to do
with whether or not the expression is a subquery.

READS SQL DATA
Indicates that SQL statements that do not modify SQL data can be
executed by the function (SQLSTATE 42985).

CONTAINS SQL
Indicates that SQL statements that neither read nor modify SQL data can
be executed by the function (SQLSTATE 42985).

I don't see any option to allow modifying SQL data in a UDF.


You might want to have a look here:
http://publib.boulder.ibm.com/infoce...n/r0003493.htm

MODIFIES SQL DATA was added to UDFs in FP4 for table functions.

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #7
> > I see that SQL UDFs has a lot of limitation.

Yes, and a lot of them are there for a good reason if you consider the
typical scenario where UDFs are used: in queries aka SELECT statements.


Thank you. I will be fight with my habits :-)

Yaro
Nov 12 '05 #8

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

Similar topics

0
by: Roel Wuyts | last post by:
CALL FOR CONTRIBUTIONS International Workshop on Revival of Dynamic Languages http://pico.vub.ac.be/~wdmeuter/RDL04/index.html (at OOPSLA2004, Vancouver, British Columbia, Canada, October...
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...
6
by: Materialised | last post by:
Hi Everyone, I apologise if this is covered in the FAQ, I did look, but nothing actually stood out to me as being relative to my subject. I want to create a 2 dimensional array, a 'array of...
3
by: Stephen Gennard | last post by:
Hello, I having a problem dynamically invoking a static method that takes a reference to a SByte*. If I do it directly it works just fine. Anyone any ideas why? I have include a example...
7
by: serge | last post by:
How can I run a single SP by asking multiple sales question either by using the logical operator AND for all the questions; or using the logical operator OR for all the questions. So it's always...
0
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab,...
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
1
by: Peterwkc | last post by:
Hello all expert, i have two program which make me desperate bu after i have noticed the forum, my future is become brightness back. By the way, my problem is like this i the first program was...
5
by: bearophileHUGS | last post by:
I often use Python to write small programs, in the range of 50-500 lines of code. For example to process some bioinformatics data, perform some data munging, to apply a randomized optimization...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.