如何獲取SQL Server資料庫裡表的占用容

2021-04-29 00:28:37 字數 1673 閱讀 4040

如何獲取sql server資料庫裡表的占用容

其實只要使用系統內建的儲存過程sp_spaceused就可以得到表的相關資訊

如:sp_spaceused 'tablename'

以下是為了方便寫的乙個儲存過程,目的是把當前的所有表的相關資訊全部都儲存在乙個指定的表裡面

create procedure get_tableinfo as

if not exists (select * from dbo.sysobjects where id = object_id(n'[dbo].[tablespaceinfo]') and objectproperty(id, n'isusertable') = 1)

create table  tablespaceinfo                         --建立結果儲存表

(nameinfo varchar(50) , 

rowsinfo int , reserved varchar(20) , 

datainfo varchar(20)  , 

index_size varchar(20) , 

unused varchar(20) )

delete from tablespaceinfo --清空資料表

declare @tablename varchar(255)  --表名稱

declare @cmdsql varchar(500)

declare info_cursor cursor for 

select o.name  

from dbo.sysobjects o where objectproperty(o.id, n'istable') = 1 

and o.name not like n'#%%'  order by o.name

open info_cursor

fetch next from info_cursor 

into @tablename 

while @@fetch_status = 0

begin

if exists (select * from dbo.sysobjects where id = object_id(@tablename) and objectproperty(id, n'isusertable') = 1)

execute sp_executesql 

n'insert into tablespaceinfo  exec sp_spaceused @tbname',

n'@tbname varchar(255)',

@tbname = @tablename

fetch next from info_cursor 

into @tablename 

end

close info_cursor

deallocate info_cursor

go 執行儲存過程

exec get_tableinfo

查詢執行該儲存過程後得到的結果

select *

from tablespaceinfo 

order by cast(left(ltrim(rtrim(reserved)) , len(ltrim(rtrim(reserved)))-2) as int) desc

獲取SQL Server 資料庫結構

select 表名 case when a.colorder 1 then d.name else end,表說明 case when a.colorder 1 then isnull f.value,else end,字段序號 a.colorder,欄位名 a.name,字段說明 isnull g...

sqlserver獲取第n行資料

如何在乙個沒有主鍵的表中獲取第n行資料,在sql2005中可以用row number,但是必須指定排序列,否則你就不得不用select into來過渡到臨時表並增加乙個排序字段。用光標的fetch absolute語句可以獲取絕對行數下的某行資料,測試 如下 set nocount on 建立測試環...

sqlserver獲取第n行資料

如何在乙個沒有主鍵的表中獲取第n行資料,在sql2005中可以用row number,但是必須指定排序列,否則你就不得不用select into來過渡到臨時表並增加乙個排序字段。用光標的fetch absolute語句可以獲取絕對行數下的某行資料,測試 如下 set nocount on 建立測試環...