473,385 Members | 1,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Count function

I have a table which has a million records. Each night it get trashed and
re-built.

I want to run a select statment and pass the results into a variable to let
me know if it contains more than 0 records.

I don't want to do this because it takes to long to perform the count.

declare @counttable int
select @counttable = count(*) from [tablename]

Is there a quicker way to do what I want to achieve
May 26 '06 #1
5 1410

Try this

SELECT 'Primary Key' AS IndexName, [rows]
FROM sysindexes
WHERE (id = OBJECT_ID('<Table_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
I have a table which has a million records. Each night it get trashed and
re-built.

I want to run a select statment and pass the results into a variable to
let
me know if it contains more than 0 records.

I don't want to do this because it takes to long to perform the count.

declare @counttable int
select @counttable = count(*) from [tablename]

Is there a quicker way to do what I want to achieve

May 26 '06 #2
Is there no simple way to check for the exist of rows in a table?

"AMDRIT" wrote:

Try this

SELECT 'Primary Key' AS IndexName, [rows]
FROM sysindexes
WHERE (id = OBJECT_ID('<Table_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
I have a table which has a million records. Each night it get trashed and
re-built.

I want to run a select statment and pass the results into a variable to
let
me know if it contains more than 0 records.

I don't want to do this because it takes to long to perform the count.

declare @counttable int
select @counttable = count(*) from [tablename]

Is there a quicker way to do what I want to achieve


May 26 '06 #3
Since you only care if there are greater than 0 records, you can just
select the top record:

Select Top 1 * From [tablename]

That should be fairly quick. And if you get a record, you know there
is more than 0. As for getting the actual count, I think AMDRIT's
solution is the one. I don't know how much more simple you want it
than that!

May 26 '06 #4
I think that is the simplest way. That query should be very fast. As you
insert into a table, indexes are recomputed and row count is cached for each
row. Since cluster indexes and pk indexes represent all the data, you will
know the number is good. The query I gave should be the fastest way to
determine if there are 1 or more rows.
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.com...
Is there no simple way to check for the exist of rows in a table?

"AMDRIT" wrote:

Try this

SELECT 'Primary Key' AS IndexName, [rows]
FROM sysindexes
WHERE (id = OBJECT_ID('<Table_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
>I have a table which has a million records. Each night it get trashed
>and
> re-built.
>
> I want to run a select statment and pass the results into a variable to
> let
> me know if it contains more than 0 records.
>
> I don't want to do this because it takes to long to perform the count.
>
> declare @counttable int
> select @counttable = count(*) from [tablename]
>
> Is there a quicker way to do what I want to achieve


May 26 '06 #5
This sort of approach assumes that the table in question actually has a
primary key and also that statistics are being updated automatically.

The OP say's that the table in question is 'trashed and re-built'. This
indicates (to me at least) that all the existing rows in the table are being
deleted in some way or other and that new rows are being inserted.

The inserts generate information as the number of rows affected. If any of
the inserts report that 1 or more rows were affected then the table in
question contains more than 0 rows.
"AMDRIT" <am****@hotmail.com> wrote in message
news:Ok**************@TK2MSFTNGP03.phx.gbl...
I think that is the simplest way. That query should be very fast. As you
insert into a table, indexes are recomputed and row count is cached for
each row. Since cluster indexes and pk indexes represent all the data, you
will know the number is good. The query I gave should be the fastest way
to determine if there are 1 or more rows.
"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:2F**********************************@microsof t.com...
Is there no simple way to check for the exist of rows in a table?

"AMDRIT" wrote:

Try this

SELECT 'Primary Key' AS IndexName, [rows]
FROM sysindexes
WHERE (id = OBJECT_ID('<Table_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discussions.microsoft.com> wrote in message
news:78**********************************@microsof t.com...
>I have a table which has a million records. Each night it get trashed
>and
> re-built.
>
> I want to run a select statment and pass the results into a variable
> to
> let
> me know if it contains more than 0 records.
>
> I don't want to do this because it takes to long to perform the count.
>
> declare @counttable int
> select @counttable = count(*) from [tablename]
>
> Is there a quicker way to do what I want to achieve


May 27 '06 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Terry E Dow | last post by:
Howdy, I am having trouble with the objectCategory=group member.Count attribute. I get one of three counts, a number between 1-999, no member (does not contain member property), or 0. Using...
3
by: jason | last post by:
Hello. I've got this simple collection populate code I downloaded from the net (sorry can't find source now) I'm trying to test, but I can't seem to get it to work. Any help would be greatly...
10
by: Jon | last post by:
I want to count the number of instances of a certain string(delimiter) in another string. I didn't see a function to do this in the framework (if there is, please point me to it). If not, could...
3
by: Kuups | last post by:
Hi! I have a question regarding the count if character within a string like for example I have a string of e.g. 123#123# I would like to determine what is the code? of getting the # sign
68
by: Martin Joergensen | last post by:
Hi, I have some files which has the following content: 0 0 0 0 0 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 1 1 1 1 0 0 0 0 0 0 0
23
by: Gary Wessle | last post by:
Hi I have a vector<charwhich looks like this (a d d d a d s g e d d d d d k) I need to get the biggest count of consecutive 'd'. 5 in this example I am toying with this method but not sure if...
2
by: Volkan | last post by:
Hi, I'm trying to compare two XML documents and i'm using XPath queries to select nodes. XPathNavigator's Select method runs fast enough and returns an XPathNodeIterator object. When i try to...
2
by: DrewKull | last post by:
Ok ... so I'm looking at a query where im trying to show a bunch of columns counted up based on criteria ... So far i've been able to count all the rows up and show them ... and also count up...
22
by: MP | last post by:
vb6,ado,mdb,win2k i pass the sql string to the .Execute method on the open connection to Table_Name(const) db table fwiw (the connection opened via class wrapper:) msConnString = "Data Source="...
26
by: Ping | last post by:
Hi, I'm wondering if it is useful to extend the count() method of a list to accept a callable object? What it does should be quite intuitive: count the number of items that the callable returns...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.