Solved

Need to write Complicated SQL Update Query in Informix

Posted on 2008-10-10
10
3,140 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
Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

 
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 500 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

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
In this blog post, we’ll look at how ClickHouse performs in a general analytical workload using the star schema benchmark test.
Viewers will learn how the fundamental information of how to create a table.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.

707 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