473,466 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Using a SQL Table function in a view ...

Hi,

This is a real hopeful one!

What we are trying to do:

1. We MUST present a mappable database object for our application
objects i.e a Table or a View

Some of the views are very complex, which raises another couple of
reqs.
1. The view complexity may need to be reduced. Using Table Functions,
could allow for
a.predicating parameters to obtain best performance
b.and the use of SQL/PL to help reduce complexity with the
inclusion of some procedural logic.

But, the heart of the problem is this,

How do we use Table functions within a view and still manage to pass
parameters in ?

One idea I have been looking at, is to create a 'parameter table'
which is populated with call parameters by the application, which the
view then performs a correlated join with
i.e
Calling App: (JABSTFP = table function parameter table for view)
insert into JABSTFP.ADD VALUES (1,1),(1,2),(1,3);
Select * from JABSTFV.ADD;

View definition (JABSTFV):
SELECT b.RESULT
FROM JABSTFP.ADD a, TABLE(JABSTF.ADD(a.v1,a.v2)) b

This works in the simple case but raises questions of
1. Multiple concurrent queries, how do we uniquely identify the
correct rows in the parameter table ?
2. How do we automatically delete the rows from the parameter table
after the select ? - (we can't do it within the Table function, as
MODIFIES SQL DATA is not valid when
referenced
inside a view).

If anybody has any input on any of this at all, then feel free.

Thanks.

Paul.
Nov 12 '05 #1
11 13479
Paul,

Can you define "mappable" object?
Or in other words:
Why can't you expose a table-function? Is it for updatability?

One rtaher wild thought would be to expose the "parameters" in in the
signature of the view (as columns!) and of course in the RETURNS clause
of the table function
You would then use a WHERE clause to pin the values.
After that it's up to the optimizer to tunnel through the mess
(predicate push down right into the RETURN statement, transitive
closure, etc....)
You may want to try this in FP4. Taught the horse a few more tricks there)

No guarantees... Depends on just how complex the table functions are.
Keep me posted :-)
Serge

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

One rtaher wild thought would be to expose the "parameters" in in the
signature of the view (as columns!) and of course in the RETURNS clause
of the table function
You would then use a WHERE clause to pin the values.
After that it's up to the optimizer to tunnel through the mess
(predicate push down right into the RETURN statement, transitive
closure, etc....)
You may want to try this in FP4. Taught the horse a few more tricks there)


not easy to understand. A working example would be helpful
Nov 12 '05 #3
*gulp*I was affraid of that....

CREATE TABLE T1(c1 INT, c2 INT);
CREATE FUNCTION foo() RETURNS TABLE(a INT, b INT)
RETURN SELECT * FROM T1;

CREATE VIEW v AS SELECT * FROM TABLE(foo()) AS F;

SELECT * FROM v WHERE a = 5;

DB2 will unravel all this and turn it into:
SELECT * FROM T1 WHERE c1 = 5;

This of course is a trivial example.
The point is that the table function is, at the end of the day just a
correlated subquery.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #4
Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<c0**********@hanover.torolab.ibm.com>...
Paul,

Can you define "mappable" object?
Or in other words:
Why can't you expose a table-function? Is it for updatability?


By 'mappable' I mean ..

We use a ObjectRelational mapping layer called TopLink(owned by Oracle
now).
This requires that we have physical Tables or Views to map the objects
too,
I don't believe we can map directly to a table function. Hence, we
need a way to wrap Table functions in views.
Nov 12 '05 #5
Serge,

Thanks a lot for this input.

The one area I'm still not sure of with this approach, is when
the table function gets more complex (which ours do & will).

i.e One of the main reasons for going this route would be to
pass parameters in to use those parameters throughout the function
to optimise ALL the queries within it. Would the predicate push-down
be as effective at achieving this throughout the function whereever the
parameter/predicate appeared, and not just in the final select ?

Thanks.

Paul.
Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<c1**********@hanover.torolab.ibm.com>...
*gulp*I was affraid of that....

CREATE TABLE T1(c1 INT, c2 INT);
CREATE FUNCTION foo() RETURNS TABLE(a INT, b INT)
RETURN SELECT * FROM T1;

CREATE VIEW v AS SELECT * FROM TABLE(foo()) AS F;

SELECT * FROM v WHERE a = 5;

DB2 will unravel all this and turn it into:
SELECT * FROM T1 WHERE c1 = 5;

This of course is a trivial example.
The point is that the table function is, at the end of the day just a
correlated subquery.

Cheers
Serge

Nov 12 '05 #6
Paul,

Ok let's refine this:

CREATE V AS SELECT p1, p2, c1, c2)
FROM (SELECT p1, p2 FROM SYSIBM.SYSDUMMY1) AS P,
TABLE(foo(P.p1, P.p2)) AS F(c1l, c2);

SELECT c1, c2 FROM V WHERE p1 = 5 and p2 = 6;

I think the optimizer has enough power to substitute p1 and p2 with 5
and 6 everywhere in the table function body.
Now I have never tried this and if it doesn't do it in all cases, too bad.

Note that the example above is, of course, not updatable anymore.
An instead of trigger may be advised here.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #7
I see, fat chance to convince Oracle to support DB2 Table functions, eh?

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

I have now a had chance to look/a try at this.

I don't think the FROM (select p1,p2 FROM SYSIBM.SYSDUMMMY1) you
suggest can be made to work.
Since...
1. p1,p2 are not a valid columns for SYSIBM.SYSDUMMY.
- I have also tried (VALUES(p1,p2)) as P(p1,p2) but again the
compiler
complains that p1 not valid where it used.
Presumably, you cannot reference p1 on itself in this way?

Many Thanks

Paul.

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<c1**********@hanover.torolab.ibm.com>...
Paul,

Ok let's refine this:

CREATE V AS SELECT p1, p2, c1, c2)
FROM (SELECT p1, p2 FROM SYSIBM.SYSDUMMY1) AS P,
TABLE(foo(P.p1, P.p2)) AS F(c1l, c2);

SELECT c1, c2 FROM V WHERE p1 = 5 and p2 = 6;

I think the optimizer has enough power to substitute p1 and p2 with 5
and 6 everywhere in the table function body.

Nov 12 '05 #9
Hmm, which version are you on? I dimply recall some name resolution
issues which ought to be fixed now.
Simply use:
from TABLE(......) as x
That will make suer DB2 can access all columns/variabels outside the
subquery.

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab
Nov 12 '05 #10
Serge,

I am running on V8.1 Fp4.

The following is the small test script I'm using. This works, but
note I have hard coded values in the view, (which I actually want to replace
with the "select p1,p2 from sysibm.sysdummy1" subquery - see later).

CREATE FUNCTION ADD(p_v1 INT, p_v2 INT )
RETURNS TABLE (RESULT INT)
F1: BEGIN ATOMIC
RETURN VALUES(p_v1 + p_v2);
END/

CREATE VIEW ADD_VIEW
(p1,
p2,
c1
)
AS SELECT p1,p2,c1
FROM
TABLE (VALUES(1,2),(2,3)) AS P(p1,p2),
TABLE (ADD(P.p1, P.p2)) AS F(c1)
/

The following version of the view can't be created

CREATE VIEW ADD_VIEW
(p1,
p2,
c1
)
AS SELECT p1,p2,c1
FROM
TABLE (select p1,p2 from sysibm.sysdummy1) AS P(p1,p2),
TABLE (ADD(P.p1, P.p2)) AS F(c1)
/
SQL0206N "P1" is not valid in the context where it is used. SQLSTATE=42703

I'm a little worried because this looks fundamentally strange to be referencing
p1 in both the main select and in the table subquery ?

Thanks again

Paul.

Serge Rielau <sr*****@ca.eye-be-em.com> wrote in message news:<c1**********@hanover.torolab.ibm.com>...
Hmm, which version are you on? I dimply recall some name resolution
issues which ought to be fixed now.
Simply use:
from TABLE(......) as x
That will make suer DB2 can access all columns/variabels outside the
subquery.

Cheers
Serge

Nov 12 '05 #11
OK, looking at this again, I was out for lunch with that example.
We seem to have a bit of a chicken-egg problem here.
If we start of with p1 and p2 being constants (just to have something to
start with), then the theorem prover will compare what you pass in with
the constnts chosen in the view definition and you end up with an empty
table because they are not the same.

I confess I don't see an out right now :-(

Cheers
Serge

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

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

Similar topics

3
by: Lauren Quantrell | last post by:
I did this thing with host_name(). I'm hoping someone can tell me if I'm gonna get into trouble with this scheme before it's too late... I have maybe 75 users. They all call on a table of...
7
by: PaulR | last post by:
Hi, We have several inter-related SQL Table Functions, which all optimise fine on their own and most of the time together. However, increasingly as we are adding logic and making their logic...
0
by: Michelle Keys | last post by:
I am trying to call a print function to print a string from a database using javascript. Which is RC_DATA of Varchar2(2500). This is a javascript is not being used. I have a thing that needs to...
1
by: ratnakarp | last post by:
Hi, I have a search text box. The user enters the value in the text box and click on enter button. In code behind on button click i'm writing the code to get the values from the database and...
7
by: Serge Rielau | last post by:
Hi all, Following Ian's passionate postings on problems with ALTOBJ and the alter table wizard in the control center I'll try to explain how to use ALTOBJ with this thread. I'm not going to get...
3
by: Beowulf | last post by:
I was just messing around with some ad hoc views and table returning UDFs today so I could look at and print out data from a small table and noticed something strange. If I stick my select...
4
by: lesperancer | last post by:
it looks like this will save many versions of a relationship window, but based on the fact that the same tables are displayed in the relationship window and it will restore versions of what was...
1
by: Sibongile Khanyile | last post by:
If anyone can help... I have a function that takes one parameter and bring back quite a lot of records. but with a conventional function the field size has a limit of 8000 and i can not use field...
1
by: swethak | last post by:
hi, i have a code to disply the calendar and add events to that. It works fine.But my requirement is to i have to disply a weekly and daily calendar.Any body plz suggest that what modifications i...
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
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,...
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
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...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
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 ...

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.