我面临一个非常孤立的问题dynamic sql query
。我有两个查询在一个stored procedure
. 他们正在关注
第一个查询:
SELECT *
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY viwPerformance.LastModifiedOn DESC) AS rowNumber,viwPerformance.* FROM viwPerformance WHERE OrgId=218 AND EmployeeId = 1668 AND IsTerminate = 0 AND TagId LIKE '%' + CAST(2893 AS VARCHAR) + '%' AND Archive='False' AND SmartGoalId IS NOT NULL
) AS E
WHERE rowNumber >= 1 AND
rowNumber < 11
它导致所有列值和 SmartGoalId 为
4471,2815,4751,4733,4863,4690,4691,4692,4693,4694
第二个查询(这里我只需要上面查询中的 SmartgoalId,所以我使用东西)
SELECT @strGoalIds = STUFF((SELECT ',' + CAST(SmartGoalId AS VARCHAR)
FROM (
SELECT ROW_NUMBER() OVER(ORDER BY viwPerformance.LastModifiedOn DESC) AS rowNumber,viwPerformance.* FROM viwPerformance WHERE OrgId=218 AND EmployeeId = 1668 AND IsTerminate = 0 AND TagId LIKE '%' + CAST(2893 AS VARCHAR) + '%' AND Archive='False' AND SmartGoalId IS NOT NULL
) AS E
WHERE rowNumber >= 1 AND
rowNumber < 11 FOR XML PATH('')), 1, 1, '')
结果 SmartgoalId 为
4471,2815,4751,4733,4863,4651,4690,4691,4692,4693
请注意,最后一个 id “4694”在上面的查询中不可用,因为“4651”被添加到它,但它在第一个查询中不可用,这是正确的,“4651”不应该出现在第二个查询结果中。
所以我的主要观点是为什么第二个查询给出不同的结果,因为它与第一个查询相同。
注意:我是否正确,该Stuff
函数反转了值并且没有以正确的顺序给出它们。