取原月的计划内容作为计划表目标月的计划内容,取原月的任务内容作为任务表目标月的任务内容 <游标>
ALTER PROCEDURE [dbo].[DustPatrol_Copy_PlanTasks]
-- Add the parameters for the stored procedure here
@NewDate DATETIME,
@OldDate DATETIME
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @PlanID INT
DECLARE @PlanName VARCHAR(50)
DECLARE @InspectorID INT
DECLARE @InspectorGroupID INT
DECLARE @Enabled INT
DECLARE @CreateUserID INT
DECLARE @BeginTime DATETIME
DECLARE @EndTime DATETIME
DECLARE @Remark VARCHAR(1000)
DECLARE @count INT
SET @count=0
BEGIN TRAN --启动事务
PRINT '启动事务'
DECLARE @ERROR int
SET @ERROR=0
BEGIN
print '定义游标'
DECLARE syscolumns_cursor CURSOR FOR
SELECT ID,InspectorID,InspectorGroupID,[Enabled],CreateUserID,BeginTime,EndTime,Remark,PlanName
FROM dbo.tblDustPatrolPlan
WHERE BeginTime BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR(8),@OldDate,120)+'01',120)
AND DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1)
OPEN syscolumns_cursor
FETCH NEXT FROM syscolumns_cursor INTO @PlanID,@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,@BeginTime,@EndTime,@Remark,@PlanName
print '打开游标'
print @PlanID
WHILE(@@fetch_status=0)--判断是否读到了记录
BEGIN
print '插入计划'
print @PlanID
print @BeginTime
INSERT INTO dbo.tblDustPatrolPlan(InspectorID,InspectorGroupID,[Enabled],CreateUserID,BeginTime,EndTime,Remark,PlanName)
VALUES(@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,
CASE WHEN
DAY(@BeginTime)
>
DAY(DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1))
THEN
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),
CONVERT(CHAR(3),DAY(DATEADD(MS,-3,DATEADD(MONTH,1,CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),
MONTH(@NewDate))+'-1 0:00:00.000')))))+CONVERT(char(10),@BeginTime,108)))
ELSE
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(@BeginTime))+CONVERT(char(10),@BeginTime,108))
END,
CASE WHEN
DAY(@EndTime)
>
DAY(DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@NewDate),1)-1))
THEN
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),
CONVERT(CHAR(3),DAY(DATEADD(MS,-3,DATEADD(MONTH,1,CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),
MONTH(@NewDate))+'-1 0:00:00.000')))))+CONVERT(char(10),@EndTime,108)))
ELSE
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(@EndTime))+CONVERT(char(10),@EndTime,108))
END,
@Remark,@PlanName)
SET @count=@count+@@Rowcount
IF (@ERROR <>0) GOTO EXT
print ' 插入任务'
INSERT INTO dbo.tblDustPatrolTask(PlanID,BeginTime,Periodicity,RepeatInterval,AreaID,[Status],InspectorID,[Name],ActionTime,[Description])
SELECT IDENT_CURRENT('tblDustPatrolPlan'),
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(BeginTime))+CONVERT(char(10),BeginTime,108)),
Periodicity,0,AreaID,0,InspectorID,[Name],null,[Description]
FROM dbo.tblDustPatrolTask
WHERE PlanID = @PlanID
and (BeginTime BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR(8),@OldDate,120)+'01',120)
AND DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1))
SET @count=@count+@@Rowcount
PRINT '小计:影响'+CONVERT(VARCHAR,@count)+'行'
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
FETCH NEXT FROM syscolumns_cursor INTO @PlanID,@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,@BeginTime,@EndTime,@Remark,@PlanName
END
print '结束游标'
CLOSE syscolumns_cursor
DEALLOCATE syscolumns_cursor
--异常出口
EXT:
--判断执行状态
IF (@ERROR =0)
BEGIN
PRINT '提交事务'
COMMIT
END
ELSE
BEGIN
PRINT '回滚事务'
ROLLBACK
END
PRINT '共影响'+CONVERT(VARCHAR,@count)+'行'
END
END
-- Add the parameters for the stored procedure here
@NewDate DATETIME,
@OldDate DATETIME
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
DECLARE @PlanID INT
DECLARE @PlanName VARCHAR(50)
DECLARE @InspectorID INT
DECLARE @InspectorGroupID INT
DECLARE @Enabled INT
DECLARE @CreateUserID INT
DECLARE @BeginTime DATETIME
DECLARE @EndTime DATETIME
DECLARE @Remark VARCHAR(1000)
DECLARE @count INT
SET @count=0
BEGIN TRAN --启动事务
PRINT '启动事务'
DECLARE @ERROR int
SET @ERROR=0
BEGIN
print '定义游标'
DECLARE syscolumns_cursor CURSOR FOR
SELECT ID,InspectorID,InspectorGroupID,[Enabled],CreateUserID,BeginTime,EndTime,Remark,PlanName
FROM dbo.tblDustPatrolPlan
WHERE BeginTime BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR(8),@OldDate,120)+'01',120)
AND DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1)
OPEN syscolumns_cursor
FETCH NEXT FROM syscolumns_cursor INTO @PlanID,@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,@BeginTime,@EndTime,@Remark,@PlanName
print '打开游标'
print @PlanID
WHILE(@@fetch_status=0)--判断是否读到了记录
BEGIN
print '插入计划'
print @PlanID
print @BeginTime
INSERT INTO dbo.tblDustPatrolPlan(InspectorID,InspectorGroupID,[Enabled],CreateUserID,BeginTime,EndTime,Remark,PlanName)
VALUES(@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,
CASE WHEN
DAY(@BeginTime)
>
DAY(DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1))
THEN
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),
CONVERT(CHAR(3),DAY(DATEADD(MS,-3,DATEADD(MONTH,1,CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),
MONTH(@NewDate))+'-1 0:00:00.000')))))+CONVERT(char(10),@BeginTime,108)))
ELSE
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(@BeginTime))+CONVERT(char(10),@BeginTime,108))
END,
CASE WHEN
DAY(@EndTime)
>
DAY(DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@NewDate),1)-1))
THEN
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),
CONVERT(CHAR(3),DAY(DATEADD(MS,-3,DATEADD(MONTH,1,CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),
MONTH(@NewDate))+'-1 0:00:00.000')))))+CONVERT(char(10),@EndTime,108)))
ELSE
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(@EndTime))+CONVERT(char(10),@EndTime,108))
END,
@Remark,@PlanName)
SET @count=@count+@@Rowcount
IF (@ERROR <>0) GOTO EXT
print ' 插入任务'
INSERT INTO dbo.tblDustPatrolTask(PlanID,BeginTime,Periodicity,RepeatInterval,AreaID,[Status],InspectorID,[Name],ActionTime,[Description])
SELECT IDENT_CURRENT('tblDustPatrolPlan'),
CONVERT(DATETIME,CONVERT(CHAR(4),YEAR(@NewDate))+'-'+CONVERT(CHAR(2),MONTH(@NewDate))+'-'+CONVERT(CHAR(3),DAY(BeginTime))+CONVERT(char(10),BeginTime,108)),
Periodicity,0,AreaID,0,InspectorID,[Name],null,[Description]
FROM dbo.tblDustPatrolTask
WHERE PlanID = @PlanID
and (BeginTime BETWEEN CONVERT(DATETIME,CONVERT(VARCHAR(8),@OldDate,120)+'01',120)
AND DATEADD(MS,-3,DATEADD(MONTH,1+DATEDIFF(MONTH,0,@OldDate),1)-1))
SET @count=@count+@@Rowcount
PRINT '小计:影响'+CONVERT(VARCHAR,@count)+'行'
SET @ERROR =@ERROR +@@ERROR
IF (@ERROR <>0) GOTO EXT
FETCH NEXT FROM syscolumns_cursor INTO @PlanID,@InspectorID,@InspectorGroupID,@Enabled,@CreateUserID,@BeginTime,@EndTime,@Remark,@PlanName
END
print '结束游标'
CLOSE syscolumns_cursor
DEALLOCATE syscolumns_cursor
--异常出口
EXT:
--判断执行状态
IF (@ERROR =0)
BEGIN
PRINT '提交事务'
COMMIT
END
ELSE
BEGIN
PRINT '回滚事务'
ROLLBACK
END
PRINT '共影响'+CONVERT(VARCHAR,@count)+'行'
END
END