数据库管理脚本(一个session只能执行一次?)

 


/****** Object:  Schema [DBAdmin]    Script Date: 02/28/2012 15:50:56 ******/
CREATE SCHEMA [DBAdmin] AUTHORIZATION [dbo]
GO

 

CREATE TABLE [DBAdmin].[_Doing_DDL_Log](
 [PostTime] [datetime] NULL,
 [DB] [varchar](100) NULL,
 [DB_User] [nvarchar](100) NULL,
 [Event] [nvarchar](100) NULL,
 [TSQL] [nvarchar](4000) NULL
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'提交时间' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log', @level2type=N'COLUMN',@level2name=N'PostTime'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'操作库' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log', @level2type=N'COLUMN',@level2name=N'DB'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'操作用户' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log', @level2type=N'COLUMN',@level2name=N'DB_User'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'操作事件类型' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log', @level2type=N'COLUMN',@level2name=N'Event'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'使用语句' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log', @level2type=N'COLUMN',@level2name=N'TSQL'
GO

EXEC sys.sp_addextendedproperty @name=N'存储对象', @value=N'简易记录几个关键库的DDL操作记录,用于查看库中对象的增删改情况,通过这几个库的数据库触发器自动写入' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log'
GO

EXEC sys.sp_addextendedproperty @name=N'中文名', @value=N'记录表:DDL操作' , @level0type=N'SCHEMA',@level0name=N'DBAdmin', @level1type=N'TABLE',@level1name=N'_Doing_DDL_Log'
GO


CREATE CLUSTERED INDEX [idx__Doing_DDL_Log] ON [DBAdmin].[_Doing_DDL_Log]
(
 [PostTime] DESC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO
CREATE NONCLUSTERED INDEX [idx__Doing_DDL_Log_2] ON [DBAdmin].[_Doing_DDL_Log]
(
 [DB] ASC,
 [Event] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, SORT_IN_TEMPDB = OFF, IGNORE_DUP_KEY = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
GO

CREATE function [DBAdmin].[F_Spaceused] --- 2003/05/19 14:00
(@objname nvarchar(776)  
)     
returns @tmp
table(name varchar(50),rows int,reserved varchar(50),data varchar(50),indx_size varchar(50),
  unused varchar(50))
as
begin

declare @id int   
  ,@type character(2)
  ,@pages bigint  
  ,@dbname sysname
  ,@dbsize bigint
  ,@logsize bigint
  ,@reservedpages  bigint
  ,@usedpages  bigint
  ,@rowCount bigint


if @objname IS NOT NULL
begin
 select @dbname = parsename(@objname, 3)

 SELECT @id = object_id, @type = type FROM sys.objects (readpast) WHERE object_id = object_id(@objname)

 IF @type = 'SQ'
  SELECT @id = object_id FROM sys.internal_tables (readpast)  WHERE parent_id = @id and internal_type = 201 --ITT_ServiceQueue

end

 SELECT
  @reservedpages = SUM (reserved_page_count),
  @usedpages = SUM (used_page_count),
  @pages = SUM (
   CASE
    WHEN (index_id < 2) THEN (in_row_data_page_count + lob_used_page_count + row_overflow_used_page_count)
    ELSE lob_used_page_count + row_overflow_used_page_count
   END
   ),
  @rowCount = SUM (
   CASE
    WHEN (index_id < 2) THEN row_count
    ELSE 0
   END
   )
 FROM sys.dm_db_partition_stats (readpast)
 WHERE object_id = @id;


 IF (SELECT count(*) FROM sys.internal_tables (readpast)  WHERE parent_id = @id AND internal_type IN (202,204,211,212,213,214,215,216)) > 0
 BEGIN
  SELECT
   @reservedpages = @reservedpages + sum(reserved_page_count),
   @usedpages = @usedpages + sum(used_page_count)
  FROM sys.dm_db_partition_stats p (readpast) , sys.internal_tables it (readpast)
  WHERE it.parent_id = @id AND it.internal_type IN (202,204,211,212,213,214,215,216) AND p.object_id = it.object_id;
 END

insert @tmp
 SELECT
  name = OBJECT_NAME (@id),
  rows = convert (char(11), @rowCount),
  reserved = LTRIM (STR (@reservedpages * 8, 15, 0) + ' KB'),
  data = LTRIM (STR (@pages * 8, 15, 0) + ' KB'),
  index_size = LTRIM (STR ((CASE WHEN @usedpages > @pages THEN (@usedpages - @pages) ELSE 0 END) * 8, 15, 0) + ' KB'),
  unused = LTRIM (STR ((CASE WHEN @reservedpages > @usedpages THEN (@reservedpages - @usedpages) ELSE 0 END) * 8, 15, 0) + ' KB')

return

end

GO

 

CREATE view [DBAdmin].[V_数据字典]                     
   as                     
SELECT                         
 d.name AS 英文表名,                     
  ISNULL(h.[value], '') AS 中文表名,  
  ISNULL(j.[value], '') AS 表别名,   
   ISNULL(i.[value], '') AS 存储对象,               
   isnull((select rows from DBAdmin.f_spaceused(d.name)),0) as 表数据量,             
   isnull((select reserved from DBAdmin.f_spaceused(d.name)),'') as 已分配空间,           
     isnull((select data from DBAdmin.f_spaceused(d.name)),'') as 数据空间,           
      isnull((select indx_size from DBAdmin.f_spaceused(d.name)),'') as 索引空间,           
       isnull((select unused from DBAdmin.f_spaceused(d.name)),'') as 未用空间,             
      a.colorder AS 字段序号, a.name AS 英文列名, ISNULL(g.[value], '') AS 中文列名,                     
      CASE WHEN COLUMNPROPERTY(a.id,                            
      a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识列,                      
                                 
      CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空,                       
    case when COLUMNPROPERTY(a.id, a.name, 'IsComputed')=1 then '√' else '' end AS 计算列 ,                      
 CASE WHEN EXISTS                           
          (SELECT 1                           
 FROM dbo.sysindexes si (readpast) INNER JOIN                           
               dbo.sysindexkeys sik  (readpast)  ON si.id = sik.id AND si.indid = sik.indid INNER JOIN                           
               dbo.syscolumns sc  (readpast) ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN                           
               dbo.sysobjects so  (readpast) ON so.name = si.name AND so.xtype = 'PK'                           
 WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键,                  
 isnull((                 
 select top 1 definition                 
 from sys.check_constraints (readpast)                  
 where parent_object_id=d.id and parent_column_id=a.colid                 
 ),'') as Check约束,                 
 ISNULL(e.text, '') AS 默认值,                
  isnull((select top 1 cx.name                  
 from syscolumns ax (readpast) ,sysobjects bx (readpast)  ,sysobjects  (readpast) cx               
where ax.id=bx.id and ax.domain=cx.id                
and d.id=bx.id and a.colid=ax.colid ),'') as 绑定规则,                 
 isnull((select top 1 ee.name                   
 from  sys.foreign_key_columns aa (readpast) ,                   
syscolumns bb  (readpast) ,                   
sysobjects cc (readpast) ,                   
syscolumns dd  (readpast) ,                   
sysobjects ee  (readpast)                    
where aa.parent_column_id=bb.colid                   
and aa.parent_object_id=cc.id                    
and bb.id=cc.id                    
and aa.referenced_column_id=dd.colid                   
and aa.referenced_object_id=ee.id                    
and dd.id=ee.id and bb.colid=a.colid and cc.id=d.id),'') as 参照表,                   
 isnull((select top 1 dd.name                   
 from  sys.foreign_key_columns aa(readpast),                   
syscolumns bb  (readpast) ,                   
sysobjects cc (readpast) ,                   
syscolumns dd  (readpast) ,                   
sysobjects ee  (readpast)                    
where aa.parent_column_id=bb.colid                   
and aa.parent_object_id=cc.id                    
and bb.id=cc.id                    
and aa.referenced_column_id=dd.colid                   
and aa.referenced_object_id=ee.id                    
and dd.id=ee.id and bb.colid=a.colid and cc.id=d.id),'') as 参照列,                   
      b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name, 'PRECISION')                            
 AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数,                            
d.crdate AS 创建时间,                            
 CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END AS 更改时间                      
                         
FROM dbo.syscolumns a  (readpast) LEFT OUTER JOIN                           
      dbo.systypes b  (readpast) ON a.xtype = b.xusertype INNER JOIN                           
      dbo.sysobjects d  (readpast) ON a.id = d.id AND d.xtype = 'U' AND                      
      d.name not in ('sysdiagrams')    and                      
      d.status >= 0 LEFT OUTER JOIN                           
      dbo.syscomments e (readpast)  ON a.cdefault = e.id LEFT OUTER JOIN               
      sys.extended_properties g  (readpast) ON a.id = g.major_id AND a.colid = g.minor_id AND                           
      g.name = 'MS_Description'     LEFT OUTER JOIN                       
      sys.extended_properties h  (readpast) ON a.id = h.major_id AND 0= h.minor_id AND                           
      h.name = '中文名' LEFT OUTER JOIN         
         sys.extended_properties i  (readpast) ON a.id = i.major_id AND 0= i.minor_id AND                           
      i.name = '存储对象' LEFT OUTER JOIN    
          sys.extended_properties j  (readpast) ON a.id = j.major_id AND 0= j.minor_id AND                           
      j.name = '别名'
     
     

GO

 

 

Create TRIGGER [TS_Recording_DDL_Logs]
ON DATABASE
FOR DDL_DATABASE_LEVEL_EVENTS
AS
BEGIN
SET ANSI_PADDING ON
SET ANSI_NULLS ON
SET QUOTED_IDENTIFIER ON
DECLARE @data XML
SET @data = EVENTDATA()
begin try
INSERT DBAdmin._Doing_DDL_Log

   (PostTime, DB,DB_User, Event, TSQL)
   VALUES
   (GETDATE(), db_name(),
   CONVERT(nvarchar(100), CURRENT_USER),
   @data.value('(/EVENT_INSTANCE/EventType)[1]', 'nvarchar(100)'),
   @data.value('(/EVENT_INSTANCE/TSQLCommand)[1]', 'nvarchar(2000)') )    ;
end try
BEGIN CATCH
END CATCH  
SET ANSI_PADDING OFF
SET ANSI_NULLS OFF
SET QUOTED_IDENTIFIER OFF
END

 

GO

SET ANSI_NULLS OFF
GO

SET QUOTED_IDENTIFIER OFF
GO

DISABLE TRIGGER [TS_Recording_DDL_Logs] ON DATABASE
GO

ENABLE TRIGGER [TS_Recording_DDL_Logs] ON DATABASE
GO


 
alter view [DBAdmin].[V_数据字典]                       
   as                       
SELECT                           
 d.name AS 英文表名,                       
  ISNULL(h.[value], '') AS 中文表名,    
  ISNULL(j.[value], '') AS 表别名,     
   ISNULL(i.[value], '') AS 存储对象,                 
   isnull((select rows from DBAdmin.f_spaceused(schema_name(d.uid)+'.'+d.name)),0) as 表数据量,               
   isnull((select reserved from DBAdmin.f_spaceused(schema_name(d.uid)+'.'+d.name)),'') as 已分配空间,             
     isnull((select data from DBAdmin.f_spaceused(schema_name(d.uid)+'.'+d.name)),'') as 数据空间,             
      isnull((select indx_size from DBAdmin.f_spaceused(schema_name(d.uid)+'.'+d.name)),'') as 索引空间,             
       isnull((select unused from DBAdmin.f_spaceused(schema_name(d.uid)+'.'+d.name)),'') as 未用空间,               
      a.colorder AS 字段序号, a.name AS 英文列名, ISNULL(g.[value], '') AS 中文列名,                       
      CASE WHEN COLUMNPROPERTY(a.id,                              
      a.name, 'IsIdentity') = 1 THEN '√' ELSE '' END AS 标识列,                        
                                   
      CASE WHEN a.isnullable = 1 THEN '√' ELSE '' END AS 允许空,                         
    case when COLUMNPROPERTY(a.id, a.name, 'IsComputed')=1 then '√' else '' end AS 计算列 ,                        
 CASE WHEN EXISTS                             
          (SELECT 1                             
 FROM dbo.sysindexes si (readpast) INNER JOIN                             
               dbo.sysindexkeys sik  (readpast)  ON si.id = sik.id AND si.indid = sik.indid INNER JOIN                             
               dbo.syscolumns sc  (readpast) ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN                             
               dbo.sysobjects so  (readpast) ON so.name = si.name AND so.xtype = 'PK'                             
 WHERE sc.id = a.id AND sc.colid = a.colid) THEN '√' ELSE '' END AS 主键,                    
 isnull((                   
 select top 1 definition                   
 from sys.check_constraints (readpast)                    
 where parent_object_id=d.id and parent_column_id=a.colid                   
 ),'') as Check约束,                   
 ISNULL(e.text, '') AS 默认值,                  
  isnull((select top 1 cx.name                    
 from syscolumns ax (readpast) ,sysobjects bx (readpast)  ,sysobjects  (readpast) cx                 
where ax.id=bx.id and ax.domain=cx.id                  
and d.id=bx.id and a.colid=ax.colid ),'') as 绑定规则,                   
 isnull((select top 1 ee.name                     
 from  sys.foreign_key_columns aa (readpast) ,                     
syscolumns bb  (readpast) ,                     
sysobjects cc (readpast) ,                     
syscolumns dd  (readpast) ,                     
sysobjects ee  (readpast)                      
where aa.parent_column_id=bb.colid                     
and aa.parent_object_id=cc.id                      
and bb.id=cc.id                      
and aa.referenced_column_id=dd.colid                     
and aa.referenced_object_id=ee.id                      
and dd.id=ee.id and bb.colid=a.colid and cc.id=d.id),'') as 参照表,                     
 isnull((select top 1 dd.name                     
 from  sys.foreign_key_columns aa(readpast),                     
syscolumns bb  (readpast) ,                     
sysobjects cc (readpast) ,                     
syscolumns dd  (readpast) ,                     
sysobjects ee  (readpast)                      
where aa.parent_column_id=bb.colid                     
and aa.parent_object_id=cc.id                      
and bb.id=cc.id                      
and aa.referenced_column_id=dd.colid                     
and aa.referenced_object_id=ee.id                      
and dd.id=ee.id and bb.colid=a.colid and cc.id=d.id),'') as 参照列,                     
      b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name, 'PRECISION')                              
 AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, 'Scale'), 0) AS 小数位数,                              
d.crdate AS 创建时间,                              
 CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END AS 更改时间                        
                           
FROM dbo.syscolumns a  (readpast) LEFT OUTER JOIN                             
      dbo.systypes b  (readpast) ON a.xtype = b.xusertype INNER JOIN                             
      dbo.sysobjects d  (readpast) ON a.id = d.id AND d.xtype = 'U' AND                        
      d.name not in ('sysdiagrams')    and                        
      d.status >= 0 LEFT OUTER JOIN                             
      dbo.syscomments e (readpast)  ON a.cdefault = e.id LEFT OUTER JOIN                 
      sys.extended_properties g  (readpast) ON a.id = g.major_id AND a.colid = g.minor_id AND                             
      g.name = 'MS_Description'     LEFT OUTER JOIN                         
      sys.extended_properties h  (readpast) ON a.id = h.major_id AND 0= h.minor_id AND                             
      h.name = '中文名' LEFT OUTER JOIN           
         sys.extended_properties i  (readpast) ON a.id = i.major_id AND 0= i.minor_id AND                             
      i.name = '存储对象' LEFT OUTER JOIN      
          sys.extended_properties j  (readpast) ON a.id = j.major_id AND 0= j.minor_id AND                             
      j.name = '别名'  

你可能感兴趣的:(数据库管理脚本(一个session只能执行一次?))