Solved

Need to write Complicated SQL Update Query in Informix

Posted on 2008-10-10
10
3,071 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
  • 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
 
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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
Ever wondered why sometimes your SQL Server is slow or unresponsive with connections spiking up but by the time you go in, all is well? The following article will show you how to install and configure a SQL job that will send you email alerts includ…
Via a live example, show how to extract information from SQL Server on Database, Connection and Server properties
Viewers will learn how to use the INSERT statement to insert data into their tables. It will also introduce the NULL statement, to show them what happens when no value is giving for any given column.

759 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

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now