如何从存储过程返回临时表 [英] How to return temporary table from stored procedure

查看:46
本文介绍了如何从存储过程返回临时表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

CREATE PROCEDURE [test].[proc]
@ConfiguredContentId int,
@NumberOfGames int
AS
BEGIN
 SET NOCOUNT ON
 RETURN 
 @WunNumbers TABLE (WinNumb int)

    INSERT INTO @WunNumbers (WinNumb)
 SELECT TOP (@NumberOfGames) WinningNumber
 FROM [Game].[Game] g
 JOIN [Game].[RouletteResult] AS rr ON g.[Id] = rr.[gameId]
 WHERE g.[ConfiguredContentId] = @ConfiguredContentId
 ORDER BY g.[Stoptime] DESC

 SELECT WinNumb, COUNT (WinNumb) AS "Count"
 FROM @WunNumbers wn
 GROUP BY wn.[WinNumb]
END
GO

此存储过程返回第一个 select 语句中的值,但我希望返回第二个 select 语句中的值.表@WunNumbers 是一个临时表.

This stored procedure returns values from first select statement, but I would like to have values from second select statement to be returned. Table @WunNumbers is a temporary table.

有什么想法吗???

推荐答案

您使用的是什么版本的 SQL Server?在 SQL Server 2008 中,您可以使用 表参数和表类型.

What version of SQL Server are you using? In SQL Server 2008 you can use Table Parameters and Table Types.

另一种方法是从用户定义的函数返回一个表变量,但我不是这种方法的忠实粉丝.

An alternative approach is to return a table variable from a user defined function but I am not a big fan of this method.

您可以在此处

这篇关于如何从存储过程返回临时表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆