如何读取数据的所有用户表

Declare @iCount int,@familyTableName varchar(50),@Sql varchar(3000)

Declare @tempTable Table(familyTableName varchar(50) primary key(familyTableName))

Insert @tempTable(familyTableName)

Select Distinct sysobj.name From dbo.sysobjects sysobj where sysobj.xtype='U'

Set @iCount=@@RowCount

While @iCount>0

Begin

Select Top 1 @familyTableName=familyTableName From @tempTable

Set @Sql='if not exists(select 1 from syscolumns where id=object_id('''+@familyTableName+''') and name=''AuditID'') begin'

Set @Sql=@Sql+' alter table '+@familyTableName+' add AuditID int identity(1,1) not null'

Set @Sql=@Sql+' ALTER TABLE [dbo].['+@familyTableName+'] WITH NOCHECK ADD '

Set @Sql=@Sql+'CONSTRAINT [PK_'+@familyTableName+'] PRIMARY KEY NONCLUSTERED'

Set @Sql=@Sql+'(AuditID ASC) ON [PRIMARY] end'

Exec(@Sql)

Delete From @tempTable Where familyTableName=@familyTableName

Set @iCount=@iCount-1

End

posted @ 2009-06-25 16:32  空紫竹  阅读(222)  评论(0编辑  收藏  举报