且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如果没有找到数据,Sql server查询将连续返回“无数据”

更新时间:2023-02-09 15:16:59

Maciej解决方案可以工作,但是...这不是一个真正的数据检索功能,它是一个演示功能,在你的工作中这样做会更好,效率更高演示语言。



这很简单:检索行,计算它们,并采取适当的措施。在SQL中执行它会返回可变数量的列,这对于演示来说很麻烦。



Maciej的解决方案将起作用,但这意味着有效地运行查询两次,这两者都是如果需要修改查询,则效率低下并存在风险问题。
Maciej solution will work, but ... this isn't really a data retrieval function, it's a presentation function and it would be both much better and more efficient to do this in your presentation language.

It's simple there: retrieve the rows, count them, and take the appropriate action. Doing it in SQL returns a variable number of columns, which is messy for presentation.

Maciej's solution will work, but it means effectively running the query twice, which is both inefficient and risks problems if the query needs to be modified.


简而言之:您可以使用 IF [ ^ ] + EXISTS [ ^ ]:



In a short: you can use IF[^] + EXISTS[^]:

IF NOT EXISTS
(
    SELECT TOP 1 Barcode FROM [Danny].[dbo].[InventoryCustomer] WHERE Barcode In ('ean','9789830093819'))
) 
   SELECT 'Bookcode not found'
ELSE
    SELECT Your_Complex_Query_Here
END


--Actually Table records is filtered by values but you including filtered values as records.This solution is given based on output,i hope this helpful 

CREATE TABLE #InventoryCustomer(InvtID Varchar(20),Bookcode Varchar(20),BarCode Varchar(50)) 

INSERT INTO #InventoryCustomer
Values
      ('CB0212','CB0212','ean'),
      ('DD2921','DD2921','9789830093819');

--In 2008 server need to create split_function for splitting  values.

select 
  ISNULL(InvtID,'') AS InvtID
 ,ISNULL(Bookcode,'Bookcode not found') AS Bookcode 

   FROM split_fun('1,ean,9789830093819',',') AS Stv LEFT JOIN #InventoryCustomer cte   
       ON( Stv.value=Cte.Barcode)

OUTPUT:
-------------------
InvtID | Bookcode
--------------------
	Bookcode not found
CB0212	CB0212
DD2921	DD2921

>