您的位置:首页 > 数据库

manipulate array of data in SQL

2017-01-11 10:40 363 查看
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
相关文章推荐