首页 > 数据库 >BackUpLogView 系列 - 生成日志数据库脚本(MS Sql Server)

BackUpLogView 系列 - 生成日志数据库脚本(MS Sql Server)

时间:2023-07-02 21:26:23浏览次数:56  
标签:BackUpLogView dbo datediff adddate MS Sql NULL day getdate

 在企业管理器中执行脚本

CREATE DATABASE [BackupLogview] ON PRIMARY
( NAME = N'BackupLogview', FILENAME = N'C:\DATA\BackupLogview.mdf' , SIZE = 3072KB , MAXSIZE = UNLIMITED, FILEGROWTH = 1024KB )
LOG ON
( NAME = N'BackupLogview_log', FILENAME = N'C:\DATA\BackupLogview_log.ldf' , SIZE = 1024KB , MAXSIZE = 2048GB , FILEGROWTH = 10%)
GO
use BackupLogview
Go
CREATE TABLE [dbo].[LogFiles](
[ID] [bigint] IDENTITY(1,1) NOT NULL,
[FileName] [nvarchar](255) NULL,
[FileContent] [nvarchar](max) NULL,
[Adddate] [smalldatetime] NULL,
[BTree] [varchar](50) NULL,
[Tree] [varchar](50) NULL,
[memo] [varchar](150) NULL,
CONSTRAINT [PK__BackupFi__3214EC2762049C14] PRIMARY KEY CLUSTERED
(
[ID] ASC
) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]
GO
CREATE VIEW [dbo].[View_Num]
AS
SELECT COUNT(ID) AS Num, BTree + '-' + Tree AS Class
FROM dbo.LogFiles
WHERE (DATEDIFF(month, Adddate, GETDATE()) < 6) AND (NOT (BTree + '-' + Tree IS NULL))
GROUP BY BTree + '-' + Tree
GO
CREATE VIEW [dbo].[View_Num_Day]
AS
SELECT BTree + ' ' + Tree AS Item, CASE WHEN datediff(day, adddate, getdate()) = 0 THEN '今日' WHEN datediff(day, adddate,
getdate()) = 1 THEN '昨日' WHEN datediff(day, adddate, getdate()) = 2 THEN '前日' END AS Dayof, COUNT(ID)
AS Num
FROM dbo.LogFiles
WHERE (DATEDIFF(day, Adddate, GETDATE()) < 3)
GROUP BY BTree + ' ' + Tree, CASE WHEN datediff(day, adddate, getdate()) = 0 THEN '今日' WHEN datediff(day, adddate, getdate())
= 1 THEN '昨日' WHEN datediff(day, adddate, getdate()) = 2 THEN '前日' END
GO
ALTER TABLE [dbo].[LogFiles] ADD CONSTRAINT [DF_LogFiles_Adddate] DEFAULT (getdate()) FOR [Adddate]

 


-- 创建用户并分配权限
USE [master]
GO
CREATE LOGIN [backuser] WITH PASSWORD = 'backusr'
GO
USE [BackupLogview]
GO
CREATE USER [backuser] FOR LOGIN [backuser]
GO
EXEC sp_addrolemember 'db_datareader', 'backuser';
EXEC sp_addrolemember 'db_datawriter', 'backuser';

TRANSLATE with x English
Arabic Hebrew Polish
Bulgarian Hindi Portuguese
Catalan Hmong Daw Romanian
Chinese Simplified Hungarian Russian
Chinese Traditional Indonesian Slovak
Czech Italian Slovenian
Danish Japanese Spanish
Dutch Klingon Swedish
English Korean Thai
Estonian Latvian Turkish
Finnish Lithuanian Ukrainian
French Malay Urdu
German Maltese Vietnamese
Greek Norwegian Welsh
Haitian Creole Persian  
  TRANSLATE with COPY THE URL BELOW Back EMBED THE SNIPPET BELOW IN YOUR SITE Enable collaborative features and customize widget: Bing Webmaster Portal Back

标签:BackUpLogView,dbo,datediff,adddate,MS,Sql,NULL,day,getdate
From: https://www.cnblogs.com/dentist/p/17521415.html

相关文章

  • SQL注入之Oracle手工注入
    0x00.Oracle注入1.Oracle的数据类型是强匹配的(MYSQL有弱匹配的味道),所以在Oracle进行类似UNION查询数据时候必须让对应位置上的数据类型和表中的列的数据类型是一致的,也可以使用null代替某些无法快速猜测出数据类型的位置。1.基础知识//注释符多行注释:/**/,单行注释:--1......
  • BackUpLogView 系列 - 数据库备份脚本
    一键备份MSserver主机中所有数据库USEmaster;GOCREATEORALTERPROCEDUREdbo.BackupAllDatabases@BackupDirectoryNVARCHAR(255)ASBEGIN--获取当前日期和时间DECLARE@CurrentDateVARCHAR(8)=CONVERT(VARCH......
  • 【springboot】springboot集成mysql
    在pom.xml增加依赖的坐标<dependency><groupId>org.mybatis.spring.boot</groupId><artifactId>mybatis-spring-boot-starter</artifactId><version>2.3.1</version></dependency><!--<dependency&......
  • MySql —— 数据页与索引
    数据库的I/O操作的最小单位是页,InnoDB数据页的默认大小是16KB,意味着数据库每次读写都是以16KB为单位的,一次最少从磁盘中读取16K的内容到内存中,一次最少把内存中的16K内容刷新到磁盘中。数据页包括七个部分       在FileHeader中有两个指针,分别指向上......
  • mysql在新建用户时报错ERROR 1819
    问题描述:mysql在新建用户时报错ERROR1819,如下所示:数据库:mysql5.7.42系统:rhel7.31、问题重现mysql>grantreplicationslave,replicationclienton*.*to'slave'@'192.168.133.91'identifiedby"slave@12345";ERROR1819(HY000):Yourpassworddoe......
  • 面试官问我知不知道 MySQL 的锁,5分钟让他刮目相看是什么?一篇理解
    锁的概念锁机制是用于管理对共享资源的并发访问。InnoDB存储引擎会在行级别上对数据上锁。数据库使用锁是为了支持对共享资源进行并发访问,提供数据的完整性和一致性。lock与latchlatch一般称为闩锁(轻量级的锁),因为其要求锁定的时间必须要非常短。在innoDB存储引擎中,latch可以分为......
  • Mysql基础篇(三)之多表查询
    一.多表关系一对多(多对一)多对一一对一1.一对多(1).案例:部门与员工的关系(2).关系:一个部门对应多个员工,一个员工对应一个部门(3).实现:在多的一方建立外建,指向一的一方的主键2.多对多(1).案例:学生与课程的关系(2).关系:一个学生可以选修多门课程,一门课程也......
  • 面试官问我知不知道 MySQL 的锁,5分钟让他刮目相看
    锁的概念锁机制是用于管理对共享资源的并发访问。InnoDB存储引擎会在行级别上对数据上锁。数据库使用锁是为了支持对共享资源进行并发访问,提供数据的完整性和一致性。lock与latchlatch一般称为闩锁(轻量级的锁),因为其要求锁定的时间必须要非常短。在innoDB存储引擎中,latch可以......
  • MySQL安装
    MySQL5.7的安装下载地址https://dev.mysql.com/downloads/windows/installer/5.7.html选择GAversion稳定正式版在这里卸载!在这里卸载!在这里卸载!环境变量配置win+r键入sysdm.cpl选择-高级->环境变量系统变量->新建......
  • PL/SQL表---table()函数用法
     PL/SQL表---table()函数用法:利用table()函数,我们可以将PL/SQL返回的结果集代替table。simpleexample:1、table()结合数组:*/createorreplacetypet_testasobject(idinteger,rqdate,mcvarchar2(60));createorreplacetypet_test_tableastableo......