读取在sql 2005中返回多个记录集的输出存储过程
我正在尝试以编程方式访问执行系统存储过程 sp_spaceused 的结果。
如果您将对象的名称作为参数传递,则 sp 返回一个记录集,您可以像这样读取该记录集,
CREATE TABLE #TempTable
( [Table_Name] varchar(50),
Row_Count int,
Table_Size varchar(50),
Data_Space_Used varchar(50),
Index_Space_Used varchar(50),
Unused_Space varchar(50)
)
insert into #TempTable EXEC( 'sp_spaceused "tablexx"' )
select * from #TempTable
Table_Name Row_Count Table_Size Data_Space_Used Index_Space_Used Unused_Space
------------ ----------- ----------- ---------------- ----------------- ------------
tablexx 67 64 KB 16 KB 48 KB 0 KB
(1 row(s) affected)
(1 row(s) affected)
问题是该存储过程在不带参数使用时返回两个不同的记录集,如下所示:
sp_spaceused
database_name database_size unallocated space
-------------- -------------- ------------------
Convenios 11622.75 MB 3.16 MB
reserved data index_size unused
------------- ------------- ----------- ---------
11897696 KB 11784392 KB 103264 KB 10040 KB
我只想获取database_size 到变量中...所以问题实际上是如何以编程方式访问返回多个记录集的存储过程返回的数据...
我怎样才能实现这一点?
I'm trying to programmatically access the result of executing the system stored procedure sp_spaceused.
if you pass the name of an object as a parameter, the sp returns a recordset which you can read like this
CREATE TABLE #TempTable
( [Table_Name] varchar(50),
Row_Count int,
Table_Size varchar(50),
Data_Space_Used varchar(50),
Index_Space_Used varchar(50),
Unused_Space varchar(50)
)
insert into #TempTable EXEC( 'sp_spaceused "tablexx"' )
select * from #TempTable
Table_Name Row_Count Table_Size Data_Space_Used Index_Space_Used Unused_Space
------------ ----------- ----------- ---------------- ----------------- ------------
tablexx 67 64 KB 16 KB 48 KB 0 KB
(1 row(s) affected)
(1 row(s) affected)
the problem is that this stored procedure, when used with no parameters returns two different recordset, like this:
sp_spaceused
database_name database_size unallocated space
-------------- -------------- ------------------
Convenios 11622.75 MB 3.16 MB
reserved data index_size unused
------------- ------------- ----------- ---------
11897696 KB 11784392 KB 103264 KB 10040 KB
I just want to get the database_size into a variable... So the problem in fact is how to programmatically access the data returned by a stored procedure that returns multiple recordset...
How can I achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
对于获取存储过程返回的第二个结果集的原始问题,我没有解决方案。 但在这种情况下,您是否可以使用此脚本来代替,它应该为您提供大致相同的信息,并且这是一个漂亮、干净的 T-SQL 脚本:
如果您仍然坚持使用存储的
sp_spaceused
proc,这个解决方案怎么样:使用未记录的(但非常有用)sp_MSforeachtable
存储过程为数据库中的每个表迭代调用sp_spaceused
:在这种情况下,您会得到一个数据库中每个表的行,您应该能够捕获该行并将其插入到您的“空间使用表”中。
希望这有帮助!
马克
I don't have a solution for your original problem of getting hold of the second result set returned by the stored procedure. But in this case here, could you maybe use this script instead, which should give you roughly the same information, and it's a nice, clean T-SQL script:
If you still insist on using the
sp_spaceused
stored proc, how about this solution: use the undocumented (but extremely helpful)sp_MSforeachtable
stored proc to callsp_spaceused
iteratively for each table in your database:In this case, you get one row per table in the database and you should be able to catch that and insert it into your "space used table".
Hope this helps!
Marc
如果您使用.NET,当您使用结果填充数据集时,它将为每个结果集创建一个数据表。
If you are using .NET, when you fill a DataSet with the results, it will create a datatable for each result set.