View previous topic :: View next topic
|
Author |
Message |
ravindra.vadali
New User
Joined: 30 Jan 2012 Posts: 34 Location: USA
|
|
|
|
Hi All,
My problem is not new or anything exciting. All I have to do is to udpate a bunch of columns based on a particular value from a parent column.
I tried the following (syntatically proved wrong )
UPDATE EMPLOYEE_TABLE
SET LAST_MODIFIED_DTM = CURRENT TIMESTAMP,
IF DEPT_NAME = '101'
THEN
SET DESIGNATION = 'MANAGER'
SET BASE_SALARY = 70000
SET EMPLOYEE_CODE = 'A01'
ELSEIF DEPT_NAME IS NULL
THEN
SET DESIGNATION = 'CONTRACTOR'
SET BASE_SALARY = 50000
SET EMPLOYEE_CODE = 'A0C'
ENDIF
WHERE ORGANIZATION_NAME = 'XYZ'
I tried the CASE LOGIC but then its proving to be more statements per column.
Could anyone please let me know if you ever tried a IF-ELSE logic in the UPDATE sql?
I am trying to achieve it programtically but just wanted to check in this forum before I go ahead with the traditional approach.
My apologies if I asked a nomadic question. Also, please let me know if you need more details and I will be more than happy to share.
Thanks a many |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Cant you have two SQL's
with
Code: |
WHERE DEPT_NAME = '101' |
and
Code: |
WHERE DEPT_NAME IS NULL |
|
|
Back to top |
|
|
dbzTHEdinosauer
Global Moderator
Joined: 20 Oct 2006 Posts: 6966 Location: porcelain throne
|
|
|
|
I would do as Pandora suggested,
for no other reason than your single sql will
SET LAST_MODIFIED_DTM = CURRENT TIMESTAMP
for all of ORGANIZATION_NAME = 'XYZ'
regardless of DEPT_NAME. |
|
Back to top |
|
|
ravindra.vadali
New User
Joined: 30 Jan 2012 Posts: 34 Location: USA
|
|
|
|
Thank you both for your suggestions!!
I kind of did the same in my source code, but I wanted to check if there could be a way to achieve if via an single UPDATE sql.
Once again I appreciate your time and patience to look into my issue.
Regards,
Ravi |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3053 Location: NYC,USA
|
|
|
|
It is possible, if you take out IF-ELSE part outside of UPDATE query and use a simple cobol host variables and move the corresponding values to them as per DEPT_NAME and then perform a UPDATE query. |
|
Back to top |
|
|
sureshpathi10
Active User
Joined: 03 May 2010 Posts: 154 Location: Kuala Lumpur
|
|
|
|
Rohit,
For doing IF-ELSE with host variable, he has to SELECT first.
I guess, Mr. Ravi wouldn't agree with you .. as it is too mainstream |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3053 Location: NYC,USA
|
|
|
|
Suresh,
As he is already doing a hard coding of those values everywhere , so I personally don't think he needs any select. i hope so... |
|
Back to top |
|
|
Rohit Umarjikar
Global Moderator
Joined: 21 Sep 2010 Posts: 3053 Location: NYC,USA
|
|
|
|
I think TS is more concerned on performance of the query and hence tryint out different options.
Pandora,
I see what you suggested would solve the purpose but it looks like DEPT_NAME is a nullable column and we don't know the volume of data in the table and hence this could cause a performance issues.
Correct me if I misunderstood. |
|
Back to top |
|
|
Pandora-Box
Global Moderator
Joined: 07 Sep 2006 Posts: 1592 Location: Andromeda Galaxy
|
|
|
|
Rohit,
Talking of performance issues
1.If the field DEPT_NAME is not Indexed it could cause performance issues.
2.If there are too many records to be updated there is a high possibility that query to fail with -904 and then in that case I would have a cursor to select and update the data with frequent commits.
Thats how I would approach it. |
|
Back to top |
|
|
|