首页 > 代码库 > 日志、字段备注查询、自增ID联系设置、常用存储过程

日志、字段备注查询、自增ID联系设置、常用存储过程

-----获取数据字典SQL(表字段说明)
SELECT 
    [Table Name] = OBJECT_NAME(c.object_id), 
    [Column Name] = c.name, 
    [Description] = ex.value 
FROM 
    sys.columns c 
LEFT OUTER JOIN 
    sys.extended_properties ex 
ON 
    ex.major_id = c.object_id 
    AND ex.minor_id = c.column_id 
    AND ex.name = ‘MS_Description‘ 
WHERE 
    OBJECTPROPERTY(c.object_id, ‘IsMsShipped‘)=0 
    -- AND OBJECT_NAME(c.object_id) = ‘your_table‘ 
ORDER 
    BY OBJECT_NAME(c.object_id), c.column_id
    --第二种

SELECT 
      CASE WHEN a.colorder = 1 THEN d.name ELSE ‘‘ END AS 表名, 
      CASE WHEN a.colorder = 1 THEN isnull(f.value, ‘‘) ELSE ‘‘ END AS 表说明, 
      a.colorder AS 字段序号, a.name AS 字段名, CASE WHEN COLUMNPROPERTY(a.id, 
      a.name, ‘IsIdentity‘) = 1 THEN ‘√‘ ELSE ‘‘ END AS 标识, 
     CASE WHEN EXISTS
           (SELECT 1
        FROM dbo.sysindexes si INNER JOIN
               dbo.sysindexkeys sik ON si.id = sik.id AND si.indid = sik.indid INNER JOIN
              dbo.syscolumns sc ON sc.id = sik.id AND sc.colid = sik.colid INNER JOIN
               dbo.sysobjects so ON so.name = si.name AND so.xtype = ‘PK‘
         WHERE sc.id = a.id AND sc.colid = a.colid) THEN ‘√‘ ELSE ‘‘ END AS 主键, 
      b.name AS 类型, a.length AS 长度, COLUMNPROPERTY(a.id, a.name, ‘PRECISION‘) 
      AS 精度, ISNULL(COLUMNPROPERTY(a.id, a.name, ‘Scale‘), 0) AS 小数位数, 
      CASE WHEN a.isnullable = 1 THEN ‘√‘ ELSE ‘‘ END AS 允许空, ISNULL(e.text, ‘‘) 
AS 默认值, ISNULL(g.[value], ‘‘) AS 字段说明, d.crdate AS 创建时间, 
     CASE WHEN a.colorder = 1 THEN d.refdate ELSE NULL END AS 更改时间
FROM dbo.syscolumns a LEFT OUTER JOIN
      dbo.systypes b ON a.xtype = b.xusertype INNER JOIN
      dbo.sysobjects d ON a.id = d.id AND d.xtype = ‘U‘ AND 
      d.status >= 0 LEFT OUTER JOIN
      dbo.syscomments e ON a.cdefault = e.id LEFT OUTER JOIN
      sys.extended_properties g ON a.id = g.major_id AND a.colid = g.minor_id AND 
      g.name = ‘MS_Description‘ LEFT OUTER JOIN
      sys.extended_properties f ON d.id = f.major_id AND f.minor_id = 0 AND 
      f.name = ‘MS_Description‘
ORDER BY d.name, 字段序号


----表名解释
    SELECT a.Name,b.Value
     from sys.Objects as a left outer join sys.extended_properties as b on a.Object_id=b.major_id
     where a.type_desc=‘USER_TABLE‘ and a.schema_id=1 and b.minor_id=0
     
 --查询表的默认值

if object_id(‘tb_test‘) is not null
drop table tb_test
Go
create table tb_test(id int,name varchar(50) default ‘abc‘,num int default 5)
insert  tb_test(id) select 1 ,‘222‘,3
      UNION ALL select 1,‘FF‘,100
      UNION ALL select 1,‘WE‘
go
declare @tbname varchar(50)
set @tbname=‘tb_test‘--表名
select @tbname as tbname,c.name as colname,
  replace(replace(replace(replace(b.[text],‘(‘‘‘,‘‘),‘‘‘)‘,‘‘),‘((‘,‘‘),‘))‘,‘‘) as defaultvalue
 from sysconstraints a 
  join syscomments b on a.constid=b.id  
  join syscolumns c on a.id=c.id and a.colid=c.colid
  where a.id=object_id(@tbname) 
    and object_name(a.constid) like ‘%df%‘  
     

---获取索引数据详细信息

SELECT 
      CASE WHEN b.keyno = 1 THEN c.name ELSE ‘‘ END AS 表名, 
      CASE WHEN b.keyno = 1 THEN a.name ELSE ‘‘ END AS 索引名称, d.name AS 列名, 
      b.keyno AS 索引顺序, CASE indexkey_property(c.id, b.indid, b.keyno, ‘isdescending‘) 
      WHEN 1 THEN ‘降序‘ WHEN 0 THEN ‘升序‘ END AS 排序, CASE WHEN p.id IS NULL 
      THEN ‘‘ ELSE ‘√‘ END AS 主键, CASE INDEXPROPERTY(c.id, a.name, ‘IsClustered‘) 
      WHEN 1 THEN ‘√‘ WHEN 0 THEN ‘‘ END AS 聚集, CASE INDEXPROPERTY(c.id, 
      a.name, ‘IsUnique‘) WHEN 1 THEN ‘√‘ WHEN 0 THEN ‘‘ END AS 唯一, 
      CASE WHEN e.id IS NULL THEN ‘‘ ELSE ‘√‘ END AS 唯一约束, 
      a.OrigFillFactor AS 填充因子, c.crdate AS 创建时间, c.refdate AS 更改时间
FROM dbo.sysindexes a INNER JOIN
      dbo.sysindexkeys b ON a.id = b.id AND a.indid = b.indid INNER JOIN
      dbo.syscolumns d ON b.id = d.id AND b.colid = d.colid INNER JOIN
      dbo.sysobjects c ON a.id = c.id AND c.xtype = ‘U‘ LEFT OUTER JOIN
      dbo.sysobjects e ON e.name = a.name AND e.xtype = ‘UQ‘ LEFT OUTER JOIN
      dbo.sysobjects p ON p.name = a.name AND p.xtype = ‘PK‘
WHERE (OBJECTPROPERTY(a.id, N‘IsUserTable‘) = 1) AND (OBJECTPROPERTY(a.id, 
      N‘IsMSShipped‘) = 0) AND (INDEXPROPERTY(a.id, a.name, ‘IsAutoStatistics‘) = 0)
ORDER BY c.name, a.name, b.keyno

 

--
      
 --查询操作日志
 DBCC SQLPERF(LOGSPACE)  --数据库日志使用情况
 DBCC log (psydbnew, type=3 )
   
 DBCC log ( {dbid|dbname}, [, type={0|1|2|3|4}] )
参数: Dbid or dbname - 任一数据库的ID或名字   
type - 输出结果的类型:   
0 - 最少信息(operation, context, transaction id)   
1 - 更多信息(plus flags, tags, row length)   
2 - 非常详细的信息(plus object name, index name,page id, slot id)   
3 - 每种操作的全部信息   
4 - 每种操作的全部信息加上该事务的16进制信息
SELECT *  from::fn_dblog (null, null)   
              where operation in(‘LOP_INSERT_ROWS‘,‘LOP_DELETE_ROWS‘,‘LOP_MODIFY_COLUMNS‘)  
              
              
 ---自增ID 被删除后,新增数据ID保持连续性
create table a_test (id int identity(1,1) primary key,dd char(4))
insert a_test select ‘1eee‘union all select ‘eeee‘union all select ‘1eee‘union all select ‘eeee‘
select * from a_test
--方法1
truncate table a_test
--方法2
DBCC CHECKIDENT (‘a_test‘, RESEED, 3)
--解释说明
dbcc checkident (‘table_name‘, reseed, new_reseed_value)
/* 当前值设置为 new_reseed_value。如果自创建表后没有将行插入该表,
则在执行 DBCC CHECKIDENT 后插入的第一行将使用 new_reseed_value 作为标识。否则,下一个插入的行将使用 new_reseed_value + 1*/
---自增ID 被删除后,新增数据ID保持连续性    
--------------常用存储过程
 

exec master.dbo.sp_help tb----获取表结构信息
exec master.dbo.sp_helptext procname|viewname|functionname    ----获取存储过程、视图、函数等内容
exec master.dbo.sp_stored_procedures----获取存储过程列表
exec master.dbo.xp_availablemedia;-- 获得当前所有驱动器
exec master.dbo.xp_subdirs ‘c:‘-- 获得子目录列表
exec master.dbo.xp_dirtree ‘c:‘--获得所有子目录的目录树结构及对应的深度
exec master.dbo.xp_cmdshell  ‘ type e:\结婚失败.txt‘-- 查看某个文件的内容。
exec master.dbo.xp_cmdshell ‘dir e:\*.txt ‘;--查找指定驱动器盘符下 后缀是txt的文件
exec master.dbo.xp_cmdshell  ‘dir e:‘;--获取指定驱动器下根目录的数据信息
exec master.dbo.xp_cmdshell ‘dir e:\*.txt /s ‘;--查找指定驱动器盘符是所有目录下 后缀是txt的文件
exec master.dbo.xp_cmdshell ‘dir e:\*.txt /s/a ‘;--查找指定驱动器盘符是所有目录下 后缀是txt的文件及详细信息
exec master.dbo.xp_cmdshell ‘dir e:  /s/a‘;--指定驱动器盘符下 每个目录存在的文件及大小、创建时间
exec master.dbo.xp_fileexist   N‘d:/Program   Files/Microsoft   SQLServer2008/MSSQL/BACKUP/psydbnew.dat‘ ---查询某个盘符下的文件是否存在
exec master.dbo.sp_addumpdevice   N‘disk‘,   N‘bakdevice‘,   N‘D:/BACKUP/bakdevice‘   ----添加备份设备、
exec master.dbo.sp_dropdevice   N‘bakdevice‘ --删除备份设备
exec master.dbo.sp_rename   ‘test.dsd‘,‘dd‘   ---  更改字段名  
select   convert(sysname,   serverproperty(N‘servername‘))  --得到SQL   SERVER   的服务器名  
exec master.dbo.xp_msver   N‘ProductVersion‘,   N‘Language‘,   N‘Platform‘,   N‘WindowsVersion‘,   N‘ProcessorCount‘,   N‘PhysicalMemory‘  ---得到SQL   SERVER   平台信息     
exec master.dbo.xp_instance_regwrite   N‘HKEY_LOCAL_MACHINE‘,   N‘SOFTWARE/Microsoft/MSSQLServer/MSSQLServer‘,   ‘LoginMode‘,   N‘REG_DWORD‘,   1  ---修改SQL   SERVER实例的登陆模式
  1---Windows认证模式   
  2---SQL和Windows认证模式 
---截断事务日志 
backup   log   database_name   with   NO_LOG|TRUNCATE_ONLY  
exec master.dbo.xp_instance_regwrite   N‘HKEY_CURRENT_USER‘,   N‘Software/Microsoft/MSSQLServer‘,   N‘LastBackupFileDir‘,REG_SZ,   N‘D:/Program   Files/Microsoft   SQLServer2008/backup/MSSQL$FANHUI/BACKUP/‘ --  改写备份路径

日志、字段备注查询、自增ID联系设置、常用存储过程