SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 count the row number
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

allan8964
Posting Yak Master

249 Posts

Posted - 06/11/2013 :  12:38:45  Show Profile  Reply with Quote
Hi there,

I need count row number in a column. I know I can use @@RowCount but I need run select to make it work. But at the end I need run another select statement to feed the results to dataset in a report. But the dataset takes the first select results. So my question is how can I count the row without using select. Thanks in advance.

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 06/11/2013 :  12:46:46  Show Profile  Reply with Quote
do you mean this?


SELECT COUNT(1) OVER () as RowCount,*
FROM Table1
...
--your actual query


------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 06/11/2013 :  13:01:10  Show Profile  Reply with Quote
I am not following ... not working anyway.
I need a way to count the row without using SELECT.
Go to Top of Page

James K
Flowing Fount of Yak Knowledge

3712 Posts

Posted - 06/11/2013 :  13:06:21  Show Profile  Reply with Quote
Isn't what you really need a way to count the rows so it does not generate a recordset?
SET NOCOUNT ON;
DECLARE @Rows INT;
SELECT @Rows = COUNT(*) FROM Table1
You could also use the following, which is faster:
SET NOCOUNT ON;
DECLARE @Rows INT;

SELECT @Rows = SUM (row_count) 
FROM sys.dm_db_partition_stats 
where  (index_id=0 or index_id=1)
AND object_name(object_id) = 'Table1'
Go to Top of Page

Lamprey
Flowing Fount of Yak Knowledge

4614 Posts

Posted - 06/11/2013 :  17:03:52  Show Profile  Reply with Quote
quote:
Originally posted by allan8964

<snip> So my question is how can I count the row without using select. Thanks in advance.

You can't
Go to Top of Page

allan8964
Posting Yak Master

249 Posts

Posted - 06/11/2013 :  21:47:03  Show Profile  Reply with Quote
Thanks James, the last piece works!!!
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52323 Posts

Posted - 06/12/2013 :  01:03:40  Show Profile  Reply with Quote
quote:
Originally posted by allan8964

Thanks James, the last piece works!!!



that still uses a SELECT isnt it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs
Go to Top of Page

neeraj_sharma
Starting Member

India
15 Posts

Posted - 06/12/2013 :  03:04:50  Show Profile  Reply with Quote
quote:
Originally posted by visakh16

quote:
Originally posted by allan8964

Thanks James, the last piece works!!!



that still uses a SELECT isnt it?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/
https://www.facebook.com/VmBlogs





Haha i this Allan just wanted to ask i want it in a variable,
noting wrong with your Answer you always does good work here.


BLOG:
http://www.tutorialsqlserver.com






@nrj
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000