我有empdetails表,其中列为分配的叶子。我还有一个用于LeavesTaken
的专栏。现在,我要计算每个员工的请假余额并相应地进行更新。
{
command.Connection = _connection;
SqlDataReader reader = command.ExecuteReader();
Employee employee = new Employee();
List<Users.LeaveRequest> leaves = new List<Users.LeaveRequest>();
while (reader.Read())
{
leaves.Add(new Users.LeaveRequest() { DayPart = reader.GetInt32(3), fromdate = reader["fromdate"].ToString(), todate = reader["todate"].ToString() });
}
reader.Close();
if (leaves.Count>0)
{
leaveRequests = leaves;
}
else
{
leaveRequests = null;
}
command.CommandText = "SELECT TotalLeaves,LeavesTaken FROM empDetails WHERE empID = @empid";
//command.Parameters.AddWithValue("@empid", CurrentUser.Employee.EmpID);
reader = command.ExecuteReader();
if (reader.Read())
{
employee.TotalLeaves = reader.GetInt32(0);
employee.LeavesTaken = reader.GetDouble(1);
}
reader.Close();
return employee;
}````
答案 0 :(得分:0)
我可以想到三种解决方案,因为您没有精确指定所需的内容,也没有示例数据:
您想要总计员工的价值:
SELECT SUM(TotalLeaves), SUM(LeavesTaken)
FROM empDetails
WHERE empID = @empid
您要在两个值之间取得平衡
SELECT SUM(TotalLeaves) - SUM(LeavesTaken)
FROM empDetails
WHERE empID = @empid
如果每个empID
有一条记录,您还可以尝试:
SELECT TotalLeaves - LeavesTaken
FROM empDetails
WHERE empID = @empid
答案 1 :(得分:0)
我很好奇为什么总叶子有整数,但是无论如何,您都可以在CommandText
假设这三列AllottedLeaves, TotalLeaves, LeavesTaken
的数据类型为DECIMAL(4,2)
现在,我想计算每位员工的请假余额并相应地进行更新。
更新TABLE empDetails
{
command.CommandText = "UPDATE empDetails " +
"SET TotalLeaves = TotalLeaves - llottedLeaves, " +
"LeavesTaken = LeavesTaken + AllottedLeaves, " +
"AllottedLeaves = 0" +
"WHERE AllottedLeaves <> 0 AND AllottedLeaves <= TotalLeaves";
command.Connection = _connection;
command.ExecuteNonQuery();
}
更新TABLE empDetails
中的一名员工
{
command.CommandText = "UPDATE empDetails " +
"SET TotalLeaves = TotalLeaves - llottedLeaves, " +
"LeavesTaken = LeavesTaken + AllottedLeaves, " +
"AllottedLeaves = 0" +
"WHERE empID = @empid";
command.Parameters.AddWithValue("@empid", CurrentUser.Employee.EmpID);
command.Connection = _connection;
command.ExecuteNonQuery();
}