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 > sql update command from temporary table

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-11, 09:02
dmac25 dmac25 is offline
Registered User
 
Join Date: Jul 2009
Posts: 28
sql update command from temporary table

I have 3 tables t1, t2, and t3.

Each has a primary key of 'job_no'.

t3 has a field called 'status'.

I wish to update t3.status by setting it to “XXX” when certain conditions are met.

My syntax looks like this :-

select t1.job_no
from t1, t2, t3
where t1.job_no = t2.job_no
and t2.job_no = t3.job_no
and (more conditions)
into temp ttemp;
update t3
set t3.status = “XXX”
where ttemp.job_no = t3.job_no

but I get error :- 522: Table (ttemp) not selected in query

Can anyone advise how to get around this ?

Thanks
Reply With Quote
  #2 (permalink)  
Old 02-15-11, 12:24
john_collins john_collins is offline
Registered User
 
Join Date: Jan 2011
Posts: 14
__________________
SQL Hosting
Reply With Quote
  #3 (permalink)  
Old 02-16-11, 03:52
dmac25 dmac25 is offline
Registered User
 
Join Date: Jul 2009
Posts: 28
sql update command from temporary table

Thanks John
Unfortunately, my requirement has changed since my first post.
Rather than setting t3 status to 'XXX', I now need to set it to the
value of status now being held in the t1 table.
I don't think the examples in "Update using an Inner Join"
will do for my new scenario.
Any further advise would be most grateful !
Reply With Quote
  #4 (permalink)  
Old 02-16-11, 14:05
mjldba mjldba is offline
Registered User
 
Join Date: Dec 2003
Location: North America
Posts: 139
Using your original methodology you could:

select t1.job_no, t1.status
from t1, t2, t3
where t1.job_no = t2.job_no
and t2.job_no = t3.job_no
and (more conditions)
into temp ttemp;

update t3
set t3.status = (select ttemp.status
from ttemp
where ttemp.job_no = t3.job_no)
where t3.jobno in (select ttemp.job_no from ttemp);
Reply With Quote
  #5 (permalink)  
Old 02-17-11, 03:54
dmac25 dmac25 is offline
Registered User
 
Join Date: Jul 2009
Posts: 28
sql update command from temporary table

Hi mjl
Many thanks : your code works great.

Can I ask another question ?
If table t3 has more than one field to be updated from t1,
then I can enhance the 1st part as follows :
Code:
  select  t1.job_no, t1.status, t1.f2, t1.f3 
  from    t1,  t2,  t3
  where  t1.job_no = t2.job_no
  and     t2.job_no = t3.job_no
  and (more conditions)
  into temp ttemp;
But how would the update command look ?

Code:
  update t3
  set t3.status =  (select ttemp.status    from ttemp
                          where ttemp.job_no = t3.job_no)
  set t3.f2       =  (select ttemp.f2         from ttemp
                          where ttemp.job_no = t3.job_no)
  set t3.f3       =  (select ttemp.f3         from ttemp
                          where ttemp.job_no = t3.job_no)
  where t3.jobno in (select ttemp.job_no from ttemp);
doesn't seem to work.

many thanks again.
Reply With Quote
  #6 (permalink)  
Old 02-17-11, 08:41
mjldba mjldba is offline
Registered User
 
Join Date: Dec 2003
Location: North America
Posts: 139
You can have only one "set" in an update statement & the various values are separated with commas. Using your method this MAY work
(untested but inefficient) as long as the "where" condition of the subquery(s) returns only 1 row:

update t3
set t3.status = (select ttemp.status from ttemp
where ttemp.job_no = t3.job_no),
t3.f2 = (select ttemp.f2 from ttemp
where ttemp.job_no = t3.job_no),
t3.f3 = (select ttemp.f3 from ttemp
where ttemp.job_no = t3.job_no)
where t3.jobno in (select ttemp.job_no from ttemp);

================================================

this will work, same "subquery only returns 1 row" rule applies

update t3
set (t3.status, t3.f2, t3.f3) = (select ttemp.status, ttemp.f2, ttemp.f3
from ttemp
where ttemp.jobno = t3.jobno)
where t3.jobno in (select ttemp.job_no from ttemp);

Last edited by mjldba; 02-17-11 at 09:48.
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