472,328 Members | 1,524 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

problem with UDF

Hi all,
I am getting this problem. Script and error are below
connect to jagdip user db2inst1 using ibmdb2 @

set current sqlid='db2inst1' @

drop function salesamount @
drop table employee @
drop table clients @
drop table products @
drop table sales @

create table employee
(emp_num integer not null,
emp_name varchar(20),
emp_commision real,
emp_salary real
) @
create table clients
(c_num integer not null,
c_name varchar(20),
c_addr varchar(20),
c_city varchar(15),
c_state varchar(2),
c_zip integer,
c_phone integer
) @
create table products
(p_num integer not null,
p_quantity integer,
p_price real
) @
create table sales
(s_num integer not null,
p_num integer not null,
s_quantity integer not null,
s_amount real not null,
emp_num integer not null,
c_num integer not null
) @

create function salesamount ( s_quant integer, p_number integer ) returns
real
begin atomic
declare SalesAmount real;
SET (SalesAmount) = (SELECT p_price from products where p_num = p_number);
return SalesAmount*s_quant;
end @
create unique index employee_idx
on employee(emp_num) @

create unique index clients_idx
on clients(c_num) @

create unique index products_idx
on products(p_num) @

create unique index sales_idx
on sales(s_num) @

alter table employee
add CONSTRAINT employee_idx primary key(emp_num) @

alter table clients
add CONSTRAINT clients_idx primary key(c_num) @

alter table products
add CONSTRAINT products_idx primary key(p_num) @

alter table sales
add CONSTRAINT sales_idx primary key(s_num)
add foreign key (p_num)
REFERENCES products(p_num)
ON DELETE RESTRICT
add foreign key (emp_num)
REFERENCES employee(emp_num)
ON DELETE RESTRICT
add foreign key (c_num)
REFERENCES clients(c_num)
ON DELETE RESTRICT
add CONSTRAINT sales_constraint check(s_amount =
db2inst1.salesamount(s_quantity, p_num)) @
this script is giving me problem

DB21034E The command was processed as an SQL st
valid Command Line Processor command. During SQ
SQL0440N No function by the name "SALESAMOUNT"
was found in the function path. SQLSTATE=42884

can any one help me

regards,
Jagdip



Nov 12 '05 #1
4 2981
Jagdip Singh <jx*****@cs.rit.edu> wrote:
Hi all,
I am getting this problem. Script and error are below
connect to jagdip user db2inst1 using ibmdb2 @ [...] set current sqlid='db2inst1' @
Here you set the value for the SQLID special register to the string
'db2inst1'. Because of the quotes, this is treated as a case-sensitive
schema name for your statements below.
create function salesamount ( s_quant integer, p_number integer ) returns
real
begin atomic
declare SalesAmount real;
SET (SalesAmount) = (SELECT p_price from products where p_num =
p_number); return SalesAmount*s_quant;
end @
A suggestion to simplify the function:

CREATE FUNCTION salesamount(s_quant INTEGER, p_number INTEGER)
RETURNS REAL
RETURN ( SELECT p_price * s_quant
FROM products
WHERE p_num = p_number )

Also, I recommend to explicitly specify the schema name in your statement to
avoid the problem you have here:

CREATE FUNCTION db2inst1.salesamount ...

Also for all the tables, it might be advisable to use an explicit schema
name.
alter table sales
add CONSTRAINT sales_idx primary key(s_num)
add foreign key (p_num)
REFERENCES products(p_num)
ON DELETE RESTRICT
add foreign key (emp_num)
REFERENCES employee(emp_num)
ON DELETE RESTRICT
add foreign key (c_num)
REFERENCES clients(c_num)
ON DELETE RESTRICT
add CONSTRAINT sales_constraint check(s_amount =
db2inst1.salesamount(s_quantity, p_num)) @


First, you still can't do this as I said in the other post. A constraint
cannot refer the data in another table.

To solve your problem, you have two options:

ALTER TABLE sales
ADD CONSTRAINT sales_constraint ( CHECK ( s_amount =
"db2inst1".salesamount(s_quantity, p_num)) @

This works because the double-quotes tell DB2 to take the schema name as
case-sensitive, so it matches what you specified above.

The other option is to use a "SET CURRENT SQLID = db2inst1" (without the
double or single quotes and let DB2 convert the names to upper-case as is
the default.
Once you solved the syntactical issues, you will get this error:

SQL0546N The check constraint "X" is invalid. SQLSTATE=42621

There are several reasons:
(1) your UDF has a subquery, which is not allowed
(2) your UDF is defined as EXTERNAL ACTION, which is not allowed
(3) your UDF is defined as NOT DETERMINISTIC (aka VARIANT), which is not
allowed

--
Knut Stolze
Information Integration
IBM Germany / University of Jena
Nov 12 '05 #2
take away the qotes on sqlid. I think what you have.
You function lives in "db2inst1", but you are trying to find it in
"DB2INST1"

Cheers
Serge
--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #3
BTW, get rid of the unnecessary SQL. RETURN (SELECT ....) will be enough.
DB2 can do A LOT of optimization for functions with only a RERTURN
statement.

Cheers
Serge

--
Serge Rielau
DB2 SQL Compiler Development
IBM Toronto Lab

Nov 12 '05 #4
Thanks everyone for replying to my problem
I also was playing around and figured out the same thing as check-constraint
is very limited

Warm regards,
Jagdip Singh

"Jagdip Singh" <jx*****@cs.rit.edu> wrote in message
news:3f********@buckaroo.cs.rit.edu...
Hi all,
I am getting this problem. Script and error are below
connect to jagdip user db2inst1 using ibmdb2 @

set current sqlid='db2inst1' @

drop function salesamount @
drop table employee @
drop table clients @
drop table products @
drop table sales @

create table employee
(emp_num integer not null,
emp_name varchar(20),
emp_commision real,
emp_salary real
) @
create table clients
(c_num integer not null,
c_name varchar(20),
c_addr varchar(20),
c_city varchar(15),
c_state varchar(2),
c_zip integer,
c_phone integer
) @
create table products
(p_num integer not null,
p_quantity integer,
p_price real
) @
create table sales
(s_num integer not null,
p_num integer not null,
s_quantity integer not null,
s_amount real not null,
emp_num integer not null,
c_num integer not null
) @

create function salesamount ( s_quant integer, p_number integer ) returns
real
begin atomic
declare SalesAmount real;
SET (SalesAmount) = (SELECT p_price from products where p_num = p_number); return SalesAmount*s_quant;
end @
create unique index employee_idx
on employee(emp_num) @

create unique index clients_idx
on clients(c_num) @

create unique index products_idx
on products(p_num) @

create unique index sales_idx
on sales(s_num) @

alter table employee
add CONSTRAINT employee_idx primary key(emp_num) @

alter table clients
add CONSTRAINT clients_idx primary key(c_num) @

alter table products
add CONSTRAINT products_idx primary key(p_num) @

alter table sales
add CONSTRAINT sales_idx primary key(s_num)
add foreign key (p_num)
REFERENCES products(p_num)
ON DELETE RESTRICT
add foreign key (emp_num)
REFERENCES employee(emp_num)
ON DELETE RESTRICT
add foreign key (c_num)
REFERENCES clients(c_num)
ON DELETE RESTRICT
add CONSTRAINT sales_constraint check(s_amount =
db2inst1.salesamount(s_quantity, p_num)) @
this script is giving me problem

DB21034E The command was processed as an SQL st
valid Command Line Processor command. During SQ
SQL0440N No function by the name "SALESAMOUNT"
was found in the function path. SQLSTATE=42884

can any one help me

regards,
Jagdip




Nov 12 '05 #5

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

Similar topics

117
by: Peter Olcott | last post by:
www.halting-problem.com
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about...
16
by: Dany | last post by:
Our web service was working fine until we installed .net Framework 1.1 service pack 1. Uninstalling SP1 is not an option because our largest customer...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...

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.