是否有任何解决方案/查询可从存储过程结果中获取前N条记录,而无需检索整个结果集?
考虑到我的存储过程返回了300万行,而我只想要它的前10行。
您可以使用TOP
子句来实现
句法
SELECT TOP number|percent column_name(s)
FROM table_name
WHERE condition;
假设您有Your_stored_procedure
用户返回列表
CREATE PROCEDURE Your_stored_procedure
AS
SELECT UserId, UserName
FROM yourtable
GO;
在这里,您需要创建临时表来存储存储过程中的值
-- Check result
CREATE TABLE #TempTable
(
UserId INT,
UserName varchar(100),
)
INSERT INTO #TempTable(UserId, UserName)
EXEC Your_stored_procedure
这样你就可以得到结果
SELECT TOP 10 UserId, UserName
FROM #TempTable
ORDER BY UserId -- As @Squirrel's comment, TOP should come with ORDER BY
注意
您应确保表中的列数根据存储过程的结构而定。
更新
正如@Vinod Kumar的评论一样,您也可以使用OPENQUERY来实现它,如下所示
SELECT top 1 * FROM OPENQUERY ([MyServer], 'EXEC [VinodTest].[dbo].[tblAuthorsLarge] year = 2014')
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句