自动插入数据

时间:2010-01-18 15:17:01

标签: sql sql-server sql-server-2000

我有一个表格名称加班时间,其中包含以下列 Ot_ID,Shift_Date,Employee_ID,Hours。

我需要做的是在每个月的第一天,在这些表中自动插入一组值。

例如,我需要为1月添加值('1/1/2010',12345,4.6), 2月份的价值观('2/1/2010',12345,4.6),等等。

必须进行此添加,以便仅限某些员工列表,并且每月的小时值不变。 我在后端工作MS SQL Server 2000。和视觉工作室,Winforms on C sharp在前端。

2 个答案:

答案 0 :(得分:1)

SQL Server代理服务可以安排您的工作(插入新记录)每月执行;这可以完全在MSSQL2000中完成,不需要任何前端编程。

答案 1 :(得分:0)

您可以使用公用表表达式创建月份列表。使用交叉联接为每个月的每个员工生成一行,并将其插入小时表。

一些示例代码,包含表变量:

declare @hourtable table (
    ot_id int identity(1,1),
    shift_date datetime,
    employee_id int,
    hours float)

declare @employees table (
    employee_id int
)   

insert into @employees select 1
insert into @employees select 2

;with months as (
    select cast('2009-01-01' as datetime) as month
    union all
    select dateadd(m,1,month)
    from months
    where month < '2009-12-01'  
)
insert into @hourtable
(shift_date, employee_id, hours)
select m.month, e.employee_id, 1.23
from months m
cross join @employees e

select * from @hourtable
相关问题