gpt4 book ai didi

sql - 在 sql server 数据库中查找可用空间的最佳方法?

转载 作者:行者123 更新时间:2023-12-04 13:15:22 24 4
gpt4 key购买 nike

我希望能够找出我的数据库文件中还剩下多少空间,这样我就可以知道何时增加空间,这样当应用程序运行困难时它就不会这样做。

我希望能够编写脚本,这样我就可以定期在多个数据库中运行它。

我有 SQL Server 2000 和 SQL Server 2005 数据库,但我希望能够在两者上运行相同的脚本。

我可以使用 Management Studio 在 2005 数据库上手动执行此操作,但不能在 2000 数据库上执行此操作。

最佳答案

试试 sp_spaceused :

Displays the number of rows, disk space reserved, and disk space used by a table, indexed view, or Service Broker queue in the current database, or displays the disk space reserved and used by the whole database.



我相信这存在于 SQL Server 2000 中,但我无法证明这一点。它在 2005 年和 2008 年有效。

您可以通过 sp_helptext 查看它, 如果你想将它绑定(bind)到一些服务器端逻辑。

编辑 : 在下面扩展我的评论,感谢 http://www.sqlteam.com/forums/topic.asp?TOPIC_ID=82359 的原始贡献者,这是一种按文件分割使用情况的方法:
select
name
, filename
, convert(decimal(12,2),round(a.size/128.000,2)) as FileSizeMB
, convert(decimal(12,2),round(fileproperty(a.name,'SpaceUsed')/128.000,2)) as SpaceUsedMB
, convert(decimal(12,2),round((a.size-fileproperty(a.name,'SpaceUsed'))/128.000,2)) as FreeSpaceMB
from dbo.sysfiles a

关于sql - 在 sql server 数据库中查找可用空间的最佳方法?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/866139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com