我的代码能够检索与条形码匹配的书籍代码数据,但在没有数据匹配时则不能.如果没有找到数据,我希望它在单元格中打印“无数据”.我确实按照这个但没有工作
SQL return row if no result found
我确实尝试过这个查询,但仍然有“无数据”返回行
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中尝试了一个一个条码.正如你在这张图片中看到的那样
查询仅打印书签,不包括“未找到书签”.请帮我弄清楚如何解决这个问题
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 Bookcode
from (Select InvtID as InvtID FROM InventoryCustomer WHERE Barcode IN('123','9789830093819')) R
Left 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 end
ELSE
SELECT InvtID From InventoryCustomer WHERE Barcode in ('123','9789830093819')'
4. 'SELECT InvtID, CASE WHEN InvtID is null OR InvtID='' THEN 'no data'
ELSE InvtID
END AS Bookcode
FROM 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 status
FROM cte
WHERE rid = 1;
如果InvtId不是文本列,那么您可能必须按如下方式将其强制转换为文本以使CASE表达式起作用:
CASE WHEN Barcode IN (...)
THEN CONVERT(varchar(10), InvtId) ELSE 'bookcode not found' END