sys.tables
概述
sys.tables是sqlserver2005版本中新增一个目录视图,它存储了当前数据库中的所有表信息,在功能上大致和sqlserver2005之前版本的select * from sysobjects where xtype=''u''的功能一样。
列说明
列名 数据类型 说明
lob_data_space_id int
filestream_data_space_id int 仅限内部系统使用。
max_column_id_used int 此表曾使用的最大列 ID。
lock_on_bulk_load bit 锁被锁定于大容量装载。有关详细信息,请参阅 sp_tableoption (Transact-SQL)。
uses_ansi_nulls bit 创建表时,SET ANSI_NULLS 数据库选项设置为 ON。
is_replicated bit 1 = 使用快照复制或事务复制发布表。
has_replication_filter bit 1 = 表具有复制筛选器。
is_merge_published bit 1 = 使用合并复制发布表。
is_sync_tran_subscribed bit 1 = 使用立即更新订阅来订阅表。
has_unchecked_assembly_data bit 1 = 表包含的持久化数据依赖于上次 ALTER ASSEMBLY 期间其定义发生更改的程序集。在下一次成功执行 DBCC CHECKDB 或 DBCC CHECKTABLE 后将重置为 0。
text_in_row_limit int Text in row 的最大允许字节数。
0 = 未设置 Text in row 选项。有关详细信息,请参阅 sp_tableoption (Transact-SQL)。
large_value_types_out_of_row bit 1 = 超行存储大值类型。有关详细信息,请参阅 sp_tableoption (Transact-SQL)。
应用示例
1,查看当前数据库的所有表
select * from sys.tables
2,查询数据库中所有表的数据行数
declare @TableName varchar(128)
declare @T_TableRows Table
(
TableName varchar(128) not null,
RowsCount int not null default(0)
)
declare cur_table cursor local for
select name from sys.tables order by name
open cur_table
fetch cur_table into @TableName
while @@fetch_status = 0
begin
insert into @T_TableRows(TableName,RowsCount)
exec(''select '''''' + @TableName + '''''',count(*) from '' + @TableName)
fetch cur_table into @TableName
end
close cur_table
deallocate cur_table
3,删除数据库中所有表中的数据
declare @TableName varchar(128)
declare cur_table cursor local for
select name from sys.tables order by name
open cur_table
fetch cur_table into @TableName
while @@fetch_status = 0
begin
exec(''delete from '' + @TableName + '')
fetch cur_table into @TableName
end
close cur_table
deallocate cur_table