Geeks Talk

Prepare for your Next Interview




subquery

This is a discussion on subquery within the SQL forums, part of the Databases category; How to retrive first 5 records, after that update function to be used for those records(first 5 records) using subquery. for 1 record update sal+200 for 2 record ...


Go Back   Geeks Talk > Databases > SQL

Register FAQ Members List Calendar Mark Forums Read
  #1 (permalink)  
Old 06-30-2008
Junior Member
 
Join Date: Jun 2008
Location: hyderabad
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
jaladurgam3127 is on a distinguished road
subquery

How to retrive first 5 records, after that update function to be used for those records(first 5 records) using subquery.
for 1 record update sal+200
for 2 record update sal+220
for 3 record update sal+250
for 4th record update sal+300
for 5th record update sal+400

it is possible execute with use of subqueries???

Please help me in this regard.
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 07-01-2008
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 64 Times in 63 Posts
krishnaindia2007 is on a distinguished road
Re: subquery

Code:
UPDATE tablename 
SET    sal   = sal + 200
WHERE  empno = ( SELECT empno 
                 FROM (SELECT rownum rn, num  
                       FROM tablename)
                 WHERE rn =1);
Reply With Quote
The Following User Says Thank You to krishnaindia2007 For This Useful Post:
  #3 (permalink)  
Old 07-01-2008
Junior Member
 
Join Date: Jun 2008
Location: hyderabad
Posts: 7
Thanks: 1
Thanked 0 Times in 0 Posts
jaladurgam3127 is on a distinguished road
Re: subquery

krishna anna,
chala thanks.
Update for 5 records i have to run this query 5 times ante kada anna.

Ravi
Srikalahasti.
Reply With Quote
  #4 (permalink)  
Old 07-01-2008
Expert Member
 
Join Date: Sep 2007
Posts: 754
Thanks: 22
Thanked 64 Times in 63 Posts
krishnaindia2007 is on a distinguished road
Re: subquery

As per your sample data you can't update it in a single statement.
Reply With Quote
  #5 (permalink)  
Old 07-17-2008
Junior Member
 
Join Date: Apr 2008
Location: Hyderabad
Posts: 28
Thanks: 1
Thanked 4 Times in 2 Posts
nageshkota is on a distinguished road
Re: subquery

Hi,

Guys did you checked the Code, Is this Code works?
In my knowledge it will fail.
Reply With Quote
  #6 (permalink)  
Old 07-18-2008
Contributing Member
 
Join Date: Sep 2007
Location: hyderabad
Posts: 37
Thanks: 5
Thanked 2 Times in 2 Posts
ashalalaxmi2002 is on a distinguished road
Re: subquery

below pl/sql block can update sal of first five records.

declare
cursor c1 is select rownum,a.* from emp a where rownum<6;
c1row c1%rowtype;
begin
open c1;
loop
fetch c1 into c1row;
exit when c1%notfound;
if c1row.rownum=1 then
update emp set sal=sal+200 where empno=c1row.empno;
elsif c1row.rownum=2 then
update emp set sal=sal+220 where empno=c1row.empno;
elsif c1row.rownum=3 then
update emp set sal=sal+250 where empno=c1row.empno;
elsif c1row.rownum=4 then
update emp set sal=sal+300 where empno=c1row.empno;
elsif c1row.rownum=5 then
update emp set sal=sal+400 where empno=c1row.empno;
end if;
end loop;
close c1;
end;
/
Reply With Quote
Reply

  Geeks Talk > Databases > SQL


Thread Tools
Display Modes


Similar Threads

Thread Thread Starter Forum Replies Last Post
SubQuery chitrab SQL Server 3 05-24-2007 06:18 AM
Join Query without using NOT IN subquery Geek_Guest SQL 1 03-07-2007 04:24 PM


All times are GMT -4. The time now is 02:33 AM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Copyright © 2008 GeekInterview.com. All Rights Reserved