我有一个存储过程,可根据工作磁贴计算员工时间,并按常规/加班时间,总小时数,成本和百分比进行分解。
我将生成的表加载到网页(C#.NET)上的数据表中,以用作网格的数据源。 SQL的代码是:
ELSE IF @type = 'Reports_MH_WO_JobTitles'
BEGIN
DECLARE @TS_JT_R_O TABLE ([JobTitle] NVARCHAR(100),[R] DECIMAL(5,1),[O] DECIMAL(5,1), [Bill Rate] DECIMAL(18,2))
-- Code Here to populate @TS_JT_R_O
SELECT row_number() OVER (ORDER BY [JobTitle]) [Sort]
,T.[JobTitle]
,SUM([R]) [R]
,SUM([O]) [O]
,SUM([R]+[O]) [TOTAL]
,CAST(SUM([Cost]) AS DECIMAL(18,2)) [Cost]
,CAST(SUM([R]+[O])/( SELECT SUM([R]+[O]) FROM @TS_JT_R_O ) AS DECIMAL(6,5)) [HoursPer]
,CAST(SUM([Cost])/( SELECT SUM([COST]) FROM ( SELECT (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [COST] FROM @TS_JT_R_O GROUP BY [Bill Rate] ) Z ) AS DECIMAL(6,5)) [CostPer]
FROM
( SELECT [JobTitle],SUM([R]) [R],SUM([O]) [O],SUM([R]+[O]) [TOTAL], (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [Cost] FROM @TS_JT_R_O GROUP BY [JobTitle], [Bill Rate] ) T
GROUP BY T.[JobTitle]
UNION
SELECT '999999999'[Sort], 'GRAND TOTAL' [JobTitle], SUM([R]) [R], SUM([O]) [O], SUM([R]+[O]) [TOTAL], CAST(SUM([Cost]) AS DECIMAL(18,2)) [Cost],1,1
FROM
( SELECT [JobTitle],SUM([R]) [R],SUM([O]) [O],SUM([R]+[O]) [TOTAL], (SUM([R])*[Bill Rate])+(SUM([O])*[Bill Rate]*1.5) [Cost] FROM @TS_JT_R_O GROUP BY [JobTitle], [Bill Rate] ) T
ORDER BY [Sort],[JobTitle]
END
填充网格的代码:
sqlStr = "WorkOrder_SelectStatements";
cmd = new SqlCommand(sqlStr, cnn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandTimeout = 0;
cmd.Parameters.Add("@type", SqlDbType.NVarChar).Value = "Reports_MH_WO_JobTitles";
cmd.Parameters.Add("@ID", SqlDbType.NVarChar).Value = ddlWorkOrder.SelectedValue;
if (rdpStart.SelectedDate != null)
cmd.Parameters.Add("@startDate", SqlDbType.NVarChar).Value = txtStart.Text;
if (rdpEnd.SelectedDate != null)
cmd.Parameters.Add("@endDate", SqlDbType.NVarChar).Value = txtEnd.Text;
da = new SqlDataAdapter(cmd);
da.SelectCommand.CommandTimeout = 0;
DataTable dt = new DataTable();
da.Fill(dt);
ds.Tables.Add(dt);
grid.DataSource = dt;
grid.DataBind();
调试DataAdapter时填充DataTable,但SQL中“Cost”和“CostPer”的列作为NULL进入DataTable。在缺失的数据中,填充的唯一单元格是UNION SELECT的成本百分比的100%。
我不知道哪里只删除了一些数据。 SQL和DataAdapter之间数据丢失的想法是什么?
截图:
SQL
Visual Studio调试视图
网页结果视图