我有一个select语句
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
我想检查该语句是否返回0行。我尝试使用ISNULL和IFNULL,但似乎缺少了什么。我有一个select语句
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
我想检查该语句是否返回0行。我尝试使用ISNULL和IFNULL,但似乎缺少了什么。要查找是否存在不匹配的行,可以使用 NOT EXISTS
。这比计算所有匹配行更有效率。
IF NOT EXISTS(SELECT * FROM ...)
BEGIN
PRINT 'No matching row exists'
END
SELECT COUNT(*)
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
--This will return no of rows returned by above statement.
SELECT @@ROWCOUNT
IF @@ROWCOUNT <> 0
PRINT 'Select statement is returning some rows'
ELSE
PRINT 'No rows returned'
尝试这个:
SELECT ISNULL(QBalance, 'ReplaceValue')
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)
也可以使用外部的ISNULL检查吗?
SELECT ISNULL((
SELECT QBalance
FROM dbo.CustomerBalance
WHERE (CustomerID = 1) AND (MarchentID = @MerchantId)), 0)
DECLARE @NULL int SET @NULL= (SELECT COUNT(QBalance) FROM dbo.CustomerBalance WHERE (CustomerID = @CustomerID) AND (MarchentID = @MerchantId)) IF @NULL=0 BEGIN INSERT INTO [CustomerBalance]([CustomerID],[MarchentID],[QBalance],[InitialDate]) VALUES(@CustomerID,@MerchantId,@CodeQPoints,GETDATE()) END ELSE UPDATE [CustomerBalance] SET QBalance=@CodeQPoints+QBalance,InitialDate=GETDATE() WHERE dbo.CustomerBalance.CustomerID=@CustomerID and dbo.CustomerBalance.MarchentID=@MerchantId
- Islam