2010-01-18 62 views
0

我有一個表名稱加班小時,其中包含以下列 Ot_ID,Shift_Date,Employee_ID,Hours。自動插入數據

我需要做的是在每個月的第一天的這些表中自動插入一組值。

例如,我需要爲一月, 值( '2/1/2010',12345,4.6)二月添加的值( '1/1/2010',12345,4.6)等爲整年。

此外,必須完成此操作,以便僅限員工的某個列表,並且每個月的Hours值不變。 我正在MS SQL Server 2000的後端工作。和視覺工作室,Winforms在C sharp的前端。

回答

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 
+0

SQL Server 2000不支持CTE ... – RedFilter 2010-01-18 15:38:09

1

SQL Server代理服務可以安排(插入新記錄的)工作,每個月進行;這可以完全在MSSQL2000中完成,並且不需要任何前端編程。