读取在sql 2005中返回多个记录集的输出存储过程

发布于 2024-07-30 09:47:52 字数 1159 浏览 5 评论 0原文

我正在尝试以编程方式访问执行系统存储过程 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

长亭外,古道边 2024-08-06 09:47:52

对于获取存储过程返回的第二个结果集的原始问题,我没有解决方案。 但在这种情况下,您是否可以使用此脚本来代替,它应该为您提供大致相同的信息,并且这是一个漂亮、干净的 T-SQL 脚本:

SELECT 
    t.NAME AS TableName,
    i.name as indexName,
    p.[Rows],
    sum(a.total_pages) as TotalPages, 
    sum(a.used_pages) as UsedPages, 
    sum(a.data_pages) as DataPages,
    (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
    (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
    (sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY 
    object_name(i.object_id) 

如果您仍然坚持使用存储的 sp_spaceused proc,这个解决方案怎么样:使用未记录的(但非常有用)sp_MSforeachtable存储过程为数据库中的每个表迭代调用sp_spaceused

EXEC sp_MSforeachtable @command1="EXEC 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:

SELECT 
    t.NAME AS TableName,
    i.name as indexName,
    p.[Rows],
    sum(a.total_pages) as TotalPages, 
    sum(a.used_pages) as UsedPages, 
    sum(a.data_pages) as DataPages,
    (sum(a.total_pages) * 8) / 1024 as TotalSpaceMB, 
    (sum(a.used_pages) * 8) / 1024 as UsedSpaceMB, 
    (sum(a.data_pages) * 8) / 1024 as DataSpaceMB
FROM 
    sys.tables t
INNER JOIN      
    sys.indexes i ON t.OBJECT_ID = i.object_id
INNER JOIN 
    sys.partitions p ON i.object_id = p.OBJECT_ID AND i.index_id = p.index_id
INNER JOIN 
    sys.allocation_units a ON p.partition_id = a.container_id
WHERE 
    t.NAME NOT LIKE 'dt%' AND
    i.OBJECT_ID > 255 AND   
    i.index_id <= 1
GROUP BY 
    t.NAME, i.object_id, i.index_id, i.name, p.[Rows]
ORDER BY 
    object_name(i.object_id) 

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 call sp_spaceused iteratively for each table in your database:

EXEC sp_MSforeachtable @command1="EXEC sp_spaceused '?'"

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

转身以后 2024-08-06 09:47:52

如果您使用.NET,当您使用结果填充数据集时,它将为每个结果集创建一个数据表。

If you are using .NET, when you fill a DataSet with the results, it will create a datatable for each result set.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文