[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

Need to write Complicated SQL Update Query in Informix

Posted on 2008-10-10
10
Medium Priority
?
3,207 Views
Last Modified: 2013-12-24
I am trying to write a complicated SQL update query (at least it's complicated to me, probably simple to others). I need to update table a (adm_rec) with the data from table s (sch_rec). But a isn't joined directly with s, I have to join them via table e (ed_rec).

So here is the select SQL:
select a.id, a.yr, a.cid, a.cinit, s.cid, s.cinit
from adm_rec a, ed_rec e, sch_rec s
where e.id = a.id
and e.sch_id = s.id
and a.cinit is null
and a.cid =0
and a.yr > 2008
and s.cinit is not null

a.id                a.yr              a.cid                a.cinit               s.cid               s.cinit
50124514      2009      0      <null>      50059459      EJP
50104113      2009      0      <null>      50085862      JME
50105741      2009      0      <null>      50010855      AR  
50106390      2009      0      <null>      50071301      DLI

So I want to put the value of s.cid into a.cid and the value of s.cinit into a.cinit.

Please help asap, all of my attempts give me a syntax error. I am new to informix. What I think should work is this:

update adm_rec
set cnslr_init = (select s.cinit
from adm_rec a, ed_rec e, sch_rec s
where e.id = a.id
and e.sch_id = s.id
and a.cinit is null
and a.cid =0
and a.yr > 2008
and s.cinit is not null)
where ed_rec.id = adm_rec.id
and ed_rec.sch_id = sch_rec.id
and adm_rec.cinit is null
and adm_rec.cid =0
and adm_rec.yr > 2008
and sch_rec.cinit is not null
0
Comment
Question by:seahawk9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
  • 2
10 Comments
 
LVL 37

Expert Comment

by:momi_sabag
ID: 22692839
your syntax is good for sql server, i don't know if informix likes that syntax, you can try this

update adm_rec a
set cnslr_init = (select s.cinit  from ed_rec e, sch_rec s where e.id = a.id and e.sch_id = s.id and a.cinit is null and a.cid =0 and a.yr > 2008 and s.cinit is not null)
0
 
LVL 5

Expert Comment

by:flutophilus
ID: 22700926
Seems to me that wouldn't work because you have a WHERE clause that joins to another table in an update. If we simplify what you have there you get this ...
UPDATE adm_rec SET  cnsir_init = (  expression  )
WHERE adm_rec.id = ed_rec.id   ....

That WHERE clause is the problem, it's joining adm_rec to ed_rec and you can't do that in an update.
What you have to do is something like
UPDATE adm_rec SET cnsir_init = ( expression )
WHERE EXISTS
(SELECT adm_rec.id FROM adm_rec, ed_rec, sch_rec
WHERE adm_rec.id = ed_rec.id AND ....        etc etc   )

You may need to test this syntax in bits (that is test the SELECTs separately) before putting the whole query together

HTH

0
 

Author Comment

by:seahawk9
ID: 22712797
I have the indivisual selects working correctly. But I am getting this error now:
Error: Cannot modify table or view used in subquery.
SQLState:  IX000
ErrorCode: -360
Position: 73

Here is the code:
update adm_rec
set cnslr_init = (select sch_rec.cnslr_init
from adm_rec, ed_rec, sch_rec
where ed_rec.id = adm_rec.id
and ed_rec.sch_id = sch_rec.id
and adm_rec.cnslr_init is null
and adm_rec.cnslr_id =0
and adm_rec.plan_enr_yr > 2008
and sch_rec.cnslr_init is not null)
WHERE EXISTS
(select adm_rec.id
from adm_rec, ed_rec, sch_rec
where adm_rec.id = ed_rec.id
and ed_rec.sch_id = sch_rec.id
and adm_rec.cnslr_init is null
and adm_rec.cnslr_id =0
and adm_rec.plan_enr_yr > 2008
and sch_rec.cnslr_init is not null)
0
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
LVL 37

Expert Comment

by:momi_sabag
ID: 22712910
well
it seems you can't use adm_rec in the subquery
what i would do is issue a select that will save in a temporary table all the records you need to update (by thier primary key) and then use a subquery on that table
0
 

Author Comment

by:seahawk9
ID: 22713723
Can I do that in the same query? And how do you create temp tables in informix?
Like SQL 2005 insert into #temp?
0
 

Author Comment

by:seahawk9
ID: 22713794
This gives me a syntax error:

select adm_rec.id
from adm_rec, ed_rec, sch_rec
INTO TEMP id_temp
where adm_rec.id = ed_rec.id
and ed_rec.sch_id = sch_rec.id
and adm_rec.cnslr_init is null
and adm_rec.cnslr_id =0
and adm_rec.plan_enr_yr > 2008
and sch_rec.cnslr_init is not null
0
 
LVL 5

Accepted Solution

by:
flutophilus earned 2000 total points
ID: 22719660
select adm_rec.id
from adm_rec, ed_rec, sch_rec
where adm_rec.id = ed_rec.id
and ed_rec.sch_id = sch_rec.id
and adm_rec.cnslr_init is null
and adm_rec.cnslr_id =0
and adm_rec.plan_enr_yr > 2008
and sch_rec.cnslr_init is not null
INTO TEMP id_temp
0
 
LVL 5

Expert Comment

by:flutophilus
ID: 22719712
Also you don't need to put adm_rec in the subquery
WHERE adm_rec.id IN (select ed_rec.id
from  ed_rec, sch_rec
where ed_rec.sch_id = sch_rec.id
and sch_rec.cnslr_init is not null  )
and adm_rec.cnslr_init is null
and adm_rec.cnslr_id =0
and adm_rec.plan_enr_yr > 2008 ;

The subquery joins ed_rec and sch_rec to produce a table of id values, WHERE .. IN checks that your adm_rec value exists in this table. (Effectively joining adm_rec to sch_rec and ed_rec.) The remaining part of the WHERE clause is a straigh test of adm_rec values and so is outside the subquery.
0
 
LVL 5

Expert Comment

by:flutophilus
ID: 22719747
Check out the Informix Guide to SQL : Reference and Informix Guide to SQL : Syntax, available free online from IBM. Enter the titles into your favourite search engine.
0
 

Author Closing Comment

by:seahawk9
ID: 31506408
Thanks for your work on this flutophilus! I actually wound up doing it a different way, since there were only 10 counselor's or so I grouped the data in that way and exported the id's to excel and then did an update based on ids for each of the counselors.

Apparently we are not allowed to create temp tables on our database. That feature is turned off. But regardless, your solution would have worked if that option was on.
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In part one, we reviewed the prerequisites required for installing SQL Server vNext. In this part we will explore how to install Microsoft's SQL Server on Ubuntu 16.04.
Lotus Notes has been used since a very long time as an e-mail client and is very popular because of it's unmatched security. In this article we are going to learn about  RRV Bucket corruption and understand various methods to Fix "RRV Bucket Corrupt…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

650 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question