日历

2008 7.7 Mon
  12345
6789101112
13141516171819
20212223242526
2728293031  
«» 2008 - 7 «»

文章搜索

日志文章

2008年05月15日 19:05:58

对数据库里所有表和列执行操作

对数据库里所有表和列执行操作
sysobjects 页首
在数据库内创建的每个对象(约束、默认值、日志、规则、存储过程等)在表中占一行。只有在 tempdb 内,每个临时对象才在该表中占一行。
列名 数据类型 描述
name sysname
对象名。

Id int
对象标识号。

xtype char(2)
对象类型。可以是下列对象类型中的一种:

C = CHECK
约束

D =
默认值或 DEFAULT 约束
F = FOREIGN KEY
约束
L =
日志
FN =
标量函数
IF =
内嵌表函数
P =
存储过程
PK = PRIMARY KEY
约束(类型是 K
RF =
复制筛选存储过程
S =
系统表
TF =
表函数
TR =
触发器
U =
用户表
UQ = UNIQUE
约束(类型是 K
V =
视图
X =
扩展存储过程

uid smallint
所有者对象的用户 ID
info smallint
保留。仅限内部使用。

status int
保留。仅限内部使用。

base_schema_
ver int
保留。仅限内部使用。

replinfo int
保留。供复制使用。

parent_obj int
父对象的对象标识号(例如,对于触发器或约束,该标识号为表 ID)。

crdate datetime
对象的创建日期。

ftcatid smallint
为全文索引注册的所有用户表的全文目录标识符,对于没有注册的所有用户表则为 0

schema_ver int
版本号,该版本号在每次表的架构更改时都增加。

stats_schema_
ver int
保留。仅限内部使用。

type char(2)
对象类型。可以是下列值之一:

C = CHECK
约束

D =
默认值或 DEFAULT 约束

F = FOREIGN KEY
约束
FN =
标量函数

IF =
内嵌表函数
K = PRIMARY KEY
UNIQUE 约束
L =
日志

P =
存储过程
R =
规则
RF =
复制筛选存储过程
S =
系统表
TF =
表函数

TR =
触发器
U =
用户表
V =
视图
X =
扩展存储过程

userstat smallint
保留。
sysstat smallint
内部状态信息。

indexdel smallint
保留。

refdate datetime
留作以后使用。

version int
留作以后使用。

deltrig int
保留。

instrig int
保留。

updtrig int
保留。

seltrig int
保留。

category int
用于发布、约束和标识。

cache smallint
保留。




syscolumns 页首
每个表和视图中的每列在表中占一行,存储过程中的每个参数在表中也占一行。该表位于每个数据库中。
列名 数据类型 描述
name sysname
列名或过程参数的名称。

id int
该列所属的表对象 ID,或与该参数关联的存储过程 ID

xtype tinyint systypes
中的物理存储类型。

typestat tinyint
仅限内部使用。

xusertype smallint
扩展的用户定义数据类型 ID

length smallint systypes
中的最大物理存储长度。

xprec tinyint
仅限内部使用。

xscale tinyint
仅限内部使用。

colid smallint
列或参数 ID

xoffset smallint
仅限内部使用。

bitpos tinyint
仅限内部使用。

reserved tinyint
仅限内部使用。

colstat smallint
仅限内部使用。

cdefault int
该列的默认值 ID

domain int
该列的规则或 CHECK 约束 ID

number smallint
过程分组时(0 表示非过程项)的子过程号。

colorder smallint
仅限内部使用。

autoval varbinary(255)
仅限内部使用。

offset smallint
该列所在行的偏移量;如果为负,表示可变长度行。

status tinyint
用于描述列或参数属性的位图:

0x08 =
列允许空值。

0x10 =
当添加 varchar varbinary 列时,ANSI 填充生效。保留 varchar 列的尾随空格,保留 varbinary 列的尾随零。
0x40 =
参数为 OUTPUT 参数。
0x80 =
列为标识列。

type tinyint systypes
中的物理存储类型。
usertype smallint systypes
中的用户定义数据类型 ID

printfmt varchar(255)
仅限内部使用。

prec smallint
该列的精度级别。

scale int
该列的小数位数。

iscomputed int
表示是否已计算该列的标志:

0 =
未计算。

1 =
已计算。

isoutparam int
表示该过程参数是否是输出参数:
1 =
真。

0 =
假。

isnullable int
表示该列是否允许空值:
1 =
真。

0 =
假。





应用:




获取对象的ID
SELECT id
FROM sysobjects
WHERE (id = OBJECT_ID('VendorInformation')

获取表的列名:
SELECT name
FROM syscolumns
WHERE (id =
(SELECT id
FROM sysobjects
WHERE (id = OBJECT_ID('VendorInformation'))))
ORDER BY colid

将数据库中具有某个列的所有表进行部分修改:
declare @tableName nvarchar(200)
declare cTable cursor for
select name from sysobjects
where id in(
select id from syscolumns where id in
(select id from sysobjects where xtype='U')
and name='VendoriD')
open cTable
fetch next from cTable into @tableName
while @@fetch_status=0
begin
declare @sql nvarchar(4000)
set @sql='update '+@tableName+' Set VendorID='''+'HK'''+'+'+'substring(VendorID,3,len(VendorID)-2)'+' where CompanyID='''+'EH'''+' AND DivisionID='''+'DEFAULT'''+' AND DepartmentID='''+'DEFAULT'''+' AND VendorID like '''+'HS%'''
exec(@sql)
fetch next from cTable into @tableName
end
deallocate cTable

将数据库中具有某个列的所有表进行全部修改修改:
--Function:Change company id.
create procedure enterprise.CustomCompany_ChangeCompanyID @CompanyID as nvarchar(36),
  @NewCompanyID as nvarchar(36)
as
declare @sql as nvarchar(4000)

declare AllTables cursor for
  SELECT [name], object_id FROM sys.objects
  where type = 'U'

open AllTables

declare @TableName as varchar(256)
declare @TableID as int

fetch next from AllTables into @TableName, @TableID

while @@fetch_status = 0
begin
  if exists(select 1 from sys.columns where Object_id = @TableID and name = 'CompanyID')
  begin
    set @sql = 'update ' + @TableName + ' set CompanyID = ''' + @NewCompanyID + ''' where CompanyID = ''' + @CompanyID + ''''
    begin try
      execute(@sql)
      print 'Update ' + @TableName + ' successfully.'
    end try
    begin catch
      print ERROR_MESSAGE()
      print 'Error occured when update ' + @TableName
    end catch
  end
  fetch next from AllTables into @TableName, @TableID
end

close AllTables
deallocate AllTables

Tags: sysobjects   syscolumns  

类别: database |  评论(0) |  浏览(828) |  收藏
发表评论