问题描述:有一个存储过程 proc_ptt,它会返回2个数据集,2个数据集的结构各不相同;
这个存储过程暂时不宜修改它;现在有sql语句执行exec proc_ptt,而且期望把它返回的2个数据集都能够先保存下来,我知道,如果是返回一个数据集可以用形如 insert into 临时表 exec proc_ptt,可现在是2个数据集,而且我更需要第二个数据集,那如何获取第2个数据集,然后存储到变量或临时表?
这个存储过程暂时不宜修改它;现在有sql语句执行exec proc_ptt,而且期望把它返回的2个数据集都能够先保存下来,我知道,如果是返回一个数据集可以用形如 insert into 临时表 exec proc_ptt,可现在是2个数据集,而且我更需要第二个数据集,那如何获取第2个数据集,然后存储到变量或临时表?
也不是问 代码里,如c#,如何获取数据集,而是问sql语句如何获取exce 存储过程的返回数据集。看了好多技术帖子,感觉搞技术的,经常是对问题表达和描述不清晰,回帖的对原帖理解不透而回答的驴唇不对马嘴的情形,所以在此啰嗦一句。
建议修改存储过程,或者放在客户端用程序实现.
这些网站干嘛的,为什么我这个贴的问题和回帖,都在这个网站里出现?
却改了时间。
难道csdn的论坛数据库和它是共用的?
纯SQL无法区分存储过程返回的多个数据集的。它只能将所有的数据集全都insert到某个表。但可能会报错的,具体要看你的数据集。PS
这个就叫混分,也叫人肉置顶,
不然真有高手偶尔降临,你的贴没顶到第一页,
高手也是不会翻N页找你的问题贴来回答滴...
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
看过微软的存储过程,遇见一个存储过程需要返回多个结果集时,全部用临时表实现。举例:
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
所以,还是改一下你的代码,即可。