临时表、表变量的循环

岁酱吖の 提交于 2020-03-07 05:29:50
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
--循环:按事业部循环--结束

 

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!