 |
|
| Computers Forum Index » Computer - Databases - IBM DB2 » get count from table in function... |
|
Page 1 of 1 |
|
| Author |
Message |
| Arun Srinivasan... |
Posted: Mon Oct 26, 2009 7:31 am |
|
|
|
Guest
|
Hi
My requirement -
write a function to return count from a table that is given in the
argument.
reason :
use the function in sql like
select tabschema , tabname from syscat.tables where arun.func1
(tabschema,tabname)=0
want a list of all tables that have 0 count (requirement may vary,
like want list of all tables that have more than 10 rows of particular
predicate)....
My function :
create function arun.count_tab(v_tabschema varchar(32), v_tabname
varchar(32))
returns integer
begin atomic
declare v_count integer default 0;
declare stmt1 varchar(100) ;
--declare c1 cursor with return to client for stmt;
set stmt1= 'insert into arun.test1 select count(*) from ' ||
v_tabschema || '.' || v_tabname;
prepare stmt from stmt1 ;
execute stmt into v_count using v_tabschema, v_tabname;--using
v_tabschema, v_tabname into v_count ;
return v_count;
end;
[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "from"
was found following "name; prepare stmt". Expected tokens may
include: "<join_type_without_spec> JOIN <join_operand>". LINE
NUMBER=10. SQLSTATE=42601
Statement processed with ERROR.
The same with SP works, but only when I use the cursor and pass the
count in resultset..
Please help, and let me know wny i cant use prepare and execute in
function. |
|
|
| Back to top |
|
|
|
| Lennart... |
Posted: Mon Oct 26, 2009 6:44 pm |
|
|
|
Guest
|
On 26 Okt, 08:31, Arun Srinivasan <arunro... at (no spam) gmail.com> wrote:
Quote: Hi
My requirement -
write a function to return count from a table that is given in the
argument.
reason :
use the function in sql like
select tabschema , tabname from syscat.tables where arun.func1
(tabschema,tabname)=0
want a list of all tables that have 0 count (requirement may vary,
like want list of all tables that have more than 10 rows of particular
predicate)....
My function :
create function arun.count_tab(v_tabschema varchar(32), v_tabname
varchar(32))
returns integer
begin atomic
declare v_count integer default 0;
declare stmt1 varchar(100) ;
--declare c1 cursor with return to client for stmt;
set stmt1= 'insert into arun.test1 select count(*) from ' ||
v_tabschema || '.' || v_tabname;
prepare stmt from stmt1 ;
execute stmt into v_count using v_tabschema, v_tabname;--using
v_tabschema, v_tabname into v_count ;
return v_count;
end;
[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "from"
was found following "name; prepare stmt". Expected tokens may
include: "<join_type_without_spec> JOIN <join_operand>". LINE
NUMBER=10. SQLSTATE=42601
Statement processed with ERROR.
The same with SP works, but only when I use the cursor and pass the
count in resultset..
Please help, and let me know wny i cant use prepare and execute in
function.
Arun, I don't have an answer to your question but in general what you
can do in an sql function is more restricted than what you can do in a
sql stored procedure. But if you have a working SP, can't you call
that SP from the function and work around your problem that way?
/Lennart |
|
|
| Back to top |
|
|
|
| Tonkuma... |
Posted: Wed Nov 25, 2009 5:12 pm |
|
|
|
Guest
|
Quote:
That's the answer up to and including DB2 9.5.
In DB2 9.7 you can use the full power of SQL PL within a scalar function.
The telltale is omission of the "ATOMIC" keyword.
That is:
CREATE FUNCTION .... RETURN => INLINEd
CREATE FUNCTION .... BEGIN ATOMIC .... END => INLINEd
CREATE FUNCTION .... BEGIN ... END => COMPILEd
Disclaimer: No COMMIT and ROLLBACK in UDF (for obvious reasons)
Cheers
Serge
Yes!
Here is an example.
I could use BOOLEAN data type in an UDF, by omission of the "ATOMIC"
keyword.
------------------------------ Commands Entered
------------------------------
CREATE FUNCTION like_enhanced
(match VARCHAR(254), pattern VARCHAR(254) )
RETURNS BOOLEAN
CONTAINS SQL
DETERMINISTIC
NO EXTERNAL ACTION
BEGIN
DECLARE func_ret BOOLEAN;
CALL like_enhanced(match, pattern, func_ret);
RETURN func_ret;
END
at (no spam)
------------------------------------------------------------------------------
DB20000I The SQL command completed successfully. |
|
|
| Back to top |
|
|
|
| Serge Rielau... |
Posted: Wed Nov 25, 2009 7:14 pm |
|
|
|
Guest
|
Lennart wrote:
Quote: On 26 Okt, 08:31, Arun Srinivasan <arunro... at (no spam) gmail.com> wrote:
Hi
My requirement -
write a function to return count from a table that is given in the
argument.
reason :
use the function in sql like
select tabschema , tabname from syscat.tables where arun.func1
(tabschema,tabname)=0
want a list of all tables that have 0 count (requirement may vary,
like want list of all tables that have more than 10 rows of particular
predicate)....
My function :
create function arun.count_tab(v_tabschema varchar(32), v_tabname
varchar(32))
returns integer
begin atomic
declare v_count integer default 0;
declare stmt1 varchar(100) ;
--declare c1 cursor with return to client for stmt;
set stmt1= 'insert into arun.test1 select count(*) from ' ||
v_tabschema || '.' || v_tabname;
prepare stmt from stmt1 ;
execute stmt into v_count using v_tabschema, v_tabname;--using
v_tabschema, v_tabname into v_count ;
return v_count;
end;
[IBM][CLI Driver][DB2/LINUXX8664] SQL0104N An unexpected token "from"
was found following "name; prepare stmt". Expected tokens may
include: "<join_type_without_spec> JOIN <join_operand>". LINE
NUMBER=10. SQLSTATE=42601
Statement processed with ERROR.
The same with SP works, but only when I use the cursor and pass the
count in resultset..
Please help, and let me know wny i cant use prepare and execute in
function.
Arun, I don't have an answer to your question but in general what you
can do in an sql function is more restricted than what you can do in a
sql stored procedure. But if you have a working SP, can't you call
that SP from the function and work around your problem that way?
That's the answer up to and including DB2 9.5.
In DB2 9.7 you can use the full power of SQL PL within a scalar function.
The telltale is omission of the "ATOMIC" keyword.
That is:
CREATE FUNCTION .... RETURN => INLINEd
CREATE FUNCTION .... BEGIN ATOMIC .... END => INLINEd
CREATE FUNCTION .... BEGIN ... END => COMPILEd
Disclaimer: No COMMIT and ROLLBACK in UDF (for obvious reasons)
Cheers
Serge
PS: The same holds for triggers (BEFORE, AFTER and ROW LEVEL at this point)
--
Serge Rielau
SQL Architect DB2 for LUW
IBM Toronto Lab |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Mon Dec 07, 2009 6:18 am
|
|