If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > Informix > Update Query using a join in subquery

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 09-15-10, 14:41
seahawk10 seahawk10 is offline
Registered User
 
Join Date: Sep 2008
Posts: 3
Update Query using a join in subquery

I am probably making this more complicated that it needs to be.

But I have a field (sch_id) in a table called ed_rec that I need populated with data from a filed (id) in sch_rec.

sch_rec has all schools.

ed_rec has many students with several schools

ed_rec contains a field called 'ceeb_code' and so does sch_rec

So can I do an update query using a join?

My lame attempt:
update ed_rec
set sch_id = (select id from sch_rec where sch_rec.ceeb = ed_rec.ceeb)
where sch_id = 0 and ceeb <> 0 and sch_id is not null

Error:
Error: Cannot insert a null into column (ed_rec.sch_id).
SQLState: 23000
ErrorCode: -391
Position: 137
Reply With Quote
  #2 (permalink)  
Old 09-21-10, 06:27
InformixWilli InformixWilli is offline
Registered User
 
Join Date: Sep 2010
Location: Germany, Brunswick
Posts: 52
Yes

Its not possible to join more than one table in a Update Statement.

But i think your problem is that some ids are NULL and sch_id has a NOT NULL Constaint:
Code:
update  ed_rec
set     sch_id = (select id from sch_rec where sch_rec.ceeb = ed_rec.ceeb and id is not null)
where   sch_id = 0 and ceeb <> 0 and sch_id is not null
OR

Code:
update  ed_rec
set     sch_id = (select nvl(id,0) from sch_rec where sch_rec.ceeb = ed_rec.ceeb)
where   sch_id = 0 and ceeb <> 0 and sch_id is not null
If id is null then sch_id is set to 0.

Last edited by InformixWilli; 09-21-10 at 06:37. Reason: because I'm stupid =)
Reply With Quote
  #3 (permalink)  
Old 09-21-10, 18:14
seahawk10 seahawk10 is offline
Registered User
 
Join Date: Sep 2008
Posts: 3
This is what a colleague of mine put together to account for the nulls...

select sch_rec.ceeb,
count(*) xcnt
from sch_rec, id_rec
where sch_rec.ceeb is not null and
sch_rec.ceeb > 0 and
sch_rec.id = id_rec.id and
(id_rec.valid is null or id_rec.valid <> "N")
group by ceeb
having count(*) = 1
into temp xone with no log;


begin work;

update ed_rec set (sch_id) =
((select sch_rec.id
from sch_rec, xone, id_rec, id_rec edid_rec
where ed_rec.ceeb = sch_rec.ceeb and
sch_rec.ceeb = xone.ceeb and
sch_rec.id = id_rec.id and
(id_rec.valid is null or id_rec.valid <> "N") and
ed_rec.id = edid_rec.id and
(edid_rec.valid is null or edid_rec.valid <> "N")))
where ed_rec.id in
(select id_rec.id
from id_rec
where id_rec.valid is null or id_rec.valid <> "N") and
ed_rec.sch_id = 0 and
ed_rec.ceeb is not null and
ed_rec.ceeb > 0 and
ed_rec.ceeb in
(select xone.ceeb
from xone);
Reply With Quote
  #4 (permalink)  
Old 09-22-10, 02:25
InformixWilli InformixWilli is offline
Registered User
 
Join Date: Sep 2010
Location: Germany, Brunswick
Posts: 52
Okay... any questions or everything fine?
Reply With Quote
  #5 (permalink)  
Old 02-09-11, 18:29
seahawk10 seahawk10 is offline
Registered User
 
Join Date: Sep 2008
Posts: 3
Yes, all good. Been workign great.
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On