如果列表sql server中不存在数据,则返回值
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如果列表sql server中不存在数据,则返回值相关的知识,希望对你有一定的参考价值。
我有一个条形码列表,想要以Bookcode作为列来获取InvtID。我想如果没有条形码存在,那么它将在列表中返回“无数据”。这是我当前的查询,但它仅显示已存在条形码的列表。
> WITH cte AS (SELECT *, ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY
InvtID Asc) rid FROM InvtCust WHERE Barcode In('123','9789830093697','9789830093727',)) SELECT InvtID,
BOOKCODE = coalesce(InvtID, 'Bookcode not found') FROM cte WHERE rid =
1 UNION SELECT InvtID = '', BOOKCODE = 'Bookcode not found' WHERE NOT
EXISTS(SELECT 1 FROM CTE)
我知道这个问题有很多问题。但我真的无法解决我的问题。我试过这些没有工作。
SELECT isnull((SELECT [InvtID]
FROM InvtCust WHERE Barcode IN('123','9789830093819')),'No bookcode found')
AS InvtID
Select case when s.InvtID IS NOT NULL Then s.InvtID else 'no data' end as Bookcode
from (Select InvtID as InvtID FROM InvtCust WHERE Barcode IN('123')) R
Left Join InvtCust s ON s.InvtID = R.InvtID
select expr1, InvtID from (
select *,ROW_NUMBER() over(order by isrealrow asc) rownum from (
select COUNT(*) as expr1, InvtID, 1 as isrealrow from [DANNY].[dbo].[InventoryCustomer]
where Barcode in ('ean','9789830093819','9789830093826','9789830094205')
Group by InvtID
union select 0,'No bookcode found',0 as isrealrow
)b
)c
where isrealrow=1 or rownum=1
IF NOT EXISTS (Select InvtID From InvtCust WHERE Barcode in ('123'))
Begin SELECT 'Bookcode not found' as Bookcode end
ELSE
SELECT InvtID From InvtCust WHERE Barcode in ('123')
答案
在我看来,最好将所有逻辑放在存储过程中:
CREATE PROCEDURE YourProcedure
AS
IF OBJECT_ID('tempdb..#Results') IS NOT NULL DROP TABLE #Results
create table #Results
(
InvtID INT,
BookCode VARCHAR(255)
)
WITH cte AS (
SELECT
*
, ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid
FROM InvtCust
WHERE Barcode In('123','9789830093697','9789830093727'))
INSERT INTO #Results
(
InvtID,
BookCode
)
SELECT InvtID,
BOOKCODE = coalesce(InvtID, 'Bookcode not found')
FROM cte WHERE rid = 1
UNION
SELECT
InvtID = ''
, BOOKCODE = 'Bookcode not found'
WHERE NOT
EXISTS(SELECT 1 FROM CTE)
DECLARE @rows INT = 0;
SELECT @rows = COUNT(1) FROM #Results
SELECT @rows
IF @rows > 0
BEGIN
SELECT
*
#Results
END
ELSE
SELECT 'No Data'
GO
另一答案
此查询将为您提供所需的结果。假设您要在表格中搜索逗号分隔的条形码列表,如果查询将在表格中找到该条形码,它将返回条形码以及InvtID否则它将返回该记录的“未找到条形码”而不是InvtID。
对于SQL Server 2016或更高版本
with BarcodeList as
(
select value as barcode
from string_split('aaaa,bbbb,cccc' , ',')
)
Select isnull(cast(InvtID as varchar), 'No barcode found') InvtID, b.Barcode From BarcodeList b left outer join InvtCust i
on b.barcode = i.barcode
注意:SQL Server 2016及更高版本支持此查询中使用的string_split函数。如果您使用2016年以下的SQL服务器,则应创建并使用自定义拆分功能,如下所示。
对于SQL Server 2016以下
自定义分割功能
create FUNCTION [dbo].[String_Split]
(
@string NVARCHAR(MAX),
@delimiter CHAR(1)
)
RETURNS @output TABLE(value NVARCHAR(MAX)
)
BEGIN
DECLARE @start INT, @end INT
SELECT @start = 1, @end = CHARINDEX(@delimiter, @string)
WHILE @start < LEN(@string) + 1 BEGIN
IF @end = 0
SET @end = LEN(@string) + 1
INSERT INTO @output (value)
VALUES(SUBSTRING(@string, @start, @end - @start))
SET @start = @end + 1
SET @end = CHARINDEX(@delimiter, @string, @start)
END
RETURN
END
Go
实际查询
with BarcodeList as
(
select value as barcode
from dbo.[String_Split]('aaaa,bbbb,cccc' , ',')
)
Select isnull(cast(InvtID as varchar), 'No barcode found') InvtID, b.Barcode From BarcodeList b left outer join InvtCust i
on b.barcode = i.barcode
以上是关于如果列表sql server中不存在数据,则返回值的主要内容,如果未能解决你的问题,请参考以下文章
SQL Server 中的存储过程,它采用表值参数并插入不存在的并返回存在的
即使 sql developer 中不存在数据,也返回给定员工的行