[英]for each in MS SQL SERVER?
I have two tables:我有两张桌子:
Employees(EmployeeID, EmployeeName, EmployeeStatus, BasicSalary)
and Employees(EmployeeID, EmployeeName, EmployeeStatus, BasicSalary)
和
EmployeePayroll (PayrollID, EmployeeID, VoucherNo, BasicSalary, SalaryMonth)
I want to make a for each
loop for every employee in the first table so that I can insert dummy data like (0, EmployeeID,0,0,0)
into the second table.我想为第一个表中的每个员工创建一个for each
循环,以便我可以将(0, EmployeeID,0,0,0)
之类的虚拟数据插入到第二个表中。
I tried to make it with a for
loop but I could not make it, so is there a for each
loop in MS SQL Server??我试图用一个for
循环来实现它,但我做不到,所以MS SQL服务器中for each
循环都有一个吗?
If you really require a loop, you can use a cursor.如果你真的需要一个循环,你可以使用 cursor。 They are horribly inefficient , so you should avoid unless you absolutely require it:它们效率极低,因此除非绝对需要,否则应避免使用:
DECLARE c CURSOR READ_ONLY FAST_FORWARD FOR
SELECT EmployeeID
FROM Employees
DECLARE @id Int
-- Open the cursor
OPEN c
FETCH NEXT FROM c INTO @id
WHILE (@@FETCH_STATUS = 0)
BEGIN
INSERT INTO EmployeePayroll
SELECT 0, @id, 0, 0, 0
-- do other stuff
FETCH NEXT FROM c INTO @id
END
-- Close and deallocate the cursor
CLOSE c
DEALLOCATE c
Use the following statement:使用以下语句:
INSERT INTO EmployeePayroll
SELECT
0,EmployeeID ,0,0,0
FROM
Employees
You can check for the existance of the record before inserting it by appending:您可以在插入之前检查记录是否存在,方法是附加:
WHERE
ID NOT IN
(
SELECT
EmployeeID
FROM
EmployeePayroll
)
This is the one way to achieve your requirement using While loop & Temp table variable,这是使用 While 循环和临时表变量来满足您的要求的一种方法,
The sample query is given below,示例查询如下所示,
--: Get the Employees table data & insert this to temp table variable
Declare @TempEmpTbl Table (RowId int identity, EmployeeID int, EmployeeName nvarchar(100), EmployeeStatus int, BasicSalary int);
Insert into @TempEmpTbl Select * from Employees;
--: temp variables
Declare @TempEmpCount Int = (Select Count(RowId) From @TempEmpTbl);
Declare @MinCount Int = 1;
Declare @GetEmpId int;
--: while loop for EmployeePayroll tbl insertion based on Employees data
While(@TempEmpCount >= @MinCount)
Begin
Set @GetEmpId = (Select EmployeeID From @TempEmpTbl Where RowId = @MinCount);
Insert into EmployeePayroll values (0, @GetEmpId,0 ,0 ,0)
Set @MinCount = @MinCount + 1;
End
Note: Suppose the employee record is already there, we can able to update EmployeePayroll records from within this while loop.注意:假设员工记录已经存在,我们可以在这个 while 循环中更新 EmployeePayroll 记录。
INSERT INTO EmployeePayroll
SELECT
0,E.EmployeeID ,0,0,0
FROM
Employees E
WHERE
NOT EXISTS
(
SELECT
1
FROM
EmployeePayroll WHERE EmployeeID = E.Id
)
Hope this helps希望这可以帮助
This is most optimized way to achieve this这是实现这一目标的最优化方式
I was looking for a decent way for a foreach loop in SQL and came up with this easy code我正在为 SQL 中的 foreach 循环寻找一种体面的方法,并想出了这个简单的代码
SELECT RowNum = ROW_NUMBER() OVER(ORDER BY Id),*
INTO #Locations
FROM [dbo].[Location]
DECLARE @MaxRownum INT
SET @MaxRownum = (SELECT MAX(RowNum) FROM #Locations)
DECLARE @Iter INT
SET @Iter = (SELECT MIN(RowNum) FROM #Locations)
WHILE @Iter <= @MaxRownum
BEGIN
DECLARE @currentCountry varchar(250)
DECLARE @locId int
SET @currentCountry = ( SELECT Country
FROM #Locations
WHERE RowNum = @Iter)
SET @LocId = ( SELECT Id
FROM #Locations
WHERE RowNum = @Iter)
-- run your operation here
Update [dbo].[Location]
SET CountryId = (SELECT (Id) FROM [dbo].[Site] WHERE [dbo].[Site].Name = @currentCountry)
WHERE [dbo].[Location].Id = @locId
SET @Iter = @Iter + 1
END
DROP TABLE #Locations
Try Cross Apply
.尝试Cross Apply
。 This is the best alternate for cursor and looping.这是 cursor 和循环的最佳替代方案。 Mover over if you compare the execution plan of cross apply with other methods you will notice its noticeably faster如果您将交叉应用的执行计划与其他方法进行比较,您会发现它明显更快
http://sqlserverplanet.com/sql-2005/cross-apply-explained http://sqlserverplanet.com/sql-2005/cross-apply-explained
HI你好
USE Default constraints for the column which needs to be zero使用需要为零的列的默认约束
payrollID,VoucherNo, BasicSalary, Salary-Month payrollID,VoucherNo, BasicSalary, Salary-Month
insert the EmployeeID for the same and use the above mentioned concept插入相同的 EmployeeID 并使用上述概念
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.