我有通过SQLCMD.EXE
运行的下一个查询use [AxDWH_Central_Reporting]
GO
EXEC sp_spaceused @updateusage = N'TRUE'
GO
返回2个表...和uglu输出文件与下一个conent:
Changed database context to 'AxDWH_Central_Reporting'. database_name Pdatabase_size Punallocated space --------------------------------------------------------------------------------------------------------------------------------P------------------P------------------ AxDWH_Central_Reporting P10485.69 MB P7436.85 MB reserved Pdata Pindex_size Punused ------------------P------------------P------------------P------------------ 3121176 KB P3111728 KB P7744 KB P1704 KB ----------------------------------------------------------------
是否可以缩短它?确切地说,我只需要database_name和database_size值。我尝试了SQL查询,如
SELECT database_name, database_size FROM (EXEC sp_spaceused @updateusage = N'TRUE') AS tbl1
但这不起作用。
答案 0 :(得分:2)
sp_spaceused 是系统存储过程。您可以在master数据库中查看management studio中的源代码。将代码复制到您自己的过程中,修改它以仅返回您需要的过程。
答案 1 :(得分:0)
答案 2 :(得分:0)
如果必须使用存储过程,则可以将其插入到表变量中,然后从那里进行选择以获取所需的信息。
另一个选项,如果您可以控制存储过程,则传入一个select参数,如下所示。如果你不能做我所谓的@SelectClause,那么一旦你插入表格就可以做一个简单的选择。
INSERT INTO @atttable (RowID, Name, ID, AttributePosition, AVTable, KeyField, EntityNameField, Virtual, DataType, AttributeListName, AttributeRequired, AttributeUnique)
EXEC [dbo].[SomeStoredProcedure]
@SelectClause='ROW_NUMBER() OVER(ORDER BY AttributePosition) RowID, AttributeName, AttributeID, AttributePosition, EntityAVTableName, EntityKeyField, EntityNameField, Virtual, AttributeDataType, AttributeListName, AttributeRequired, AttributeUnique',
@WhereClause=@EntityWhereClause
答案 3 :(得分:0)
我打开了sp_spaceused系统存储过程并执行了我需要的SELECT:)
declare
@dbname sysname,
@dbsize bigint,
@logsize bigint,
@reservedpages bigint
select
@reservedpages = sum(a.total_pages)
from
sys.partitions p join sys.allocation_units a on p.partition_id = a.container_id left join sys.internal_tables it on p.object_id = it.object_id
select
@dbsize = sum(convert(bigint,case when status & 64 = 0 then size else 0 end)),
@logsize = sum(convert(bigint,case when status & 64 <> 0 then size else 0 end))
from
dbo.sysfiles
select
'database name' = db_name(),
'database size' = ltrim(str((convert (dec (15,2),@dbsize) + convert (dec (15,2),@logsize)) * 8192 / 1048576,15,2) + ' MB'),
'unallocated space' = ltrim(str((case when @dbsize >= @reservedpages then
(convert (dec (15,2),@dbsize) - convert (dec (15,2),@reservedpages)) * 8192 / 1048576 else 0 end),15,2) + ' MB')