提问人:Chris 提问时间:11/17/2023 最后编辑:David Browne - MicrosoftChris 更新时间:11/18/2023 访问量:57
从 T-SQL 存储过程检索到 C# DataTable 中的数据是倾斜的
Data retrieved from T-SQL stored procedure into C# DataTable are skewed
问:
当我将数据从 T-SQL 存储过程检索到 C# DataTable 中时,我确实遇到了一个奇怪的问题,我真的不明白。我在 T-SQL 中得到的结果与检索数据后 DataTable 显示的结果不同。不同的数据意味着 DataTable 中的单元格要么缺少行,要么显示不同的数据。
这是我在 C 中使用的代码#
public DataTable GetData()
{
string nameOfSp = "sp_SIA_Get_Ratio_Demand_Receipt";
DataAccess dataAccess = new DataAccess();
DataTable dt = new DataTable();
dataAccess.OpenDatabase(DataAccess.ConnectionTo.SQL);
SqlConnection sqlCon = dataAccess.GetSqlConnection();
try
{
SqlCommand cmd = new SqlCommand(nameOfSp, sqlCon);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 1000;
cmd.Parameters.Add("@COMPANY", SqlDbType.VarChar).Value = filter.CompanyNo;
cmd.Parameters.Add("@YEARWEEK_FROM", SqlDbType.VarChar).Value = filter.YearWeekFrom;
cmd.Parameters.Add("@YEARWEEK_TO", SqlDbType.VarChar).Value = filter.YearWeekTo;
cmd.Parameters.Add("@USERNAME", SqlDbType.VarChar).Value = filter.UserName;
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
}
catch (Exception ex)
{
MessageBox.Show("Could not retrieve data from stored procedure " + ex.Message);
}
dataAccess.CloseDatabase();
return dt;
}
下面是 SQL 语句:
ALTER PROCEDURE [dbo].[sp_SIA_Get_Ratio_Demand_Receipt]
@COMPANY as varchar(4),
@USERNAME as varchar(50),
@YEARWEEK_FROM as varchar(6),
@YEARWEEK_TO as varchar(6)
AS
BEGIN
SET NOCOUNT ON;
WITH #MATERIAL (COMPANY_NO, MATERIAL_NO, STORAGE_LOCATION, INVENTORY_QTY, INVENTORY_VAL) AS
(
SELECT
p.COMPANY_NO, p.ITEM_NO, p.STORAGE_LOCATION,
SUM(STOCK_QUANTITY), SUM(STOCK_VALUE)
FROM
tbl_SIA_Sel_PartNos p
INNER JOIN
[FUM_DWH].[dbo].[View_SCM_FACT_610_WAREHOUSE_STOCK] s ON p.ITEM_NO = s.ITEM_NO
AND p.COMPANY_NO = s.COMPANY_NO
AND p.STORAGE_LOCATION = s.STORAGE_LOCATION
WHERE
p.USERNAME = @USERNAME
AND s.YEARWEEK =@YEARWEEK_TO
AND p.ITEM_NO IN( '50048348') --p.ITEM_NO IN( '50057896','50048348','50293653','50059293')
GROUP BY
p.COMPANY_NO, p.ITEM_NO, p.STORAGE_LOCATION
)
,#DEMAND (COMPANY_NO, MATERIAL_NO, STORAGE_LOCATION, DEMAND_QTY) AS
(
SELECT
STRI_COMPANY_NO,
STRI_ITEM_NO,
STRI_STORAGE_LOCATION,
SUM(STRI_QUANTITY * -1) AS DEMAND_QTY
FROM
#MATERIAL m
INNER JOIN
[FUM_DWH].[dbo].[View_SCM_FACT_620_PAST_STOCK_RECEIPT_ISSUE] p ON m.MATERIAL_NO = p.STRI_ITEM_NO
AND p.STRI_STORAGE_LOCATION = m.STORAGE_LOCATION
WHERE
STRI_COMPANY_NO = @COMPANY
AND CONCAT(STRI_YEAR, FORMAT( STRI_WEEK, '00')) BETWEEN @YEARWEEK_FROM AND @YEARWEEK_TO
AND STRI_DEBIT_CREDIT = 'H' --nur Abgänge werten
GROUP BY
STRI_ITEM_NO, STRI_COMPANY_NO, STRI_STORAGE_LOCATION
)
, #RECEIPT(COMPANY_NO, MATERIAL_NO, STORAGE_LOCATION, RECEIPT_QTY) AS
(
SELECT
STRI_COMPANY_NO,
STRI_ITEM_NO,
STRI_STORAGE_LOCATION,
SUM(STRI_QUANTITY) AS RECEIPT_QTY
FROM
[FUM_DWH].[dbo].[View_SCM_FACT_620_PAST_STOCK_RECEIPT_ISSUE] p
INNER JOIN
#MATERIAL m ON p.STRI_ITEM_NO = m.MATERIAL_NO
AND p.STRI_STORAGE_LOCATION = m.STORAGE_LOCATION
WHERE
STRI_COMPANY_NO = @COMPANY
AND CONCAT(STRI_YEAR, FORMAT(STRI_WEEK, '00')) BETWEEN @YEARWEEK_FROM AND @YEARWEEK_TO
AND STRI_DEBIT_CREDIT = 'S' --nur Zugänge werten
GROUP BY
STRI_ITEM_NO, STRI_COMPANY_NO,STRI_STORAGE_LOCATION
)
,#DEMAND_FUT (MATERIAL_NO, STORAGE_LOCATION, DEMAND_QTY, DEMAND_VAL) AS
(
SELECT
[ITEM_NO], s.STORAGE_LOCATION,
SUM([QUANTITY]) * -1 AS DEMAND_QTY,
SUM([VALUE]) * -1 AS DEMAND_VAL
FROM
[FUM_DWH].[dbo].[View_SCM_FACT_630_FUTURE_STOCK_RECEIPT_ISSUE] s
INNER JOIN
#MATERIAL m ON m.MATERIAL_NO = s.ITEM_NO
AND m.STORAGE_LOCATION = s.STORAGE_LOCATION
WHERE
s.COMPANY_NO = @COMPANY
AND TRANSACTION_DATE BETWEEN GETDATE() AND DATEADD(day, 31, GETDATE())
GROUP BY
ITEM_NO, s.STORAGE_LOCATION
)
SELECT
m.MATERIAL_NO,
ISNULL(SUM(d.DEMAND_QTY), 1) AS PST_DEMAND_QTY,
ISNULL(SUM(r.RECEIPT_QTY), 1) AS PST_RECEIPT_QTY,
ISNULL(SUM(r.RECEIPT_QTY), 1) / ISNULL(SUM(d.DEMAND_QTY), 1) AS RATIO,
SUM(m.INVENTORY_QTY) AS INVENTORY_QTY,
SUM(m.INVENTORY_VAL) AS INVENTORY_VAL,
ISNULL(SUM(f.DEMAND_QTY), 0) AS FUT_DEMAND_QTY,
ISNULL(SUM(f.DEMAND_VAL), 0) AS FUT_DEMAND_VAL,
ISNULL(SUM(m.INVENTORY_QTY) - SUM(f.DEMAND_QTY), 0) AS RMN_INVENTORY_QTY,
ISNULL(SUM(m.INVENTORY_VAL) - SUM(f.DEMAND_VAL), 0) AS RMN_INVENTORY_VAL
FROM
#MATERIAL m
LEFT JOIN
#DEMAND d ON m.MATERIAL_NO = d.MATERIAL_NO
AND m.STORAGE_LOCATION = d.STORAGE_LOCATION
LEFT JOIN
#RECEIPT r ON m.MATERIAL_NO = r.MATERIAL_NO
AND m.STORAGE_LOCATION = r.STORAGE_LOCATION
LEFT JOIN
#DEMAND_FUT f ON m.MATERIAL_NO = f.MATERIAL_NO
AND m.STORAGE_LOCATION = f.STORAGE_LOCATION
GROUP BY
m.MATERIAL_NO
ORDER BY
m.MATERIAL_NO -- ISNULL(SUM(r.RECEIPT_QTY), 1) / ISNULL(SUM(d.DEMAND_QTY), 1) DESC
END
我从SQL语句中得到的结果:
我从 DataTable 中得到的结果:
我将 C# 中的代码从异步改回,因为我认为这会解决问题。
答:
0赞
Bart McEndree
11/17/2023
#1
您正在调用名为 sp_SIA_Get_Ratio_Demand_Receipt WHERE 子句导致差异的存储过程。若要查看发送到 SQL Server 的内容,可以使用探查器(如 SQL Server 探查器)。
评论
0赞
Chris
11/17/2023
是的,你是对的,我刚刚展示了 Select 部分,但现在已使用整个存储过程语句更新了我的问题。
0赞
Bart McEndree
11/17/2023
现在我们需要知道 COMPANY、USERNAME、YEARWEEK_FROM 和 YEARWEEK_TO 的过滤器值。您的 SQL 命令将类似于 EXEC sp_SIA_Get_Ratio_Demand_Receipt 'company', 'Chris', '202001', '202002'
0赞
Chris
11/17/2023
这些正是我正在使用的过滤器值。值不同,但都是 varchars。
0赞
Bart McEndree
11/18/2023
当您尝试命令 EXEC sp_SIA_Get_Ratio_Demand_Receipt 'company', 'Chris', '202001', '202002'(而不是查询)时,结果是否与您的网格匹配?它应该,因为这似乎是数据适配器用于填充 DataTable 的命令。
0赞
Bart McEndree
11/18/2023
嗨,@Chris如果此答案解决了您的问题,请考虑通过单击复选标记来接受它。这表明您已经找到了解决方案并为您提供了一些声誉。
评论
sp_
sp_
#