manipulate array of data in SQL

本文介绍了一种在SQL Server中模拟数组处理的方法,通过使用表变量存储数据,并结合索引进行迭代访问,以实现类似数组的操作。这种方法避免了使用速度较慢的游标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值