我的代码能够检索与条形码匹配的书籍代码数据,但是在没有数据匹配时无法检索。如果没有找到数据,我也希望它在单元格中打印“无数据”。我确实遵循了此规定,但没有工作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中尝试了一对一的条形码。如您在这张图片
中所看到的当只有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 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')'
答案 0 :(得分:1)
您可以尝试删除WHERE
子句(限制记录)中的条形码检查,而可以使用具有相同逻辑的CASE
表达式。遇到不匹配的记录时,您可能会显示bookcode not found
:
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
答案 1 :(得分:0)
根据您的代码,如果没有找到与条形码匹配的行,则不会返回任何记录,因此,只需添加一个虚拟选择即可在没有匹配的行的情况下返回一行
尝试一下
WITH cte AS
(
SELECT
*,
ROW_NUMBER() OVER(PARTITION BY Barcode ORDER BY InvtID Asc) rid
FROM InventoryCustomer
WHERE Barcode In ("<barcode_value>")
)
SELECT
InvtID,
MyMessage = coalesce(InvtID, 'bookcode not found')
FROM cte
WHERE rid=1
UNION
SELECT
InvtID = NULL,
MyMessage = 'bookcode not found'
WHERE NOT EXISTS
(
SELECT 1 FROM CTE
)