代码之家  ›  专栏  ›  技术社区  ›  Eton B.

两表中复杂的日常自动插入查询

  •  0
  • Eton B.  · 技术社区  · 16 年前

    我有下表及其列(仅列出相关列):

    条目
    EntryID(国际/汽车公司)
    员工编号(int)
    JustifyDate(日期时间)
    入口日期(日期时间)

    员工
    员工编号(int)
    corehourid(int)
    在家工作(位/布尔)

    小时
    入口ID(int)
    inhour(日期时间)
    outhour(日期时间)

    核心时刻
    corehourid(int)-不唯一
    InHour(int)
    OUTHORY(INT)

    编辑:我忘了提到corehourid不是一个唯一的字段,表可以如下所示:

    +-----------+-------+-------+----------+
    |CoreHourId |InHour |OutHour|Identifier|
    +-----------+-------+-------+----------+
    |    2      |  9    |  12   |    1     |
    |    2      |  14   |  17   |    2     |
    |    3      |  7    |  11   |    3     |
    |    3      |  15   |  18   |    4     |
    +-----------+-------+-------+----------+
    

    对不起,大的布局,我真的不知道如何正确地张贴表信息。 下面是一个解释我想做什么的尝试:

    每天都应该插入一行 Entry 和 Hour 对于所有 WorkingFromHome . 在 条目 表它应该放置相应的 EmployeeNumber 为了 JustifyDate 它应该在作业运行的任何一天添加。对于 EntryDate 字段应添加当天的日期,但时间部分应为 InHour 从第一个对应的 CoreHour 行。

    对于 小时 表它应该添加 EntryID 刚插进去的 条目 表和 每小时 应该和 入会日期 为了 OutHour 字段它应根据最后一个字段添加日期时间 外出 对应于员工的 CoreHourID .

    我努力了很多,所以任何帮助都是值得感谢的。

    附言:欢迎对我的解释提出任何意见/问题,我将很乐意回答。

    1 回复  |  直到 16 年前
        1
  •  2
  •   Thomas    16 年前

    可以将以下内容封装到存储过程中,该存储过程可以通过计划作业执行。我不太清楚你的意思 for JustifyDate it should add whatever day it is when the job is running .

    Declare @NewEntries Table (
                                EntryId int not null
                                , EmployeeNumber int not null
                                , JustifyDate datetime
                                , EntryDate datetime
                                )
    
    Insert [Entry]( EmployeeNumber, JustifyDate, EntryDate )
        Output Inserted.EntryId, Inserted.EmployeeNumber, Inserted.JustifyDate, Inserted.EntryDate 
            Into @NewEntries
    Select EmployeeNumber
        , CURRENT_TIMESTAMP
        , DateAdd(hh, CoreHour.InHour, DateAdd(d, 0, CURRENT_TIMESTAMP))
    From Employee
        Join CoreHour
            On CoreHour.CoreHourId = Employee.CoreHourId
    Where WorkingFromHome = 1
    
    Insert [Hour]( EntryId, InHour, OutHour )
    Select NE.EntryId, DatePart(hh, NE.EntryDate), CoreHour.OutHour
    From @NewEntries As NE
        Join Employee
            On Employee.EmployeeNumber = NE.EmployeeNumber
        Join CoreHour
            On CoreHour.CoreHourId = Employee.CoreHourId
    

    (修订为使用输出条款)。

    推荐文章