博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
归档脚本
阅读量:5741 次
发布时间:2019-06-18

本文共 3440 字,大约阅读时间需要 11 分钟。

USE [dbtest_history]

GO
/****** Object: StoredProcedure [dbo].[csp_backup_ListingOverdueInterest] Script Date: 12/29/2015 13:29:06 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER PROCEDURE [dbo].[csp_backup_ListingOverdueInterest]
AS
BEGIN
DECLARE @tablename VARCHAR(50)
DECLARE @sdate DATETIME
DECLARE @edate DATETIME

IF OBJECT_ID('dbtest_history..filelog', 'U') IS NULL

BEGIN
EXEC ('
use dbtest_history;
CREATE TABLE filelog
(
id INT NOT NULL IDENTITY(1,1) PRIMARY KEY ,
lastFileTime DATETIME NOT NULL ,
tableName nvarchar(50) NOT NULL ,
insertTime DATETIME NOT NULL DEFAULT GETDATE(),
updateTime DATETIME NOT NULL DEFAULT GETDATE(),
isActive BIT NOT NULL DEFAULT 1
) on [PRIMARY]
/*# Owner: nieyan ;Manager: zhaodong #*/
'
)
END

IF ( SELECT MAX(lastFileTime)

FROM dbtest_history.dbo.filelog
) IS NULL
BEGIN
INSERT INTO dbtest_history.dbo.filelog
( lastFileTime ,
tableName
)
SELECT MIN(insertTime) ,
'ListingOverdueInterest'
FROM dbtest.dbo.ListingOverdueInterest with(nolock)
END

 

SELECT @sdate = cast(MAX(lastFileTime) as DATE) ,

@edate = CAST (GETDATE() AS DATE)
FROM dbtest_history.dbo.filelog WITH ( NOLOCK )

WHILE ( @sdate < @edate )

BEGIN
IF OBJECT_ID('tempdb..#tmp_1', 'U') IS NOT NULL
BEGIN
DROP TABLE #tmp_1
END
SELECT IDENTITY( INT,1,1 ) AS 'LogID' ,
[Id]
INTO #tmp_1
FROM dbtest.dbo.ListingOverdueInterest WITH ( NOLOCK )
WHERE UpdateTime >= @sdate
AND UpdateTime < CAST(DATEADD(DAY, 1, @sdate) AS DATE)
IF EXISTS ( SELECT 1
FROM dbtest.dbo.ListingOverdueInterest
WITH ( NOLOCK )
WHERE CAST(UpdateTime AS DATE) = CAST(@sdate AS DATE) )
BEGIN
SET @tablename = 'ListingOverdueInterestBY'
+ CONVERT(VARCHAR(6), @sdate, 112)
+ CASE WHEN DATEPART(DAY, @sdate) <= 10 THEN 'A'
WHEN DATEPART(DAY, @sdate) > 10
AND DATEPART(DAY, @sdate) <= 20
THEN 'B'
WHEN DATEPART(DAY, @sdate) > 20 THEN 'C'
END
END
IF NOT EXISTS ( SELECT name
FROM dbtest_history.sys.tables
WHERE name = @tablename ) AND @tablename IS NOT NULL
BEGIN
EXEC('USE [dbtest_history];
CREATE TABLE '+@tablename+' (
[Id] [varchar](32) NOT NULL,
[ListingId] [int] NOT NULL,
[LoanId] [int] NOT NULL,
[UserAmount] [money] NOT NULL,
[CorpAmount] [money] NOT NULL,
[RecordStatus] [int] NOT NULL,
[TotalUserAmount] [money] NOT NULL,
[TotalCorpAmount] [money] NOT NULL,
[BorrowerId] [int] NOT NULL,
[OverdueLastDate] [datetime] NOT NULL,
[InsertTime] [datetime] NOT NULL DEFAULT GETDATE(),
[UpdateTime] [datetime] NOT NULL DEFAULT GETDATE(),
[IsActive] [bit] NOT NULL DEFAULT 1,
CONSTRAINT [PK_'+@tablename+'] PRIMARY KEY CLUSTERED
(
[Id] ASC
))ON [PRIMARY]
/*# Owner: nieyan ;Manager: zhaodong #*/
')
END
DECLARE @bid INT
DECLARE @eid INT
DECLARE @sql NVARCHAR(4000)
SET @bid = 0
SELECT @eid = MAX(logid)
FROM #tmp_1
WHILE @bid <= @eid
BEGIN
WAITFOR DELAY '00:00:01'
SET @sql = 'insert into dbtest_history.dbo.'
+ @tablename
+ ' select a.* FROM dbtest.dbo.ListingOverdueInterest as a with(nolock) '
+ ' inner join #tmp_1 as b with(nolock) on a.Id = b.Id and b.LogID > '
+ CONVERT(VARCHAR, @bid) + ' and b.LogID <= '
+ CONVERT(VARCHAR, @bid + 10000)
+ ' and b.LogID <= ' + CONVERT(VARCHAR, @eid)
+ ' where not exists(select id from dbtest_history.dbo.'
+ @tablename + ' where id = a.Id) '
--SELECT @sql
EXEC(@sql)
SET @bid = @bid + 10000
END
SET @sdate = CAST(DATEADD(DAY, 1, @sdate) AS DATE)
END
INSERT INTO dbtest_history.dbo.filelog
( lastFileTime ,
tableName
)
VALUES ( GETDATE() ,
'ListingOverdueInterest'
)
END

转载于:https://www.cnblogs.com/sandra/p/5085504.html

你可能感兴趣的文章
【转】VC的MFC中重绘函数的使用总结(整理)
查看>>
JQuery日记_5.13 Sizzle选择器(六)选择器的效率
查看>>
oracle查看经常使用的系统信息
查看>>
Django_4_视图
查看>>
Linux的netstat命令使用
查看>>
lvm讲解,磁盘故障小案例
查看>>
大快网站:如何选择正确的hadoop版本
查看>>
经过这5大阶段,你离Java程序员就不远了!
查看>>
IntelliJ IDEA 连接数据库详细过程
查看>>
thymeleaf 学习笔记-基础篇
查看>>
PHP-X开发扩展
查看>>
android学习笔记——onSaveInstanceState的使用
查看>>
工作中如何做好技术积累
查看>>
怎么用sysLinux做U盘双PE+DOS??
查看>>
Spring Transactional
查看>>
shell脚本实例
查看>>
我的友情链接
查看>>
Windows Phone 7 隔离存储空间资源管理器
查看>>
Microsoft Excel 2000/2003修复工具
查看>>
apache安装报错undefined reference ssl
查看>>