问题描述:有一个存储过程 proc_ptt,它会返回2个数据集,2个数据集的结构各不相同;
这个存储过程暂时不宜修改它;现在有sql语句执行exec proc_ptt,而且期望把它返回的2个数据集都能够先保存下来,我知道,如果是返回一个数据集可以用形如 insert into 临时表 exec proc_ptt,可现在是2个数据集,而且我更需要第二个数据集,那如何获取第2个数据集,然后存储到变量或临时表?

解决方案 »

  1.   

    补充:我不是问存储过程如何返回多个数据集,
    也不是问 代码里,如c#,如何获取数据集,而是问sql语句如何获取exce 存储过程的返回数据集。看了好多技术帖子,感觉搞技术的,经常是对问题表达和描述不清晰,回帖的对原帖理解不透而回答的驴唇不对马嘴的情形,所以在此啰嗦一句。
      

  2.   

    在sql语句实现不了.
    建议修改存储过程,或者放在客户端用程序实现.
      

  3.   

    如果只能用sql实现,按2楼的方法,再建一个存储过程,在原存储过程的基础上,根据你的需求修改一下,是可行的方案!
      

  4.   

    AcHerat 说的存入全局临时表,跟“insert into 临时表 exec 存储过程”  有什么不一样吗?它能实现2个返回结果集存入临时表?其他的朋友要么说不可能实现,要么说得修改被exce的过程,意思我理解,不过不宜修改它,或者复制一个再修改,那感觉得维护2个相近功能的过程,而且这事是别人做的模块,更麻烦。再等等。
      

  5.   

    http://www.lampchina.net/ask/MTU3ODY4Mw.html
    这些网站干嘛的,为什么我这个贴的问题和回帖,都在这个网站里出现?
    却改了时间。
      

  6.   

    也提醒下,上面那个连接的广告比较多,
    难道csdn的论坛数据库和它是共用的?
      

  7.   


    纯SQL无法区分存储过程返回的多个数据集的。它只能将所有的数据集全都insert到某个表。但可能会报错的,具体要看你的数据集。PS
    这个就叫混分,也叫人肉置顶,
    不然真有高手偶尔降临,你的贴没顶到第一页,
    高手也是不会翻N页找你的问题贴来回答滴...
      

  8.   

    第一种方法: 使用output参数USE AdventureWorks;
    GO
    IF OBJECT_ID ( 'Production.usp_GetList', 'P' ) IS NOT NULL 
        DROP PROCEDURE Production.usp_GetList;
    GO
    CREATE PROCEDURE Production.usp_GetList @product varchar(40) 
        , @maxprice money 
        , @compareprice money OUTPUT
        , @listprice money OUT
    AS
        SELECT p.name AS Product, p.ListPrice AS 'List Price'
        FROM Production.Product p
        JOIN Production.ProductSubcategory s 
          ON p.ProductSubcategoryID = s.ProductSubcategoryID
        WHERE s.name LIKE @product AND p.ListPrice < @maxprice;
    -- Populate the output variable @listprice.
    SET @listprice = (SELECT MAX(p.ListPrice)
            FROM Production.Product p
            JOIN  Production.ProductSubcategory s 
              ON p.ProductSubcategoryID = s.ProductSubcategoryID
            WHERE s.name LIKE @product AND p.ListPrice < @maxprice);
    -- Populate the output variable @compareprice.
    SET @compareprice = @maxprice;
    GO
    另一个存储过程调用的时候:Create Proc Test
    as
    DECLARE @compareprice money, @cost money 
    EXECUTE Production.usp_GetList '%Bikes%', 700, 
        @compareprice OUT, 
        @cost OUTPUT
    IF @cost <= @compareprice 
    BEGIN
        PRINT 'These products can be purchased for less than 
        $'+RTRIM(CAST(@compareprice AS varchar(20)))+'.'
    END
    ELSE
        PRINT 'The prices for all products in this category exceed 
        $'+ RTRIM(CAST(@compareprice AS varchar(20)))+'.'
    第二种方法:创建一个临时表create proc GetUserName
    as
    begin
        select 'UserName'
    endCreate table #tempTable (userName nvarchar(50))
    insert into #tempTable(userName)
    exec GetUserNameselect #tempTable--用完之后要把临时表清空
    drop table #tempTable--需要注意的是,这种方法不能嵌套。例如:  procedure   a   
      begin   
          ...   
          insert   #table   exec   b   
      end   
        
      procedure   b   
      begin   
          ...   
          insert   #table    exec   c   
          select   *   from   #table     
      end   
        
      procedure   c   
      begin   
          ...   
          select   *   from   sometable   
      end  --这里a调b的结果集,而b中也有这样的应用b调了c的结果集,这是不允许的,
    --会报“INSERT EXEC 语句不能嵌套”错误。在实际应用中要避免这类应用的发生。
    第三种方法:声明一个变量,用exec(@sql)执行:1);EXEC 执行SQL语句declare @rsql varchar(250)
            declare @csql varchar(300)
            declare @rc nvarchar(500)
            declare @cstucount int
            declare @ccount int
            set @rsql='(select Classroom_id from EA_RoomTime where zc='+@zc+' and xq='+@xq+' and T'+@time+'=''否'') and ClassroomType=''1'''
            --exec(@rsql)
            set @csql='select @a=sum(teststucount),@b=sum(classcount) from EA_ClassRoom where classroom_id in '
            set @rc=@csql+@rsql
            exec sp_executesql @rc,N'@a int output,@b int output',@cstucount output,@ccount output--将exec的结果放入变量中的做法
            --select @csql+@rsql
            --select @cstucount本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/fredrickhu/archive/2009/09/23/4584118.aspx
      

  9.   

    不知道我发现的是不是正确的啊 如果只是返回两个数据集 那么要用第二个的话用插入临时表的方法,这样可以得到第二个集合,如果要得到第一个的话用openrowset函数这个只返回第一个集合
      

  10.   

    更正下 存入全局临时表这个的前提条件是两个数据集的列数相同,不同的话好像不能存入 通过存入临时表然后top查询出要得那个数据集的数据再放入一个临时表是没问题的。
      

  11.   

    知道微软是怎么做的吗?
    看过微软的存储过程,遇见一个存储过程需要返回多个结果集时,全部用临时表实现。举例:
    CREATE PROC up_B
    AS
    INSERT #tempB 
    SELECT TOP(1) dept_id, dept_address FROM dbo.dept
    GOCREATE PROC up_C
    AS
    INSERT #tempA 
    SELECT TOP(1) dept_id, dept_address FROM dbo.dept
    GOCREATE PROC up_A
    AS
    CREATE TABLE #tempA(dept_id INT, dept_address VARCHAR(100))
    CREATE TABLE #tempB(dept_id INT, dept_address VARCHAR(100))
    EXEC up_B
    EXEC up_CSELECT * FROM #tempA
    SELECT * FROM #tempB
    goEXEC up_A
    所以,还是改一下你的代码,即可。