【Sql Server】存储过程通过作业定时执行按天统计记录

本文涉及的产品
云数据库 RDS SQL Server,基础系列 2核4GB
简介: 通过前两篇文章的学习,我们已经对创建表、存储过程、作业等功能点有所了解本次将结合前面所学习的知识点,创建统计表以及结合作业定时按天以及实时统计域名各个长度的记录值
作者:小5聊基础
简介:一只喜欢全栈方向的程序员,欢迎咨询,尽绵薄之力答疑解惑
编程原则:Write Less Do More

统计效果
image.png

【创建统计表】
以时间字符串作为主键,确保每天只能由唯一一条统计记录

create table domain_count_day(
  id int identity(1,1),
  length_three_count int,
  length_four_count int,
  length_five_count int,
  length_six_count int,
  length_seven_count int,
  length_eight_count int,
  create_time varchar(10) primary key
)

【临时表】
创建临时的同时,需要加一个判断
如果临时表存在,那么可以根据自己业务情况,先删除临时表,然后再通过select into的方法将查询到数据直接插入到临时表里,省了创建临时表设置的字段和类型

使用说明
1)使用object_id方法
2)特别要注意,一定要是(N'tempdb..#temp',N'U')格式,否则容易出现判断不准的情况
3)删除临时表使用drop table关键词,和删除表操作一样
4)select * from into 临时表 from(目标查询) as 别名

--创建临时表
if object_id(N'tempdb..#temp',N'U') is not null
begin
    --删除临时表
    drop table #temp2    
end
select * into #temp2 from(
    select 
    domain_length,
    count(1) as count_length
    from dbo.domain_table
    where convert(varchar(10),create_time,120)=@time_rows
    group by domain_length
    --order by domain_length asc
) as aaa

【创建存储过程】

逻辑说明
1)定义今天和昨天的记录值变量
用于判断当前时间节点是否生成了记录,有记录则更新操作,没有记录则添加操作
2)再定义今天和昨天的时间值变量
用于过滤筛选今天和昨天的统计记录
3)再定义对应38位长度的值变量
4)创建临时表
以今天或昨天时间为筛选记录,以域名长度为分组,进行记录统计,并设置域名长度和总数两个字段,追加到临时表里
5)通过38长度为筛选条件,赋值到对象变量里
6)最后根据是否存在统计记录进行添加和更新

--drop proc countDomainValueDay;
create procedure countDomainValueDay
as
begin 
    
    declare @prev_time_rows int
    declare @time_rows int
    declare @length_three_count int
    declare @length_four_count int
    declare @length_five_count int
    declare @length_six_count int
    declare @length_seven_count int
    declare @length_eight_count int
    declare @day_time_prev varchar(50)
    declare @day_time varchar(50)
    
    -----当前时间的前一天-----
    set @day_time_prev=convert(varchar(10),dateadd(day,-1,getdate()),120)
    
    --创建临时表
    if object_id(N'#temp') is not null
    begin
        --删除临时表
        drop table #temp    
    end
    select * into #temp from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@day_time_prev
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp where domain_length=3
    select @length_four_count=count_length from #temp where domain_length=4
    select @length_five_count=count_length from #temp where domain_length=5
    select @length_six_count=count_length from #temp where domain_length=6
    select @length_seven_count=count_length from #temp where domain_length=7
    select @length_eight_count=count_length from #temp where domain_length=8
    
    --判断日统计记录是否存在 - 前一天
    select @prev_time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time_prev
    
    if @prev_time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time_prev)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time_prev
    end
    -----/当前时间的前一天-----
    
    
    -----当前时间-----
    set @day_time=convert(varchar(10),getdate(),120)
    
    --创建临时表
    if object_id(N'#temp2') is not null
    begin
        --删除临时表
        drop table #temp2    
    end
    select * into #temp2 from(
        select 
        domain_length,
        count(1) as count_length
        from dbo.domain_table
        where convert(varchar(10),create_time,120)=@@day_time
        group by domain_length
        --order by domain_length asc
    ) as aaa

    select @length_three_count=count_length from #temp2 where domain_length=3
    select @length_four_count=count_length from #temp2 where domain_length=4
    select @length_five_count=count_length from #temp2 where domain_length=5
    select @length_six_count=count_length from #temp2 where domain_length=6
    select @length_seven_count=count_length from #temp2 where domain_length=7
    select @length_eight_count=count_length from #temp2 where domain_length=8
    
    --判断日统计记录是否存在 - 当前
    select @time_rows=count(1) from domain_count_day
    where convert(varchar(10),create_time,120)=@day_time
    
    if @time_rows<=0
    begin
        insert into domain_count_day(length_three_count,length_four_count,length_five_count,length_six_count,length_seven_count,length_eight_count,create_time)
        values(@length_three_count,@length_four_count,@length_five_count,@length_six_count,@length_seven_count,@length_eight_count,@day_time)
    end
    else 
    begin
        update domain_count_day set
        length_three_count=@length_three_count,
        length_four_count=@length_four_count,
        length_five_count=@length_five_count,
        length_six_count=@length_six_count,
        length_seven_count=@length_seven_count,
        length_eight_count=@length_eight_count
        where create_time=@day_time
    end
end

【开启作业】
1)作业基本信息
image.png

2)步骤设置 数据库这里容易选错,如果没注意的话
image.png

3)设置计划 可以使用上一篇文章设置好的定时计划
image.png

4)开始作业
image.png

【统计效果】
从统计效果可以得到一个有意思的结论
1)首位出现的概率稍微小一点
2)越靠近中间,值就越对称接近

image.png

相关文章
|
4月前
|
存储 SQL 数据库连接
C#程序调用Sql Server存储过程异常处理:调用存储过程后不返回、不抛异常的解决方案
本文分析了C#程序操作Sql Server数据库时偶发的不返回、不抛异常问题,并提出了解决思路。首先解析了一个执行存储过程的函数`ExecuteProcedure`,其功能是调用存储过程并返回影响行数。针对代码执行被阻塞但无异常的情况,文章总结了可能原因,如死锁、无限循环或网络问题等。随后提供了多种解决方案:1) 增加日志定位问题;2) 使用异步操作提升响应性;3) 设置超时机制避免阻塞;4) 利用线程池分离主线程;5) 通过信号量同步线程;6) 监控数据库连接状态确保可用性。这些方法可有效应对数据库操作中的潜在问题,保障程序稳定性。
371 11
|
11月前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第18天】SQL Server 存储过程具有提高性能、增强安全性、代码复用和易于维护等优点。它可以减少编译时间和网络传输开销,通过权限控制和参数验证提升安全性,支持代码共享和复用,并且便于维护和版本管理。然而,存储过程也存在可移植性差、开发和调试复杂、版本管理问题、性能调优困难和依赖数据库服务器等缺点。使用时需根据具体需求权衡利弊。
255 1
|
11月前
|
存储 SQL 缓存
SQL Server存储过程的优缺点
【10月更文挑战第22天】存储过程具有代码复用性高、性能优化、增强数据安全性、提高可维护性和减少网络流量等优点,但也存在调试困难、移植性差、增加数据库服务器负载和版本控制复杂等缺点。
422 1
|
11月前
|
存储 SQL 数据库
SQL Server存储过程的优缺点
【10月更文挑战第17天】SQL Server 存储过程是预编译的 SQL 语句集,存于数据库中,可重复调用。它能提高性能、增强安全性和可维护性,但也有可移植性差、开发调试复杂及可能影响数据库性能等缺点。使用时需权衡利弊。
189 3
|
11月前
|
存储 SQL 数据库
Sql Server 存储过程怎么找 存储过程内容
Sql Server 存储过程怎么找 存储过程内容
595 1
|
11月前
|
存储 SQL 数据库
SQL Server 临时存储过程及示例
SQL Server 临时存储过程及示例
151 3
|
11月前
|
存储 SQL 安全
|
11月前
|
存储 SQL 数据库
使用SQL创建视图和存储过程
使用SQL创建视图和存储过程
111 0
|
5月前
|
SQL 数据库 数据安全/隐私保护
数据库数据恢复——sql server数据库被加密的数据恢复案例
SQL server数据库数据故障: SQL server数据库被加密,无法使用。 数据库MDF、LDF、log日志文件名字被篡改。 数据库备份被加密,文件名字被篡改。
|
2月前
|
SQL XML Java
配置Spring框架以连接SQL Server数据库
最后,需要集成Spring配置到应用中,这通常在 `main`方法或者Spring Boot的应用配置类中通过加载XML配置或使用注解来实现。
242 0