Portal | Manuals | References | Downloads | Info | Programs | JCLs | Master the Mainframes
IBM Mainframe Computers Forums Index
 
Register
 
IBM Mainframe Computers Forums Index Mainframe: Search IBM Mainframe Forum: FAQ Memberlist Usergroups 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: 10211
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 Summing on Mixed Case Literals Roy Ware SYNCSORT 3 Tue Jan 03, 2017 10:18 pm
No new posts -913/-911 Deadlock during UPDATE stat... NoSleep319 DB2 5 Fri Nov 18, 2016 12:37 am
No new posts Problem in writing Output file vickey_dw COBOL Programming 5 Mon Nov 14, 2016 11:14 pm
No new posts Access SQLCA fields inside a stored p... gangapd DB2 4 Thu Oct 27, 2016 10:20 am
No new posts Easytrieve - Report writing - Only 1 ... Abhi Nature CA Products 2 Wed Sep 14, 2016 11:40 am


Facebook
Back to Top
 
Mainframe Wiki | Forum Rules | Bookmarks | Subscriptions | FAQ | Tutorials | Contact Us