As we know, SQL server lacks of concept of array.
a workaround is to store the data in a table variable and then iterate it.
one way to do iterate is using cursor, but it's slow
another way is add a id column in the table variable, and then access the record in table variable with index.
DECLARE @UserName VARCHAR(30), @DisplayName VARCHAR(30)
DECLARE @Users TABLE(Id int identity(1,1), UserName VARCHAR(30), DisplayName VARCHAR(30))
INSERT INTO @Users
VALUES
('lxing', 'Lili')
,('ashan', 'Allen')
DECLARE @RowsToProcess int = @@ROWCOUNT
DECLARE @Row INT=1
WHILE(@Row <= @RowsToProcess)
BEGIN
SELECT @UserName = UserName, @DisplayName = DisplayName FROM @Users where Id = @Row
declare @uid int
--insert user
SET @Row = @Row + 1
END
本文介绍了一种在SQL Server中模拟数组处理的方法,通过使用表变量存储数据,并结合索引进行迭代访问,以实现类似数组的操作。这种方法避免了使用速度较慢的游标。
1456

被折叠的 条评论
为什么被折叠?



