niru dyogi
niru dyogi

Reputation: 639

How to get number of rows in a SQL Server 2008 query

How to get number of rows with a SQL query?

I tried this :

SET @Records = count(*) SELECT * FROM  [INVENTORY].[TBL_RECEIPTS] 
                WHERE Field_Tag = '1234'

But it is giving 1. What is wrong with the above code?

Actually there was no row, it should give the output 0

When I execute this

SELECT * FROM  [INVENTORY].[TBL_RECEIPTS] 
WHERE Field_Tag = '1234'

it is giving result 0

Upvotes: 0

Views: 742

Answers (3)

codingbiz
codingbiz

Reputation: 26386

Do these - either should work, but I prefer the second code

SET @Records = (SELECT Count(*) FROM  [INVENTORY].[TBL_RECEIPTS] 
                WHERE Field_Tag = '1234')

OR

SELECT @Records = Count(*) FROM  [INVENTORY].[TBL_RECEIPTS] 
                WHERE Field_Tag = '1234'

Upvotes: 1

Linus Caldwell
Linus Caldwell

Reputation: 11058

Use

SET @Records = (SELECT count(*)
                  FROM [INVENTORY].[TBL_RECEIPTS]
                 WHERE Field_Tag = '1234')

Upvotes: 1

anon
anon

Reputation:

SELECT @Records = COUNT(*) FROM INVENTORY.TBL_RECEIPTS
   WHERE Field_Tag = '1234';

Upvotes: 1

Related Questions