473,830 Members | 1,964 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 1432

Try this

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

This will give the rowcount based on the primary key.

"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:78******** *************** ***********@mic rosoft.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('<Tab le_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:78******** *************** ***********@mic rosoft.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*****@discus sions.microsoft .com> wrote in message
news:2F******** *************** ***********@mic rosoft.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('<Tab le_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:78******** *************** ***********@mic rosoft.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******** ******@TK2MSFTN GP03.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*****@discus sions.microsoft .com> wrote in message
news:2F******** *************** ***********@mic rosoft.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('<Tab le_name>')) AND (status & 2048 <> 0)

This will give the rowcount based on the primary key.

"Stephen" <St*****@discus sions.microsoft .com> wrote in message
news:78******** *************** ***********@mic rosoft.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
10852
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 LDIFDE as a comparison I get the same results. No members means just that, an empty group. Zero means that the DirectorySearcher.SizeLimit has been exceeded....
3
4588
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 appreciated. I've compiled the following VB.NET into a DLL: Imports System Imports System.Data Imports System.Data.SqlClient Imports System.Collections
10
2861
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 someone let me know if the method I've used below is efficient or if there is a better way to do it, as these will be rather large strings I'm searching in. Thanks Public Shared Function CountDelimiter(ByVal strInput As String, ByVal...
3
26230
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
6843
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
2908
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 it is optimal. thanks int k = 0;
2
3826
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 access this iterator's Count property the process extremely slows down. If i just iterate throgh 18.000 nodes and call XPathNavigator.Select to find equivalent node from the other document it doesn't even take 1 second. But in the same loop, when i...
2
2598
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 one column based on the criteria (value = yes) ... but not both at the same time ... When i try it with all of them, all of them are either the same number or no numbers at all ...
22
12505
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=" & msDbFilename moConn.Properties("Persist Security Info") = False moConn.ConnectionString = msConnString moConn.CursorLocation = adUseClient moConn.Mode = adModeReadWrite' or using default...same result
26
2467
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 True or anything logically equivalent (non-empty sequence, non-zero number, etc). This would return the same result as len(filter(a_callable, a_list)), but without constructing an intermediate list which is thrown away after len() is done.
0
9780
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10769
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
10520
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10196
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9310
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5775
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4408
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3956
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
3070
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.