Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/sql-server-2008/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
MSTSQL:SP能否同时返回out参数和结果集_Tsql_Sql Server 2008_Stored Procedures_Resultset_Output Parameter - Fatal编程技术网

MSTSQL:SP能否同时返回out参数和结果集

MSTSQL:SP能否同时返回out参数和结果集,tsql,sql-server-2008,stored-procedures,resultset,output-parameter,Tsql,Sql Server 2008,Stored Procedures,Resultset,Output Parameter,我想知道让TSQL存储过程同时返回结果集和输出参数是否可行 create procedure uspReadMyXmlInformation(@myXmlDoc xml, @myProductNum varchar(18) output) as begin set nocount on; declare @myXmlContent table(MyOrderId varchar(12) not null

我想知道让TSQL存储过程同时返回结果集和输出参数是否可行

create procedure uspReadMyXmlInformation(@myXmlDoc xml, @myProductNum varchar(18) output) as
    begin
        set nocount on;

        declare @myXmlContent table(MyOrderId varchar(12) not null
                                   ,CreatedAt datetime not null)

        insert into @myXmlContent
            select x.c.value('MyOrderID[1]', 'varchar(12)')
                    x.c.value('CreatedAt[1]', 'datetime')
                from @myXmlDoc.nodes('MyRootNodeName/MyChildNodeName') x(c)

        set @myProductNum='MyProductNum'

        select *
            from @myXmlContent

        return;
    end
所以,这里发生的事情是,当我移除输出参数时,我可以获得结果集,或者我获得输出参数,结果集总是空的0=count*

我是否可以使用相同的存储过程获取这两个存储过程,或者最好将它们拆分

我认为这在甲骨文的这篇文章中是可行的。我希望在SQLServer中实现同样的功能,尽管受2008版本的限制

我喜欢使用同一个SP,因为结果集和输出参数都表示我从XML文档中读取的信息。因此,SP的名称以某种方式说明了一切

编辑

正如一些人认为的那样,它可能是:

我不认为答案与DataReader的行为方式有关,而与TSQL的实现方式有关

事实上,我从输出参数中获取值,但我根本没有从结果集中获取它,它总是返回null

所以,我在一个只支持SQL Server的项目上,我需要它。否则,如果我不能及时完成,我会把它一分为二

下面是它的使用方法:

declare @xmlInformationData table(MyOrderId varchar(12) not null
                                  ,CreatedAt datetime not null)
insert into @xmlInformationData
    execute uspReadMyXmlInformation @myXmlDoc, @myProductNum output

while 0<(select count(*) from @xmlInformationData)
    begin
        -- This will never be executed because I have no rows in @xmlInformationData
        -- And yet, before the loop, I have my out param value!
    end

下面是一个使用输出参数和结果集的简单演示。试着运行几次,结果会有所不同

create procedure Arthur( @TheAnswer as Int Output ) as
  begin

  -- Set the value of the output parameter.
  set @TheAnswer = 42;

  -- Generate a single row most of the time.
  select GetDate() as NextVogonPoetryReading
    where DatePart( millisecond, GetDate() ) < 750;

  end;
go 1

-- Declare the variables for the test.
declare @HHGTTU as Table ( HHGTTUId Int Identity, NextVogonPoetryReading DateTime );
declare @SixTimesNine as Int;

-- Execute the SP once so that the   while   loop might.
insert into @HHGTTU ( NextVogonPoetryReading )
  execute Arthur @TheAnswer = @SixTimesNine Output;

-- See what happens.
while exists ( select Pi() from @HHGTTU )
  begin
  -- See where we are at.
  select @SixTimesNine as SixTimesNine, Max( HHGTTUId ) as MaxHHGTTUId, Max( NextVogonPoetryReading ) as MaxNextVogonPoetryReading
    from @HHGTTU;
  -- Reset.
  delete from @HHGTTU;
  set @SixTimesNine = 54;
  select @SixTimesNine as SixTimesNineAfterReset;
  waitfor delay '00:00:00.100';
  -- Execute the SP again.
  insert into @HHGTTU ( NextVogonPoetryReading )
    execute Arthur @TheAnswer = @SixTimesNine Output;
  end;

旁白:我为我提到数据阅读器给你的生活带来的创伤道歉。我只是想把我在C应用程序中的经验传递给大家,而没有深入了解您使用的数据库的连接类型、可能涉及的驱动程序等等

您可以同时拥有这两个变量,但输出参数可能会很晚才填充,例如在关闭.NET应用程序中的DataReader之后。正如@HABO所说,您可以同时拥有这两个变量,但out变量通常只有在处理完最后一个结果集后才可用。可能重复的实际上,没有任何DataReader!我不知道你们在哪里看到C标签之类的东西,但是没有!这是一个仅限SQL Server的项目,因为此数据库的目的是用作数据交换。因此,它从一边到另一边读取数据并推送信息。所以,没有C代码!现在,我获得了输出参数值,但从未获得结果集数据。我两者都需要。有人可以帮忙吗?@WillMarcouiller您描述的问题通常发生在远程调用服务器时,这不是DataReader的问题,而是由于用于与SQL server通信的TDS协议的性质。当我在服务器上运行查询时,我无法重现您的问题。像您这样使用随机结果进行测试似乎不是很明智,因为25%的时间这将显示原始问题的失败测试用例。@Lucero目的是提供一个终止且具有不同行为的示例,包括有时返回零行,而总是返回输出参数。它解决了OP文章标题中的问题:SP能否同时返回out参数和结果集。它不能解决OP的问题,失败的情况是只有一个或另一个输出是可能的,但决不能两者都有。公平地说,我没有投反对票或其他任何东西,只是指出0行结果在这里不是一个好例子。如果您想要具有随机性,那么像1或2行这样的内容会更好实际上,我的问题是,我有这个过程,在MSSQL不太喜欢的第三层次上返回一个结果集和一个输出参数。相反,我必须使用一个表值函数和一个单独的标量值函数。因为我不喜欢这种方式,我对代码进行了不同的重构,是的,现在我的uspReadXmlDataInformation过程完美地返回了想要的信息。所以,故事的主题不是在两个嵌套的结果集SP上排列,否则会出现奇怪的行为。