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
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
相关文章推荐
- Failed to generate a user instance of SQL Server due to failure in retrieving the user's local application data path 解决(VS2010+IIS7.5)
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its
- HOW TO: Change the Owner of a User-Defined Data Type That Is in Use in SQL Server 2000
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary log
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its 错误记录
- 错误码: 1418 This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and
- [LinkedIn] Smallest/greatest/largest k element in stream of data (integer array) priority Q / heap
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and binary
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- ADO.NET Entity Framework: The version of SQL Server in use does not support datatype 'datetime2'
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法
- Error: The version of SQL Server in use does not support datatype 'datetime2'
- 解决1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declaration and b
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决
- This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its de 错误解决办法