Unix Technical Forum

help with database

This is a discussion on help with database within the Oracle Database forums, part of the Database Server Software category; --> I'm using Oracle as my database, and Dreamweaver to create the input forms. I need to be able to ...


Go Back   Unix Technical Forum > Database Server Software > Oracle Database

Register FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 02-25-2008, 03:49 AM
TekDragon
 
Posts: n/a
Default help with database

I'm using Oracle as my database, and Dreamweaver to create the input forms.
I need to be able to pass a value that is created automaticlly by the
database via the use of a trigger. Everything works fine when the first user
inputs his/her data, but then I get an error message after that.

Here is the trigger:
create or replace trigger wine_cust_trigger
before insert on wines
for each ro
begin
select customer_id into :new.customer_id from customer ;
end ;
/

Here is the error message:

Error Type:
Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
[Oracle][ODBC][Ora]ORA-01422: exact fetch returns more than requested number
of rows ORA-06512: at "LTRAUB.WINE_CUST_TRIGGER", line 2 ORA-04088: error
during execution of trigger 'LTRAUB.WINE_CUST_TRIGGER'
/phoenix/wines.asp, line 115

Any help would be appreciated.

Thanks.


Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 02-25-2008, 03:49 AM
bdbafh@gmail.com
 
Posts: n/a
Default Re: help with database

use an Oracle "sequence" to provide the number for the new customer_id,
e.g.:

create sequence seq_cust_id start with 1 increment by 1 nocycle
nomaxvalue cache 20;

select seq_cust_id.nextval into :new.customer_id from dual;

that should get you started.

-bdbafh

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 02-25-2008, 03:49 AM
onedbguru
 
Posts: n/a
Default Re: help with database

you are selecting ALL customer_ids into :new.customer_id...

not complete syntax
create sequence newcustseq.....

create or replace trigger wine_cust_trigger
before insert on wines
for each ro
begin
select newcustseq.nextval into :new.customer_id from dual;
end ;
/

OR

this very poor-man's sequence that is not very scalable due to locking
etc..
create or replace trigger wine_cust_trigger
before insert on wines
for each ro
begin
select max(customer_id)+1 into :new.customer_id from customer ;
end ;
/

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 02-25-2008, 03:49 AM
TekDragon
 
Posts: n/a
Default Re: help with database

Thanks that did the trick.

I forgot about the max function. Why the +1? I tried it with out that and it
works. My next question is what would happen if this application is used
accross multiple computers at the same time? Would the correct customer_id
by identified with the correct customer?

"onedbguru" <onedbguru@firstdbasource.com> wrote in message
news:1145390327.156831.163740@v46g2000cwv.googlegr oups.com...
> you are selecting ALL customer_ids into :new.customer_id...
>
> not complete syntax
> create sequence newcustseq.....
>
> create or replace trigger wine_cust_trigger
> before insert on wines
> for each ro
> begin
> select newcustseq.nextval into :new.customer_id from dual;
> end ;
> /
>
> OR
>
> this very poor-man's sequence that is not very scalable due to locking
> etc..
> create or replace trigger wine_cust_trigger
> before insert on wines
> for each ro
> begin
> select max(customer_id)+1 into :new.customer_id from customer ;
> end ;
> /
>



Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 02-25-2008, 03:49 AM
fitzjarrell@cox.net
 
Posts: n/a
Default Re: help with database

Comments embedded.
TekDragon wrote:
> Thanks that did the trick.
>
> I forgot about the max function. Why the +1? I tried it with out that and it
> works.


Because you don't have customer_id declared as a primary key, so you
can have duplicate entries in your table. This is what your current
trigger produces.

> My next question is what would happen if this application is used
> accross multiple computers at the same time? Would the correct customer_id
> by identified with the correct customer?
>


No, and with the code you're implementing a single user won't get a
correctly identified customer, either. The max(col) + 1 increments the
current highest value in the col and (hopefully) prevents duplicates.
Of course, it won't once you have more than one user connected. Any
select against the target table will produce duplicates between
sessions as the 'select' can only 'see' what has been committed, not
what is in process. A sequence has no such issues.

> "onedbguru" <onedbguru@firstdbasource.com> wrote in message
> news:1145390327.156831.163740@v46g2000cwv.googlegr oups.com...
> > you are selecting ALL customer_ids into :new.customer_id...
> >
> > not complete syntax
> > create sequence newcustseq.....
> >
> > create or replace trigger wine_cust_trigger
> > before insert on wines
> > for each ro
> > begin
> > select newcustseq.nextval into :new.customer_id from dual;
> > end ;
> > /
> >
> > OR
> >
> > this very poor-man's sequence that is not very scalable due to locking
> > etc..
> > create or replace trigger wine_cust_trigger
> > before insert on wines
> > for each ro
> > begin
> > select max(customer_id)+1 into :new.customer_id from customer ;
> > end ;
> > /
> >


I'd be using a sequence and a trigger to ensure you have unique entries
across sessions. I would also make your customer_id your primary key,
so it's both unique and not null; this, of course, would cause your
current trigger to fail on insert, and it may cause the creation of the
constraint to generate errors as you have multiple records with the
exact same customer_id (this is the problem your current trigger
creates).

You asked for assistance; it's foolish of you to not take that which is
offered.


David Fitzjarrell

Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On
Forum Jump


All times are GMT. The time now is 02:04 AM.


Powered by vBulletin® Version 3.6.5
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0
www.UnixAdminTalk.com