数据库中tmstamp monitor的示例代码
发表于:2025-01-21 作者:千家信息网编辑
千家信息网最后更新 2025年01月21日,这篇文章主要介绍了数据库中tmstamp monitor的示例代码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。USE [DBCent
千家信息网最后更新 2025年01月21日数据库中tmstamp monitor的示例代码
这篇文章主要介绍了数据库中tmstamp monitor的示例代码,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
USE [DBCenter]GO/****** Object: StoredProcedure [dba].[GetRowDiff] Script Date: 2017/5/8 13:06:50 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOalter procedure [dba].[GetRowDiff] asdeclare @databse_name varchar(15),@schema_name varchar(10),@table_name varchar(100),@max_tmstamp_2 bigint,@row_count_2 bigint,@max_tmstamp_1 bigint,@row_count_1 bigint,@datetime datetime,@sql varchar(8000),@record_time_1 varchar(19),@record_time_2 varchar(19)SET NOCOUNT onset @sql=''set @datetime =getdate()truncate table DBCenter..viewTMstamp_diffdeclare mycursor cursor for select aa.databse_name,aa.[schema_name],aa.table_name,aa.max_tmstamp as max_tmstamp_2,aa.row_count as row_count_2,bb.max_tmstamp as max_tmstamp_1,bb.row_count as row_count_1, convert(varchar(19),aa.record_time,120) as record_time_2,convert(varchar(19),bb.record_time,120) as record_time_1 from ( select a.databse_name,a.[schema_name],a.table_name,a.max_tmstamp,a.row_count,a.record_time from [DBCenter].[dbo].[viewMaxTMtamp] a with (nolock) where convert(varchar(10),a.record_time,120)+' '+convert(varchar(2),a.record_time,114) = convert(varchar(10),dateadd(hh,0,getdate()),120)+' '+convert(varchar(2),dateadd(hh,0,getdate()),114) ) as aa join ( select b.databse_name,b.[schema_name],b.table_name,b.max_tmstamp,b.row_count,b.record_time from [DBCenter].[dbo].[viewMaxTMtamp] b with (nolock) where convert(varchar(10),b.record_time,120)+' '+convert(varchar(2),b.record_time,114) = convert(varchar(10),getdate(),120)+' '+convert(varchar(2),dateadd(hh,-1,getdate()),114) ) as bb on aa.databse_name=bb.databse_name and aa.[schema_name]=bb.[schema_name] and aa.table_name=bb.table_name --打开游标 open mycursor --从游标里取出数据赋值到我们刚才声明的2个变量中 fetch next from mycursor into @databse_name,@schema_name,@table_name,@max_tmstamp_2,@row_count_2,@max_tmstamp_1,@row_count_1,@record_time_2,@record_time_1 --判断游标的状态 -- 0 fetch语句成功 ---1 fetch语句失败或此行不在结果集中 ---2 被提取的行不存在 while (@@fetch_status=0) begin set @sql='insert into DBCenter..viewTMstamp_diff([db_name],[schema_name],[table_name],[row_count_diff],[tmstmp_row_count_diff],[tmstmp_diff],[record_time_2],[record_time_1],[date_time]) select '+''''+@databse_name+''''+','+''''+@schema_name+''''+','+''''+@table_name+''''+','+cast((@row_count_2-@row_count_1)as varchar(100))+' as row_count_diff,count(1) as tmstmp_row_count_diff ,' +cast((@max_tmstamp_2-@max_tmstamp_1)as varchar(100))+' as tmstmp_diff,'+''''+convert(varchar(19),@record_time_2,120)+''''+','+''''+convert(varchar(19),@record_time_1,120)+''''+','+''''+convert(varchar(19),getdate(),120)+''''+' from '+@databse_name+'.'+@schema_name+'.'+@table_name+' with (nolock) where cast(TMSTAMP as bigint) >='+cast(@max_tmstamp_1 as varchar(100))+' and cast(TMSTAMP as bigint)<'+cast(@max_tmstamp_2 as varchar(100)) --print @sql EXEC (@sql) set @sql='insert into DBCenter..viewTMstamp_diff_his([db_name],[schema_name],[table_name],[row_count_diff],[tmstmp_row_count_diff],[tmstmp_diff],[record_time_2],[record_time_1],[date_time]) select '+''''+@databse_name+''''+','+''''+@schema_name+''''+','+''''+@table_name+''''+','+cast((@row_count_2-@row_count_1)as varchar(100))+' as row_count_diff,count(1) as tmstmp_row_count_diff ,' +cast((@max_tmstamp_2-@max_tmstamp_1)as varchar(100))+' as tmstmp_diff,'+''''+convert(varchar(19),@record_time_2,120)+''''+','+''''+convert(varchar(19),@record_time_1,120)+''''+','+''''+convert(varchar(19),getdate(),120)+''''+' from '+@databse_name+'.'+@schema_name+'.'+@table_name+' with (nolock) where cast(TMSTAMP as bigint) >='+cast(@max_tmstamp_1 as varchar(100))+' and cast(TMSTAMP as bigint)<'+cast(@max_tmstamp_2 as varchar(100)) --print @sql EXEC (@sql) fetch next from mycursor into @databse_name,@schema_name,@table_name,@max_tmstamp_2,@row_count_2,@max_tmstamp_1,@row_count_1,@record_time_2,@record_time_1 end --关闭游标 close mycursor --撤销游标 DEALLOCATE mycursor SET NOCOUNT offGO
USE [datayesdb]GO/****** Object: StoredProcedure [dba].[GetMaxTMstmp] Script Date: 2017/5/8 14:07:04 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE procedure [dba].[GetMaxTMstmp] @database varchar(20),@schema varchar(20),@tbname nvarchar(100),@datetime datetimeasdeclare @Max_TMstmp bigintdeclare @sql nvarchar(4000)declare @sql2 nvarchar(4000)declare @i int=0DECLARE @startDate1 DATE;DECLARE @startDate DATETIME;DECLARE @endDate DATETIME ;SET NOCOUNT onSET @startDate1=GETDATE();SELECT @startDate=DATEADD(DAY,-0,@startDate1); SET @endDate=DATEADD(d,+1,CONVERT(DATETIME,@startDate1)); --SELECT @startDate startDate,@endDate endDate;--set @tbname='bond'--print @tbnamebegin--print @tbnameset @sql2='select @i=count(1) from sys.columns col with (nolock) join sys.tables tbl with (nolock) on col.object_id=tbl.object_id where tbl.name='+'''' +@tbname+''''+' and col.name in ('+''''+'TMSTAMP'+''''+','+''''+'UPDATE_TIME'+''''+') and tbl.type='+''''+'U'+''''+ ' and tbl.schema_id =schema_id('+''''+@schema+''''+')' --print @sql2exec sp_executesql @sql2 ,N'@i int out' ,@i out--print @iif isnull(@i,0)=2beginset @sql= 'insert into [DBCenter].[dbo].[viewMaxTMtamp] ([databse_name],[schema_name],[table_name],[min_tmstamp],[max_tmstamp],[row_count],[min_update_time],[max_update_time],[record_time]) select ' +''''+@database+''''+','+''''+@schema+''''+','+''''+@tbname+''''+ ', isnull(cast(min(TMSTAMP) as bigint),0)'+', isnull(cast(max(TMSTAMP) as bigint),0)'+',count(1)'+', min(UPDATE_TIME)'+', max(UPDATE_TIME),'+'''' +cast (@datetime as varchar(20))+''''+' from '+@database+'.'+@schema+'.'+@tbname +' with (nolock)'--print @sqlEXEC (@sql)endendSET NOCOUNT off------------------------------------------------------------------------------------------------------------------------- GO
USE [datayesdb]GO/****** Object: StoredProcedure [dba].[GetMaxTMstmp_job] Script Date: 2017/5/8 14:07:45 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGOCREATE procedure [dba].[GetMaxTMstmp_job] asdeclare @database varchar(20),@schema varchar(20),@tblname varchar(100),@datetime datetimeSET NOCOUNT onset @datetime=GETDATE()set @database='datayesdb'declare mycursor cursor for select schema_name(schema_id) [schema],name from sys.tables with (nolock) where type='U' order by [schema],name --打开游标 open mycursor --从游标里取出数据赋值到我们刚才声明的2个变量中 fetch next from mycursor into @schema,@tblname --判断游标的状态 -- 0 fetch语句成功 ---1 fetch语句失败或此行不在结果集中 ---2 被提取的行不存在 while (@@fetch_status=0) begin --print @tblname exec dba.GetMaxTMstmp @database,@schema,@tblname,@datetime fetch next from mycursor into @schema,@tblname end --关闭游标 close mycursor --撤销游标 DEALLOCATE mycursor SET NOCOUNT offGO
感谢你能够认真阅读完这篇文章,希望小编分享的"数据库中tmstamp monitor的示例代码"这篇文章对大家有帮助,同时也希望大家多多支持,关注行业资讯频道,更多相关知识等着你来学习!
游标
数据
篇文章
语句
代码
数据库
示例
成功
变量
状态
结果
价值
兴趣
同时
更多
朋友
知识
编带
行业
资讯
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
宁夏银川软件开发花多少钱
软件开发的单子怎么接
软件开发私活怎么找
百度纠偏数据库
软件开发排行榜功能怎么实现
思科网络技术学院心得体会
江苏网络技术咨询电话多少
网络技术部笔试题
vb软件开发的优点
杨浦区市场软件开发代理商
张槎深信服网络安全号
品色堂服务器一
java显示数据库的图书信息
服务器做raid数据会丢失吗
联网办公设备网络安全事故
广东省网络技术竞赛试题
dota 2无法连接到服务器
python抢购软件开发
个人对软件开发的理解
云服务器如何设置安全
读写数据库d和dump
服务器安全狗证书
共建网络安全议论文800字
网络安全技术培训考试内容
计算机网络技术求职信息
软件开发转测试好不好转
软件开发做到60岁
数据库建表命令是什么
使命召唤4官方服务器
一日计划表软件开发