首页 > 解决方案 > In TSQL Can I/How can I fill an array with data from a table?

问题描述

Here is the code I have so far. (I changed the column names and other stuff for safety)

/* declare empty array */
DECLARE @oArray varchar(26)
/* loop variables */
DECLARE @Counter INT
DECLARE @rows INT 
SET @Counter = 1
SET @rows    = (select count(z) from z where z like 'z-%' and z = 1)
/* fill array */
WHILE (@Counter <= @rows)
BEGIN
SET @oArray(@Counter) = (SELECT z FROM (
                         SELECT ROW_NUMBER() OVER (ORDER BY z ASC) AS RowNumber,*
                         FROM z WHERE z like 'z-%' and z = 1) AS taboo
                         WHERE RowNumber = @Counter)
END

It works up to the set in the while loop. Is there a way to perform something like this in TSQL? I tried to research this. However, I cannot seem to find any post like it. (for TSQL)

I need the @Counter to work at "@oArray(@Counter) = " & "WHERE RowNumber = @Counter".

I want to dynamically pull data from a particular table to be used in calculations without hardcoding in values.

标签: arraystsqlwhile-loop

解决方案


推荐阅读