我有tbl_emp
这样的话:
empid
1
2
3
4
和tbl_att
一样:
empid workingdate
1 2017-05-11
2 2017-05-13
3 2017-05-14
...........
...........
我在SQL Server代理中有一个工作要在每个星期天执行步骤,我希望该工作在当天为empid
的每个tbl_att
插入一行。假设周日是2017-05-22,所以我想这样:
empid workingdate
1 2017-05-22
2 2017-05-22
3 2017-05-22
这意味着我希望它在同一天(任务执行日)插入tbl_att
所有empid
,所以任何人都可以引导我查询我需要放入我的步骤命令?
答案 0 :(得分:2)
试试这个,
INSERT INTO tbl_att SELECT empid,CAST(GETDATE() AS DATE) FROM tbl_emp;
答案 1 :(得分:0)
insert into tbl_att (empid, workingdate)
Select empid,cast(getdate() as date) from tbl_emp
当您在每个星期日运行作业时,上面的查询将根据您的预期插入数据我相信。仅供参考,但这取决于系统日期
答案 2 :(得分:0)
希望它可以帮到你
CREATE TABLE #tbl_att (empid INT,workingdate DATE)
CREATE TABLE #EmpidTab (empid INT)
INSERT INTO #EmpidTab
SELECT 1 UNION ALL
SELECT 2 UNION ALL
SELECT 3 UNION ALL
SELECT 4
SELECT * FROM #EmpidTab
INSERT INTO #EmpidTab
SELECT 5
INSERT INTO #tbl_att
SELECT Empid
,Getdate() AS workingdate
FROM #EmpidTab i
WHERE NOT EXISTS (
SELECT 1
FROM #tbl_att t
WHERE t.empid = i.Empid
) --Eliminate duplicte insertion of empid's
SELECT @@ROWCOUNT AS NoRowsInserted
SELECT *
FROM #tbl_att