原文来自:https://www.lesg.cn/netdaima/sqlservert-sql/2016-463.html SsqlServer 中循环表有几种方式 1.临时表 2.游标 3-. 下面就来说说怎么用临时表格来循环数据 create table t( id ,), dt datetime not null default(getdate()), name ) not null default('') ) --测试案例,给表插入数据 ; ) begin insert into t
DECLARE @FunctionCode VARCHAR(20)--声明游标变量DECLARE curfuntioncode CURSOR FOR SELECT FunctionalityCode FROM dbo.SG_Functionality WHERE Type=2 ORDER BY TimeStamp --创建游标OPEN curfuntioncode --打开游标FETCH NEXT FROM curfuntioncode INTO @FunctionCode --给游标变量赋值W
)SET @OrgId = N'901205CA-6C22-4EE7-AE4B-96CC7165D07F'; WITH Childs AS ( SELECT * FROM HROrgRelation WHERE ParentNM = @OrgId UNION ALL SELECT A.* FROM HROrgRelation A, Childs B WHERE A.FatherID = B.ParentNM ) SELECT ParentNM FROM Childs
代码 create table tm_lzh as SELECT 'a1' c1,'b1' c2 FROM dual union all SELECT 'a2' c1,'b2' c2 FROM dual union all SELECT 'a3' c1,'b3' c2 FROM dual union all SELECT 'a4' c1,'b4' c2 FROM dual union all SELECT 'a5' c1,'b5' c2 FROM dual union all SELECT 'a
[cursor]游标:用于循环表行数据,类似指针 格式如下: declare tempIndex cursor for (select * from table) --定义游标 open tempIndex --打开游标 fetch next from tempIndex into @x --抓取下一行数据给变量 --0表示抓取成功,1表示抓取失败,2表示不存在抓取行 begin --sql 语句 end close tempIndex --关闭游标 deallocate tempIndex -