[an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive] (none) [an error occurred while processing this directive] [an error occurred while processing this directive] [an error occurred while processing this directive][an error occurred while processing this directive]
 
[an error occurred while processing this directive] [an error occurred while processing this directive]
Skåne Sjælland Linux User Group - http://www.sslug.dk Home   Subscribe   Mail Archive   Forum   Calendar   Search
MhonArc Date: [Date Prev] [Date Index] [Date Next]   Thread: [Date Prev] [Thread Index] [Date Next]   MhonArc
 

Re: [PROGRAMMERING] [SQL] Betinget INSERT?



> > Jacob Sparre Andersen skrev:
>
> > > Har SQL en kompakt og effektiv formulering af at man gerne vil
> > > indsætte en række i en tabel, hvis der ikke allerede findes en
> > > række med samme nøgle?
> >
> > Hvis du aldrig vil accepterer to rækker med samme nøgle, så bør du
> > lave et unique index på nøglen.
>
> Det har jeg skam også gjort.  Jeg regner bare ikke fejlhåndtering for
> at være en effektiv løsning.

Det är nog effektivare, och renare att använda exceptionhantering här?

http://www.stanford.edu/dept/itss/docs/oracle/10g/appdev.101/b10807/07_errs.htm#i3880
=>
DUP_VAL_ON_INDEX
A program attempts to store duplicate values in a database column that
is constrained by a unique index.

Hade jag fått underhålla kod, och dessutom välja mellan

begin
 insert into ASU_Papirtyper (Betegnelse) values ( 'obligation')
exception
  when DUP_VAL_ON_INDEX then
  null;
end

eller

   INSERT INTO ASU_Papirtyper (Betegnelse)
  SELECT 'obligation'
  FROM   DUAL
  WHERE  NOT EXISTS (SELECT 'x'
                     FROM   ASU_Papirtyper
                     WHERE  Betegnelse = 'obligation');

så hade jag inte begripit den senare.
Performancemässigt är det nog tveksamt också.

Men, jag får kanske inte välja ;-)

--
/Björn


 
Home   Subscribe   Mail Archive   Index   Calendar   Search

 
 
Questions about the web-pages to <www_admin>. Last modified 2009-04-01, 02:01 CEST [an error occurred while processing this directive]
This page is maintained by [an error occurred while processing this directive]MHonArc [an error occurred while processing this directive] # [an error occurred while processing this directive] *