Portal | Manuals | References | Downloads | Info | Programs | JCLs | Mainframe wiki | Quick Ref
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Profile Log in to check your private messages Log in
 
Writing a case statement inside an INSERT statement

 
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2
View previous topic :: :: View next topic  
Author Message
ravindra.vadali

New User


Joined: 30 Jan 2012
Posts: 34
Location: USA

PostPosted: Sat Mar 03, 2012 12:55 am    Post subject: Writing a case statement inside an INSERT statement
Reply with quote

Hi,

This is the first time I am addressing this forum so please excuse me if my post is irrelevant & I would be happy to receive the stones (smaller please) if you throw at me

Here goes my requirement:
I have to INSERT a row in a table, and I have to alternate the values of a particular column to either a zero or one & either to Y or N depending on the values that are retrieved from a select stament, so I used a CASE statement inside the SET clause of the INSERT statement and designed the query as follows:

Code:
INSERT INTO AGREMENT_TABLE CSR_I
(
CSR_I.CTC_ADR_TYP_ID,
CSR_I.AGREEMENT_COUNT,
CSR_I.HAS_QUOTES_I,
)

VALUES
(
12345,
CASE 1   /*FIND ANY POLICIES*/ /*AGREEMENT_COUNT*/
WHEN (
      SELECT COUNT(*)
      FROM POLICY_TABLE CAG_POL
      JOIN MASTER_TABLE AGR_POL
        ON AGR_POL.AGREEMENT_ID = CAG_POL.AGREEMENT
      WHERE AGR_POL.AGREEMENT_TYPE = 'POLICY'
      AND CAG_POL.CUSTOMER_ID = CURSOR_CUS:CUSTOMER_ID
      AND CAG_POL.NMD_ISU_ODR_NB > 0
     ) > 0
THEN 1
ELSE 0
END,
CASE  2  /*FIND ANY QUOTES*/   /*HAS_QUOTES_I*/
WHEN (
      SELECT COUNT(*)
      FROM POLICY_TABLE CAG_POL
      JOIN MASTER_TABLE AGR_POL
        ON AGR_POL.AGREEMENT_ID = CAG_POL.AGREEMENT
      WHERE AGR_POL.AGREEMENT_TYPE = 'QUOTE'
      AND CAG_POL.CUSTOMER_ID = CURSOR_CUS:CUSTOMER_ID
      AND CAG_POL.NMD_ISU_ODR_NB > 0
     ) > 0
THEN 'Y'
ELSE 'N'
END
)

I know you would suggest me to run it in a test environment but unfortunately I don't have a test environment to run this so I am looking foward for your suggestions as to whether this query would work or should I bring the case logic out of the INSERT statement and then write two different INSERT statements which will be qualified based on the CASE logic.

Once again my sincere apologies if I didn't address my problem correctly or misquoted the details. I will more than happy to provide any details you need.

Thanks,
Ravi
Back to top
View user's profile Send private message

enrico-sorichetti

Global Moderator


Joined: 14 Mar 2007
Posts: 10308
Location: italy

PostPosted: Sat Mar 03, 2012 1:14 am    Post subject: Reply to: Writing a case statement inside an INSERT statemen
Reply with quote

Quote:
I would be happy to receive the stones (smaller please) if you throw at me


bigger are better, smaller stones tend to get in Your eyes icon_wink.gif
Back to top
View user's profile Send private message
ravindra.vadali

New User


Joined: 30 Jan 2012
Posts: 34
Location: USA

PostPosted: Sat Mar 03, 2012 3:30 am    Post subject:
Reply with quote

Hi All,

I think I got my answer, CASE statement works in SET clause of the INSERT.

Thanks a many for your patience
Back to top
View user's profile Send private message
View previous topic :: :: View next topic  
Post new topic   Reply to topic    IBMMAINFRAMES.com Support Forums -> DB2 All times are GMT + 6 Hours
Page 1 of 1

 

Search our Forum:

Similar Topics
Topic Author Forum Replies Posted
No new posts ejck thru rexx to multiple jobs insid... Susanta All Other Mainframe Topics 5 Tue Sep 19, 2017 1:39 pm
No new posts Insert a Row_number into table useit DB2 2 Tue Sep 19, 2017 1:07 pm
No new posts WER268A OUTREC STATEMENT : SYNTAX E... frozenblood87 SYNCSORT 12 Sat Aug 26, 2017 9:45 pm
No new posts Suppress value reported by BREAK in D... Learncoholic DFSORT/ICETOOL 3 Wed Aug 16, 2017 6:03 pm
No new posts ON 2 AND EVERY 1 - Statement ??? UmeySan COBOL Programming 2 Tue Jul 25, 2017 1:20 pm

Facebook
Back to Top
 
Job Vacancies | Forum Rules | Bookmarks | Subscriptions | FAQ | Polls | Contact Us