zoukankan      html  css  js  c++  java
  • SQL Server中几个扩展存储过程

    --1.返回计算机名称
    execute master..xp_getnetname 
    
    --自动报告计算机网络名的更改
    select SERVERPROPERTY('MachineName') 
    
    --不报告计算机网络名的更改
    select @@SERVERNAME  
    
    
    --2.返回错误日志内容
    if exists(select 1 from sys.tables where name = 'errorLog')
    begin
    	drop table errorLog
    end
    
    create table errorLog
    (
    LogDate datetime,
    ProcessInfo nvarchar(20),
    Text nvarchar(max)
    )
    
    insert into errorlog
    exec master.sys.sp_readerrorlog
    
    select *
    from errorLog
    
    
    --查找存储过程xp_ReadErrorLog
    --发现sys.xp_ReadErrorLog调用了master.dbo.xp_ReadErrorLog
    select *
    from sys.all_sql_modules
    where definition like '%sys.xp_ReadErrorLog%'
    
    
    /*===================================================
    默认情况下除当前的错误日志文件外,还有6个错误日志文件,
    编号越小那么日志越接近当前日期,错误日志文件数的范围:6~99个。
    
    可接受参数@p1,表示要返回哪个错误日志文件的内容,
    可选的参数值:
    		0:默认值,表示返回当前错误日志文件的内容
    		1:表示返回存档编号1的错误日志文件的内容 
    		2:表示返回存档编号2的错误日志文件的内容 
    		3:表示返回存档编号3的错误日志文件的内容 
    		4:表示返回存档编号4的错误日志文件的内容 
    		5:表示返回存档编号5的错误日志文件的内容 
    		6:表示返回存档编号6的错误日志文件的内容 
    =====================================================*/
    exec master.dbo.xp_ReadErrorLog @p1 = 1
    
    
    
    --3.返回服务器上安装的所有OLEDB提供程序
    if exists(select 1 from sys.tables where name = 'oledbProvider')
    begin
    	drop table oledbProvider
    end
    
    create table oledbProvider
    (
    ProviderName nvarchar(50),
    ProcessInfo nvarchar(100),
    Text nvarchar(100)
    )
    
    insert into oledbProvider
    execute master..xp_enum_oledb_providers 
    
    select *
    from oledbProvider
    
    
    
    --4.返回服务器上固定驱动器和可用空间
    if exists(select 1 from sys.tables where name = 'fixeddrivesFreeSpace')
    begin
    	drop table fixeddrivesFreeSpace
    end
    
    create table fixeddrivesFreeSpace
    (
    drive nvarchar(10),   --盘符,不带':\'
    freeSpace bigint      --以MB为单位的空闲空间
    )
    
    insert into fixeddrivesFreeSpace
    execute master..xp_fixeddrives 
    
    select drive '盘符',   
           freeSpace * 1.0 / 1024  '空闲空间(GB)'
    from fixeddrivesFreeSpace
    
    
    
    --5.返回每个存储介质的空闲空间和介质类型
    if exists(select 1 from sys.tables where name = 'availablemediaFreeSpace')
    begin
    	drop table availablemediaFreeSpace
    end
    
    create table availablemediaFreeSpace
    (
    name nvarchar(10),   --盘符,带':\'
    lowFree bigint,      --空闲空间的低于32位的表示
    highFree int,        --空闲空间的高于32位的表示
    mediaType tinyint    --以MB为单位的空闲空间
    )
    
    insert into availablemediaFreeSpace
    exec master.dbo.xp_availablemedia
    
    /*=============================================
    计算方法:
    
    低32位值:正的lowfree或者(65536 * 65536)+负的lowfree 
    高32位值:highfree * (65536 * 65536) 
    
    空闲空间 = 低32位值 + 高32位值
    ===============================================*/
    select name '盘符',   
           lowfree,
           highfree,
           
           case when sign(lowfree) = 1  --正数返回1
                     then lowfree + highfree * (1.0 * 65536 * 65536)
                else lowfree + (1.0 * 65536 * 65536) + highfree * (1.0 * 65536 * 65536) 
           end / 1024 /1024 / 1024 '空闲空间(GB)',
           
           case when mediaType = 1
                     then '软盘'
                when mediaType = 2
                     then '硬盘'
                when mediaType = 8
                     then 'CD-ROM'
           end '存储介质类型'
    from availablemediaFreeSpace
    
    
    
    --6.返回服务器上所有windows本地组 
    execute master..xp_enumgroups 
    
    
    --7.返回指定目录下的目录和文件
    /*========================================
    参数1:目录名
    参数2:目录深度
    参数3:是否显示文件
    ==========================================*/
    --显示c盘下第一层的目录和文件
    execute master..xp_dirtree 'c:',1,1 
    
    
    --显示c盘下第一层的目录,不显示文件
    execute master..xp_dirtree 'c:',1  
    
    
    --显示c盘下所有的目录和文件
    execute master..xp_dirtree 'c:',0,1
    
    
    --显示指定目录下的子目录
    EXEC master.dbo.xp_subdirs 'C:\'
    
    
    
    --8.文件是否存在
    if exists(select 1 from sys.tables where name = 'fileExists')
    begin
    	drop table fileExists
    end
    
    create table fileExists
    (
    fileExists tinyint,             --文件是否存在
    IsDirectory tinyint,            --是否是目录
    parentDirectoryExists tinyint   --父目录是否存在
    )
    
    insert into fileExists
    execute master..xp_fileexist 'c:\c.txt' 
    
    select *
    from fileExists


     查询服务是否启动

    --显示SQL Server的服务名称:MSSQLSERVER
    select @@servicename
    
    
    --查询某个服务的状态,返回CurrentServiceState列:Running.
    --这样就表示正在运行
    EXEC master.dbo.xp_servicecontrol 
    	N'QUERYSTATE',        --查询状态
    	N'MSSQLSERVER'        --改写为你的SQL Server的服务名称
    	


     

  • 相关阅读:
    访问日志不记录静态文件、访问日志切割、静态元素(文件)过期时间
    apache用户认证、域名跳转、Apache访问日志(两种格式)
    apache 和 php 整合、apache配制虚拟机
    PHP5和PHP7的安装、PHP和apache的整合!
    mariaDB的安装 apache的安装
    lamp 相关mysql的安装
    rsync命令 续集 、linux系统日志、screen工具
    rsync命令
    linux cron计划任务、chkconfig 命令、systemd命令、unit 相关、target 相关
    spark_to_es
  • 原文地址:https://www.cnblogs.com/momogua/p/8304594.html
Copyright © 2011-2022 走看看