如果SQL中没有记录匹配,如何返回零 [英] How can I return zero if no record is match in SQL
本文介绍了如果SQL中没有记录匹配,如何返回零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨!!!!!
i尝试获取产品名称为笔记本电脑的最大ID
如果根据产品名称找到ID,则返回ID
但我想如果根据产品名称找不到ID那么sql返回零
请任何人告诉我该怎么做。
我正在使用代码,但可能不是很好
我尝试了什么:
hi!!!!!
i try to get maximum ID where product name is 'Laptop'
if it find ID according to product name then it will return ID
but i want if no ID is find according to product name then sql return zero
please any one tell me how can i do this.
I'm using a code but may be it is not fine
What I have tried:
ALTER PROCEDURE [dbo].[USP_Select_Current_PurchaseItem_QtyPurRate_BAseOnItemName]
(
@ItemName nvarchar(50)
)
AS
BEGIN
SELECT
max(isnull,([Id]),0) as 'CurrentlyaddeItemId'
FROM [dbo].[Purchasing_Items_Hist]
where Itemtype=@ItemName
END
推荐答案
你有正确的想法,只是错误的语法。而不是IsNull我相信你应该使用COALESCE。
你想知道MAX(id)何时为空。这样做:
You have the right idea, just the wrong syntax. And instead of IsNull I believe you should be using COALESCE.
You want to know when MAX(id) is null. Do this:
SELECT
COALESCE(max([Id]), 0) as 'CurrentlyaddeItemId'
FROM [dbo].[Purchasing_Items_Hist]
where Itemtype=@ItemName
这篇关于如果SQL中没有记录匹配,如何返回零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文