我确实尝试过这个查询,但仍然有“无数据”返回行
WITH CTE AS( SELECT *,ROW_NUMBER() OVER(PARTITION BY barcode ORDER BY InvtID Asc) rID FROM InventoryCustomer) SELECT InvtID,coalesce(InvtID,'bookcode not found') FROM cte WHERE rID=1 and barcode In ("<barcode_value>")
代码如下,
using (sqlConnection conn = new sqlConnection("Server=")) using (sqlCommand cmd = new sqlCommand(null,conn)) { StringBuilder sb = new StringBuilder("WITH cte AS(SELECT *,ROW_NUMBER() OVER(PARTITION BY barcode ORDER BY InvtID Asc) rID FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rID=1 and barcode In ("); for (int i = 0; i < dt.Rows.Count; i++) { if (i != 0) sb.Append(","); string name = "@P" + i; cmd.Parameters.AdDWithValue(name,dt.Rows[i]["3"]); sb.Append(name); } sb.Append(") ORDER BY barcode"); cmd.CommandText = sb.ToString(); sqlDataAdapter da = new sqlDataAdapter(cmd); da.Fill(dt); dt.Columns["BOOKCODE"].Setordinal(0); dataGrIDVIEw2.DataSource = dt; }
}
更新
我在sql server中尝试了一个一个条码.正如你在这张图片
中看到的那样当只有1个参数查询能够打印’未找到书签’但第二张图片
查询仅打印书签,不包括“未找到书签”.请帮我弄清楚如何解决这个问题
WITH cte AS(SELECT *,ROW_NUMBER() OVER(PARTITION BY barcode ORDER BY InvtID Asc) rID FROM [dbo].[InventoryCustomer] WHERE barcode In ('123','9789830093826') ) 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 )
2月11日
这些是我尝试过的所有查询,但如果在sql中找不到行,它们都不会显示“无数据”
1. 'SELECT isnull((SELECT InvtID FROM InventoryCustomer WHERE barcode IN('123','9789830093819')),'No bookcode found') AS InvtID '2. 'Select case when s.InvtID IS NOT NulL Then s.InvtID else 'no data' end as Bookcodefrom (Select InvtID as InvtID FROM InventoryCustomer WHERE barcode IN('123','9789830093819')) Rleft Join InventoryCustomer s ON s.InvtID = R.InvtID'3. 'IF NOT EXISTS (Select InvtID From InventoryCustomer WHERE barcode in ('123','9789830093819')) Begin SELECT 'Bookcode not found' as Bookcode endELSESELECT InvtID From InventoryCustomer WHERE barcode in ('123','9789830093819')'4. 'SELECT InvtID,CASE WHEN InvtID is null OR InvtID='' THEN 'no data'ELSE InvtIDEND AS BookcodeFROM InventoryCustomer WHERE barcode in ('123','9789830093819')'5. 'WITH cte AS(SELECT case WHEN InvtID IS NulL OR InvtID='' THEN 'No Bookcode Found' ELSE InvtID END AS InvtID,barcode,ROW_NUMBER() OVER(PARTITION BY barcode ORDER BY InvtID Asc) rID FROM InventoryCustomer) SELECT InvtID AS BOOKCODE FROM cte WHERE rID=1 and barcode In ('123','9789830093819')'解决方法 您可以尝试删除WHERE子句中的条形码检查(限制记录),而是使用具有相同逻辑的CASE表达式.遇到不匹配的记录时,您可能会显示未找到的书签:
WITH cte AS ( SELECT *,ROW_NUMBER() OVER(PARTITION BY barcode ORDER BY InvtID Asc) rID FROM InventoryCustomer)SELECT InvtID,CASE WHEN barcode IN (...) THEN InvtID ELSE 'bookcode not found' END AS statusFROM cteWHERE rID = 1;
如果InvtID不是文本列,那么您可能必须按如下方式将其强制转换为文本以使CASE表达式起作用:
CASE WHEN barcode IN (...) THEN CONVERT(varchar(10),InvtID) ELSE 'bookcode not found' END总结
以上是内存溢出为你收集整理的c# – 如果未找到数据,SQL Server查询将连续返回“无数据”全部内容,希望文章能够帮你解决c# – 如果未找到数据,SQL Server查询将连续返回“无数据”所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)