使用循环从 table in sql 中选择值
Selecting the values from a table in sql using a loop
我有一个带有一定数量 ID 的 table,我想使用这些单独的 ID 从另一个 table.
检索数据
set @CurrentRow = 0
set @RowsToProcess = (SELECT COUNT(*) FROM @QuestionsPrimaryTable)
WHILE(@CurrentRow < @RowsToProcess)
BEGIN
DECLARE @id int
DECLARE @value varchar(200)
SET @CurrentRow = @CurrentRow + 1
SELECT @id = Q.QuestionsId FROM @QuestionsPrimaryTable Q
SET @value = (SELECT Q.QuestionPrimaryDescription FROM QuestionPrimary Q WHERE Q.QuestionPrimaryID = @id)
PRINT @value
END
我要检索的单独 ID 值是 5
、7
、9
因为现在我只检索 9
的值
如何检索单独的 ID 值?
我不确定您所追求的是否真的需要一个循环。 SQL 中很少需要游标,因此如果可能的话,我总是希望在没有游标的情况下获得结果。
您是否正在寻找这样的东西,您可以在其中 JOIN
QuestionPrimary
和 @QuestionsPrimaryTable
,并在 5 or 7 or 9
中过滤 ID
的结果?
SELECT qp.QuestionPrimaryID, qp.QuestionPrimaryDescription
FROM QuestionPrimary qp
INNER JOIN @QuestionsPrimaryTable qpt
ON qp.QuestionPrimaryID = qpt.[JOIN_COLUMN]
WHERE qpt.QuestionPrimaryID IN(5,7,9)
只要基于集合的方法可行,就应该避免循环。话虽如此,如果您确实想要一个循环,那么这应该可以解决您的问题:
SET @CurrentRow = @CurrentRow + 1 -- first value is 1
SELECT @id = Q.QuestionsId
FROM (
SELECT QuestionsId,
ROW_NUMBER() OVER (ORDER BY QuestionsId) AS rn
FROM @QuestionsPrimaryTable) Q
WHERE Q.rn = @CurrentRow
在您的代码中,每次循环迭代都会获取相同的 QuestionsId
值。使用 ROW_NUMBER()
您可以访问 @CurrentRow
记录。
如果您绝对需要遍历此数据,则需要在脚本中添加一些内容以移动到@QuestionsPrimaryTable 中的下一条记录。目前的编写方式是在每次迭代期间将@Id 设置为相同的值。
根据您打算如何使用@QuestionsPrimaryTable,您可以简单地在循环中添加一个删除以删除您选择的最后一条记录。
set @CurrentRow = 0
set @RowsToProcess = (SELECT COUNT(*) FROM @QuestionsPrimaryTable)
WHILE(@CurrentRow < @RowsToProcess)
BEGIN
DECLARE @id int
DECLARE @value varchar(200)
SET @CurrentRow = @CurrentRow + 1
SELECT @id = MAX(Q.QuestionsId) FROM @QuestionsPrimaryTable Q
SET @value = (SELECT Q.QuestionPrimaryDescription FROM QuestionPrimary Q WHERE Q.QuestionPrimaryID = @id)
PRINT @value
DELETE @QuestionsPrimaryTable
WHERE QuestionsId = @id
END
话虽这么说,但可能有更好的方法来完成此任务。如果您能详细说明您的问题,我们可能会为您提供更好的解决方案。
我有一个带有一定数量 ID 的 table,我想使用这些单独的 ID 从另一个 table.
检索数据set @CurrentRow = 0
set @RowsToProcess = (SELECT COUNT(*) FROM @QuestionsPrimaryTable)
WHILE(@CurrentRow < @RowsToProcess)
BEGIN
DECLARE @id int
DECLARE @value varchar(200)
SET @CurrentRow = @CurrentRow + 1
SELECT @id = Q.QuestionsId FROM @QuestionsPrimaryTable Q
SET @value = (SELECT Q.QuestionPrimaryDescription FROM QuestionPrimary Q WHERE Q.QuestionPrimaryID = @id)
PRINT @value
END
我要检索的单独 ID 值是 5
、7
、9
因为现在我只检索 9
如何检索单独的 ID 值?
我不确定您所追求的是否真的需要一个循环。 SQL 中很少需要游标,因此如果可能的话,我总是希望在没有游标的情况下获得结果。
您是否正在寻找这样的东西,您可以在其中 JOIN
QuestionPrimary
和 @QuestionsPrimaryTable
,并在 5 or 7 or 9
中过滤 ID
的结果?
SELECT qp.QuestionPrimaryID, qp.QuestionPrimaryDescription
FROM QuestionPrimary qp
INNER JOIN @QuestionsPrimaryTable qpt
ON qp.QuestionPrimaryID = qpt.[JOIN_COLUMN]
WHERE qpt.QuestionPrimaryID IN(5,7,9)
只要基于集合的方法可行,就应该避免循环。话虽如此,如果您确实想要一个循环,那么这应该可以解决您的问题:
SET @CurrentRow = @CurrentRow + 1 -- first value is 1
SELECT @id = Q.QuestionsId
FROM (
SELECT QuestionsId,
ROW_NUMBER() OVER (ORDER BY QuestionsId) AS rn
FROM @QuestionsPrimaryTable) Q
WHERE Q.rn = @CurrentRow
在您的代码中,每次循环迭代都会获取相同的 QuestionsId
值。使用 ROW_NUMBER()
您可以访问 @CurrentRow
记录。
如果您绝对需要遍历此数据,则需要在脚本中添加一些内容以移动到@QuestionsPrimaryTable 中的下一条记录。目前的编写方式是在每次迭代期间将@Id 设置为相同的值。
根据您打算如何使用@QuestionsPrimaryTable,您可以简单地在循环中添加一个删除以删除您选择的最后一条记录。
set @CurrentRow = 0
set @RowsToProcess = (SELECT COUNT(*) FROM @QuestionsPrimaryTable)
WHILE(@CurrentRow < @RowsToProcess)
BEGIN
DECLARE @id int
DECLARE @value varchar(200)
SET @CurrentRow = @CurrentRow + 1
SELECT @id = MAX(Q.QuestionsId) FROM @QuestionsPrimaryTable Q
SET @value = (SELECT Q.QuestionPrimaryDescription FROM QuestionPrimary Q WHERE Q.QuestionPrimaryID = @id)
PRINT @value
DELETE @QuestionsPrimaryTable
WHERE QuestionsId = @id
END
话虽这么说,但可能有更好的方法来完成此任务。如果您能详细说明您的问题,我们可能会为您提供更好的解决方案。