If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > DB2 > Delete statement

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-14-07, 16:00
citi citi is offline
Registered User
 
Join Date: Aug 2007
Posts: 45
Question Delete statement

DELETE FROM S.I73_FNDSRC_PTYPE_T
WHERE I73_NEED_ID =
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID);

The above code is part of a stored procedure that I'm writing. The delete statement works if the tables I73 & I74 each have only one row where I73_NEED_ID = I74_NEED_ID. However, if there is more than one row returned, I get a scalar function error and the stored procedure terminates.

Is it possible to rewrite the above code using some type of join?
Where could I find an example?

Thanks in advance.
Reply With Quote
  #2 (permalink)  
Old 09-14-07, 16:43
nidm nidm is offline
Registered User
 
Join Date: May 2003
Posts: 113
Quote:
Originally Posted by citi
DELETE FROM S.I73_FNDSRC_PTYPE_T
WHERE I73_NEED_ID =
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID);

The above code is part of a stored procedure that I'm writing. The delete statement works if the tables I73 & I74 each have only one row where I73_NEED_ID = I74_NEED_ID. However, if there is more than one row returned, I get a scalar function error and the stored procedure terminates.

Is it possible to rewrite the above code using some type of join?
Where could I find an example?

Thanks in advance.
the right high side
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID)
is called scalar fullselect, which must result only one row(otherwise, sqlcode)

you can use '= any' predicate, such as
DELETE FROM S.I73_FNDSRC_PTYPE_T
WHERE I73_NEED_ID = ANY
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID);
Reply With Quote
  #3 (permalink)  
Old 09-15-07, 09:50
stolze stolze is offline
Registered User
 
Join Date: Jan 2007
Location: Jena, Germany
Posts: 2,662
Quote:
Originally Posted by nidm
the right high side
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID)
is called scalar fullselect, which must result only one row(otherwise, sqlcode)

you can use '= any' predicate, such as
DELETE FROM S.I73_FNDSRC_PTYPE_T
WHERE I73_NEED_ID = ANY
(SELECT I74_NEED_ID
FROM S.I74_NEED_SERVICE_T
WHERE I74_CAP_ID = CAP_ID);
= ANY is the same as an IN-operator.

Code:
WHERE I73_NEED_ID IN ( SELECT ... )
__________________
Knut Stolze
IBM DB2 Analytics Accelerator
IBM Germany Research & Development
Reply With Quote
  #4 (permalink)  
Old 09-17-07, 08:21
citi citi is offline
Registered User
 
Join Date: Aug 2007
Posts: 45
Smile

Thank you both for your replies!
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On