IBM Mainframe Forum Index
 
Log In
 
IBM Mainframe Forum Index Mainframe: Search IBM Mainframe Forum: FAQ Register
 

Writing a case statement inside an INSERT statement


IBM Mainframe Forums -> DB2
Post new topic   Reply to topic
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
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

Superior Member


Joined: 14 Mar 2007
Posts: 10873
Location: italy

PostPosted: Sat Mar 03, 2012 1:14 am
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
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 View Bookmarks
All times are GMT + 6 Hours
Forum Index -> DB2

 


Similar Topics
Topic Forum Replies
No new posts COBOL -Linkage Section-Case Sensitive COBOL Programming 1
No new posts Two input files & writing counter... DFSORT/ICETOOL 12
No new posts Insert header record with record coun... DFSORT/ICETOOL 14
No new posts Insert system time/date (timestamp) u... DFSORT/ICETOOL 5
No new posts Identify Program Insert DB2 7
Search our Forums:

Back to Top