Welcome to the dBforums forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions, articles and access our other FREE features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload your own photos and access many other special features. Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact contact support.

If you prefer not to see double-underlined words and corresponding ads, place your cursor
here for ContentLink opt out.

Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > SQL (Return the number of row)

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-18-04, 05:48
gop373 gop373 is offline
Registered User
 
Join Date: Aug 2004
Posts: 77
SQL (Return the number of row)

Is it possible to write sql to return the number of row in the table ? If so how ?

For example , table A
id name
21 AA
33 BB
11 CC

From this example , it should return 3 (because 3 rows)
Reply With Quote
  #2 (permalink)  
Old 10-18-04, 06:46
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
select count(*) from a;
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #3 (permalink)  
Old 10-18-04, 08:01
kevin30k kevin30k is offline
Registered User
 
Join Date: Oct 2004
Location: Dublin, Ireland
Posts: 16
Is it possible to return all the data and the number of rows in one select? Something like select *, count(*) from a?
Reply With Quote
  #4 (permalink)  
Old 10-18-04, 08:16
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Yes, if you really must:
Code:
1 select dept.*, (select count(*) from dept) cnt from dept; DEPTNO DNAME LOC CNT ---------- -------------- ------------- ---------- 10 ACCOUNTING NEW YORK 4 20 RESEARCH DALLAS 4 30 SALES CHICAGO 4 40 OPERATIONS BOSTON 4
Is that what you meant?
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #5 (permalink)  
Old 10-18-04, 08:21
kevin30k kevin30k is offline
Registered User
 
Join Date: Oct 2004
Location: Dublin, Ireland
Posts: 16
Hmmm. That works but it looks inefficient. I just need to present a total at the end so a separate select at the end would be better. I am selecting over 2000 rows.

Another possibility might be to use a cursor and count the number of rows returned.

I'm just trying to avoid a second select statement.
Reply With Quote
  #6 (permalink)  
Old 10-18-04, 08:32
andrewst andrewst is offline
Moderator.
 
Join Date: Sep 2002
Location: UK
Posts: 4,874
Yes, it is inefficient. I was trying to give a "pure SQL" solution as this is a "pure SQL" forum. If you want to display a total number of selected records at the end, then probably your DBMS already has a built-in way to do this. For example, in Oracle SQL Plus:

Code:
SQL> select * from dept; DEPTNO DNAME LOC MGR ---------- -------------- ------------- ---------- 10 ACCOUNTING NEW YORK 20 RESEARCH DALLAS 7782 30 SALES CHICAGO 40 OPERATIONS BOSTON 4 rows selected.
And in Oracle PL/SQL:
Code:
SQL> declare 2 cursor c is select * from dept; 3 r c%ROWTYPE; 4 begin 5 open c; 6 loop 7 fetch c into r; 8 exit when c%notfound; 9 end loop; 10 dbms_output.put_line('Number of rows='||c%rowcount); 11 close c; 12 end; 13 / Number of rows=4 PL/SQL procedure successfully completed.
__________________
Tony Andrews
http://tonyandrews.blogspot.com
Reply With Quote
  #7 (permalink)  
Old 10-18-04, 11:07
kevin30k kevin30k is offline
Registered User
 
Join Date: Oct 2004
Location: Dublin, Ireland
Posts: 16
I found an easier solution:
compute count of dname on report;
select * from dept;

I know it does not belong in this forum.
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

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