?
Solved

Need to write Complicated SQL Update Query in Informix

Posted on 2008-10-10
10
Medium Priority
?
3,177 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
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Suggested Courses

762 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