DECLARE @rowsDepartments INT --循环:事业部统计数 DECLARE @dSerialID INT --循环:循环计数 DECLARE @departmentsID INT DECLARE @tmpDepartments TABLE(SerialID INT IDENTITY(1,1),DepartmentsID INT,DepartmentsCode VARCHAR(50)) --事业部表 --循环:按事业部循环--开始 INSERT INTO @tmpDepartments SELECT [id] AS DepartmentsID, code AS DepartmentsCode FROM Departments ORDER BY code SET @rowsDepartments=@@rowcount SET @dSerialID=1 WHILE @dSerialID<=@rowsDepartments BEGIN ...... SELECT @departmentsID=DepartmentsID FROM @tmpDepartments WHERE SerialID=@dSerialID SET @dSerialID=@dSerialID+1 END --循环:按事业部循环--结束
来源:https://www.cnblogs.com/star-studio/archive/2012/05/12/2497187.html