我有两个表:
Employees(EmployeeID, EmployeeName, EmployeeStatus, BasicSalary) 和
Employees(EmployeeID, EmployeeName, EmployeeStatus, BasicSalary)
EmployeePayroll (PayrollID, EmployeeID, VoucherNo, BasicSalary, SalaryMonth)
我想为for each第一个表中的每个员工创建一个循环,以便可以将虚假数据(0, EmployeeID,0 ,0 ,0)插入第二个表中。
for each
(0, EmployeeID,0 ,0 ,0)
我试图通过for循环使其实现,但无法实现,因此for eachMS SQL Server中是否存在循环?
for
使用以下语句:
INSERT INTO EmployeePayroll SELECT 0,EmployeeID ,0,0,0 FROM Employees
您可以通过添加以下内容来检查记录是否存在,然后再插入:
WHERE ID NOT IN ( SELECT EmployeeID FROM EmployeePayroll )