I have a select statement
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
I want to check if that statement returns 0 rows. I tried to use the ISNULL and IFNULL but it seems that I'm missing something.
You can use @@ROWCOUNT. For e.g. You will get 0 if first statement will not return any rows. You can also use if statement to check that just after first statement.
Your query is returning nothing because the execution engine is using an index that is incorrectly referenced by this specific application (Sage BusinessVision) you have to work around the issue.
The common table expression ( WITH clause) wraps the first query that we want to execute no matter what. We then select from the first query, and use UNION ALL to combine the result with the result of the second query, which we're executing only if the first query didn't yield any results (through NOT EXISTS ).
It is like a Shorthand form of CASE statement. We can conveniently use it when we need to decide between two options. There are three parts in IIF statement, first is a condition, second is a value if the condition is true and the last part is a value if the condition is false.
To find out whether no matching rows exist you can use NOT EXISTS
. Which can be more efficient than counting all matching rows
IF NOT EXISTS(SELECT * FROM ...)
BEGIN
PRINT 'No matching row exists'
END
If this is SQL Server, try @@ROWCOUNT.
SELECT COUNT(*)
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
If you get 0, you got 0. :)
You can use @@ROWCOUNT. For e.g.
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
--This will return no of rows returned by above statement.
SELECT @@ROWCOUNT
You will get 0 if first statement will not return any rows. You can also use if statement to check that just after first statement. e.g.
IF @@ROWCOUNT <> 0
PRINT 'Select statement is returning some rows'
ELSE
PRINT 'No rows returned'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With