sqlserver两条求和sql脚本相加的方法分享,Server自动备份存储过程和视图的方法

1 建立备份数据表

图片 1图片 2

CREATE TABLE [dbo].[ProcBackup](
    [id] [int] IDENTITY(1,1) NOT NULL,
    [name] [sysname] NOT NULL,
    [db] [nvarchar](50) NULL,
    [obj_id] [int] NULL,
    [create_date] [datetime] NOT NULL,
    [modify_date] [datetime] NOT NULL,
    [text] [nvarchar](4000) NULL,
    [type] [nvarchar](5) NULL,
    [remark] [nvarchar](500) NULL,
    [backup_date] [datetime] NULL
) ON [PRIMARY]

GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'自增ID' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'id'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'名称' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'name'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'所在数据库' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'db'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'系统对象id' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'obj_id'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'创建时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'create_date'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'修改时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'modify_date'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'内容' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'text'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'类型' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'type'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'备注' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'remark'
GO

EXEC sys.sp_addextendedproperty @name=N'MS_Description', @value=N'记录时间' , @level0type=N'SCHEMA',@level0name=N'dbo', @level1type=N'TABLE',@level1name=N'ProcBackup', @level2type=N'COLUMN',@level2name=N'backup_date'
GO

ProcBackup

转自:http://www.maomao365.com/?p=7205

2 创建存储过程

图片 3图片 4

create proc proc_backup
as
--插入新增的存储过程
insert into ProcBackup 
select A.name,'db_' as db,A.[object_id] as obj_id,A.create_date,A.modify_date,C.[text],'P' as type,'' as remark,getdate() as backup_date from sys.procedures A 
left join sys.syscomments C on A.[object_id] = C.id 
where A.name not in (select name from ProcBackup) 

--插入修改过的存储过程
insert into ProcBackup 
select A.name,'db_' as db,A.[object_id] as obj_id,A.create_date,A.modify_date,C.[text],'P' as type,'' as remark,getdate() as backup_date from sys.procedures A 
left join ProcBackup B on A.[object_id] = B.obj_id 
left join sys.syscomments C on A.[object_id] = C.id 
where A.modify_date > B.modify_date

--插入新增的视图
insert into ProcBackup 
select A.name,'db_' as db,A.[object_id] as obj_id,A.create_date,A.modify_date,C.[text],'V' as type,'' as remark,getdate() as backup_date from sys.views A 
left join sys.syscomments C on A.[object_id] = C.id 
where A.name not in (select name from ProcBackup) 

--插入修改过的视图
insert into ProcBackup 
select A.name,'db_' as db,A.[object_id] as obj_id,A.create_date,A.modify_date,C.[text],'V' as type,'' as remark,getdate() as backup_date from sys.views A 
left join ProcBackup B on A.[object_id] = B.obj_id 
left join sys.syscomments C on A.[object_id] = C.id 
where A.modify_date > B.modify_date

proc_backup

0.将安装的iso文件解压

摘要:

3 创建SQL Server 代理 作业

在SQL Server代理中创建作业,设置为定时执行存储过程proc_backup即可。

 

图片 5

下文分享两条sql求和脚本,再次求和的方法分享

  1. 使用管理员账户打开 cmd命令行
 /*
     例:
       下文已知两条sql求和脚本,现需对两张不同表的求和记录再次求和   
     */  
     ---对两条求和sql脚本求和的方法
      select sum(q) from 
      (
       select sum(qty)  as q from tableNameA where ...
       union all 
        select sum(qty)  as q from tableNameB where ...
       ) as table

     ---对多条求和sql脚本,再次求和的方法分享
     select sum(q) from 
      (
       select sum(qty)  as q from tableNameA where ...
       union all 
        select sum(qty)  as q from tableNameB where ...
         union all 
        select sum(qty)  as q from tableNameC where ...
        union all 
        select sum(qty)  as q from tableNameD where ...
       ...  
    ) as table
网站地图xml地图