【问题标题】:Stored procedure wrong output存储过程错误输出
【发布时间】:2015-10-23 08:52:53
【问题描述】:

我有这个存储过程返回选定周的数据,如 CompanyNameExpected Work在 2015 年 10 月 19 日和 2015 年 10 月 25 日之间完成的工作(每个示例)。

我最近刚刚添加了预期工作行,由于某种奇怪的原因,输出因一周而异,而值应该相同。

Company1 - Christopher - 35 - 35 |一个星期可以在另一个星期给出以下内容:

公司 1 - 克里斯托弗 - 350 - 35

我刚刚意识到Work done 的值是不正确的,如果没有记录的工作Expected Work 具有正确的值。

程序如下:

ALTER procedure [dbo].[spGetWeeklyActivityByEmployee]
      @startDate date
    , @endDate date
as
    set datefirst 1 -- Monday

    select 
        Company.Name as [Company]
        , Employee.FirstName + ' ' + Employee.LastName as [Name]        
        , sum(UserActivity.Cost) as [Recorder Time]
        , sum(Employee.ExpectedTime) as [Expected Time] // I have added this line, not sure if it's correct
    from 
        dbo.Employee
    inner join 
        dbo.Company on Company.CompanyId = Employee.CompanyId
    left join 
        dbo.UserActivity on UserActivity.Login = Employee.Login
                         and UserActivity.CalendarDate >= @startDate
                         and UserActivity.CalendarDate <= @endDate
    where 
        (Employee.EntranceDate is null
         or YEAR(Employee.EntranceDate) < YEAR(@startDate)
         or (YEAR(Employee.EntranceDate) = YEAR(@startDate) 
         and DATEPART(WEEK, Employee.EntranceDate) <= DATEPART(WEEK, @startDate)))
        and (Employee.ExitDate is null
             or YEAR(Employee.ExitDate) > YEAR(@endDate)
             or (YEAR(Employee.ExitDate) = YEAR(@endDate) 
             and DATEPART(WEEK, Employee.ExitDate) >= DATEPART(WEEK, @endDate)))
    group by 
        Company.Name, Employee.FirstName + ' ' + Employee.LastName

    return 0

我错过了什么吗?我检索预期时间的方式错了吗?

编辑:

这是代码中我将信息保存在数组中的部分:

// create and open a connection object
SqlConnection conn = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);
conn.Open();

// 1.  create a command object identifying
//     the stored procedure
SqlCommand cmd = new SqlCommand("spGetWeeklyActivityByEmployee", conn);

// 2. set the command object so it knows
//    to execute a stored procedure
cmd.CommandType = CommandType.StoredProcedure;

// 3. add parameter to command, which
//    will be passed to the stored procedure
cmd.Parameters.Add(new SqlParameter("@startDate", wk1));
cmd.Parameters.Add(new SqlParameter("@endDate", wk2));

// execute the command
rdr = cmd.ExecuteReader();         

string[] tab_company = new string[31]; // Don't mind the sizes
string[] tab_name = new string[31];
string[] tab_expectedtime = new string[31];
string[] tab_rectime = new string[31];

int counter;

counter = 0;

while (rdr.Read())
{
    tab_company[counter] = rdr["Company"].ToString();
    tab_name[counter] = rdr["Name"].ToString();
    tab_expectedtime[counter] = rdr["Expected Time"].ToString();
    tab_rectime[counter] = rdr["Recorder Time"].ToString();

    counter++;               
}

或许价值的变化来自这里?

【问题讨论】:

  • 一个快速的想法?您的@endDate 是否包含时间?如果您不与“23:59:59”进行比较,您将错过结束日期的所有条目,因为and UserActivity.CalendarDate &lt;= @endDate 必须小于或等于endDate 的第一秒...
  • 是的 @endDate 包含一个 DateTime 变量,@startDate 也是如此。
  • 当您传入date 类型的参数时,它不能包含时间。如果这不能解决您的问题,请检查并回电...
  • 如果您去掉 Employee.Expected 时间的总和,您必须将其包含在您的 GROUP BY 列列表中。这可能会产生副作用。试一试,然后回电……
  • 您好,很高兴阅读,我可以帮助您。答案就在那里。谢谢您的投票和接受!

标签: sql sql-server asp.net-mvc stored-procedures


【解决方案1】:

只需从Employee.ExpectedTime 中删除SUM() 并将其添加到您的GROUP BY

【讨论】:

    猜你喜欢
    • 2017-01-05
    • 1970-01-01
    • 2015-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多