By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,226 Members | 1,027 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,226 IT Pros & Developers. It's quick & easy.

Dynamic SQL in UDF

P: n/a
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
Share this Question
Share on Google+
7 Replies


P: n/a

"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

P: n/a
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

P: n/a
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

P: n/a
Thanks everybody for reply.
I see that SQL UDFs has a lot of limitation.

Regards
Yaro
Nov 12 '05 #5

P: n/a
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

P: n/a
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

P: n/a
> > 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 discussion thread is closed

Replies have been disabled for this discussion.