如何从存储过程中获取数据

时间:2012-02-10 12:45:59

标签: database sql-server-2008 tsql stored-procedures

我正在尝试从t-sql(sql server 2008)调试\执行SP,当我执行以下sql时,它只返回一条消息'Command(s)completed successfully'。

我知道SP通过@RETURN_VALUE参数返回表中的数据,如何在sql查询窗口中显示它。

exec STARMS.dbo.GetMetaData @@Assembly=13366,@@Namespace=NULL,@@ParameterName=NULL,@@Standard=0,@@Timestamp=NULL

SP看起来像这样(删除了主体)有趣的部分是创建临时表,然后返回临时表的内容:

CREATE PROCEDURE [dbo].[GetMetaData]
    @@Assembly Sql_Variant = NULL,
    @@Namespace Varchar(30) = NULL,
    @@ParameterName Varchar(40) = NULL,
    @@Standard Bit = 0,
    @@Timestamp DateTime = NULL
AS

    SET NOCOUNT ON

    DECLARE @ResultTable TABLE (AssemblyId Int, Namespace Varchar(30), ParameterName Varchar(40), Value Varchar(100))

    -- Does loads of stuff and then inserts into @ResultTable table...

    SET NOCOUNT OFF

    -- Return the result
    SELECT AssemblyId, Namespace, ParameterName, [Value]
        FROM @ResultTable

    RETURN

注意:我问这个问题的原因是因为我试图查看SQL Server 2008中SP的性能,我不确定为什么这不会在查询窗口中返回数据。< / p>

1 个答案:

答案 0 :(得分:2)

检查SP的完整代码,可能在某处有RETURN语句,所以 - SP不会返回任何数据(注意IF SomeContition IS TRUE RETURN):

CREATE PROCEDURE [dbo].[GetMetaData]
    @@Assembly Sql_Variant = NULL,
    @@Namespace Varchar(30) = NULL,
    @@ParameterName Varchar(40) = NULL,
    @@Standard Bit = 0,
    @@Timestamp DateTime = NULL
AS

    SET NOCOUNT ON

    DECLARE @ResultTable TABLE (AssemblyId Int, Namespace Varchar(30), ParameterName Varchar(40), Value Varchar(100))

    -- Does loads of stuff and then inserts into @ResultTable table...

    IF SomeContition IS TRUE RETURN

    SET NOCOUNT OFF

    -- Return the result
    SELECT AssemblyId, Namespace, ParameterName, [Value]
        FROM @ResultTable

    RETURN