Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3245
  • Last Modified:

Need to write Complicated SQL Update Query in Informix

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
seahawk9
Asked:
seahawk9
  • 4
  • 4
  • 2
1 Solution
 
momi_sabagCommented:
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
 
flutophilusCommented:
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
 
seahawk9Author Commented:
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
momi_sabagCommented:
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
 
seahawk9Author Commented:
Can I do that in the same query? And how do you create temp tables in informix?
Like SQL 2005 insert into #temp?
0
 
seahawk9Author Commented:
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
 
flutophilusCommented:
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
 
flutophilusCommented:
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
 
flutophilusCommented:
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
 
seahawk9Author Commented:
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

Get free NFR key for Veeam Availability Suite 9.5

Veeam is happy to provide a free NFR license (1 year, 2 sockets) to all certified IT Pros. The license allows for the non-production use of Veeam Availability Suite v9.5 in your home lab, without any feature limitations. It works for both VMware and Hyper-V environments

  • 4
  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now