如果列表SQL Server中不存在数据,则返回值

时间:2019-02-25 09:14:40

标签: 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') 

2 个答案:

答案 0 :(得分:0)

在我看来,最好将所有逻辑放入存储过程中

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

答案 1 :(得分:0)

此查询将为您提供所需的结果。假设您要在表中搜索要用逗号分隔的条形码列表,如果查询在表中找到该条形码,它将返回该条形码以及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

注意:此查询中使用的string_split函数在SQL Server 2016及更高版本中受支持。如果您在2016年以下使用SQL Server,则应创建并使用如下所示的自定义拆分功能。

对于Under 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