473,785 Members | 2,249 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_Tab leName 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 8486

"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_Tab leName 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_Tab leName 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_Tab leName 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_Tab leName 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_a meritech.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
1891
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 24-28, 200) Organization committee: Roel Wuyts (primary contact - roel.wuyts@ulb.ac.be), Gilad Bracha, Wolfgang De Meuter, Stéphane Ducasse and Oscar Nierstrasz.
1
2390
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 2000, using an SqlConnection object to open the database and an SqlCommand object to transfer my SQL text to the database. Is this the "dynamic SQL" that is such a bad thing? What is my alternative? Wait until after my program is working and...
6
2982
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 strings'. I already know that no individual string will be longer than 50 characters. I just don't know before run time how many elements of the array will be needed. I have heard it is possible to dynamically allocate memory for a 2
3
1312
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 below... --
7
3391
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 either AND or OR but never mixed together. We can use Northwind database for my question, it is very similar to the structure of the problem on the database I am working on. IF(SELECT OBJECT_ID('REPORT')) IS NOT NULL DROP TABLE REPORT_SELECTION
0
2073
by: Pascal Costanza | last post by:
Dynamic Languages Day @ Vrije Universiteit Brussel ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Monday, February 13, 2006, VUB Campus Etterbeek The VUB (Programming Technology Lab, System and Software Engineering Lab), ULB (deComp) and the Belgian Association for Dynamic Languages (BADL) are very pleased to invite you to a whole day of presentations about the programming languages Self, Smalltalk and Common Lisp by experts in...
7
22498
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 added an entry to the executable's .exe.config file to specify the URL, and under the 1.1 framework this worked well. Unfortunately, this is not working under the 2.0 framework. I see in the Reference.cs file under the web service reference the...
1
7978
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 compiled and run without any erros but the second program has a run time error when the function return from allocate and the ptr become NULL. How to fixed this? Second Program: /* Best Method to allocate memory for 2D Array because it's ...
5
2586
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 algorithm to solve a certain messy problem, and many different things. For that I often use several general modules that I have written, like implementation of certain data structures, and small general "utility" functions/classes, plus of course...
0
9646
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10350
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10157
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10097
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8983
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6742
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4055
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3658
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2887
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.