费了好几个小时才 搞定呀,从中可以看到很多技术.

转移数据的存储过程set ANSI_NULLS ON
转移数据的存储过程
set QUOTED_IDENTIFIER ON
转移数据的存储过程
go
转移数据的存储过程
转移数据的存储过程
-- =============================================
转移数据的存储过程--
 Author:        <Author,,Neil>
转移数据的存储过程--
 Create date: <Create Date,,20070514>
转移数据的存储过程--
 Description:    <Description,,MOVE DATA TO from a table to another table according to datetime>
转移数据的存储过程--
 =============================================
转移数据的存储过程--
test begin
转移数据的存储过程
execute sp_Move_Data 
转移数据的存储过程    
'2007-05-13',
转移数据的存储过程    
'2007-05-14',
转移数据的存储过程    
'table1',
转移数据的存储过程    
'table2'
转移数据的存储过程
print @@rowcount
转移数据的存储过程
declare @exeCount int
转移数据的存储过程
declare @temp  int
转移数据的存储过程
set @exeCount = 3
转移数据的存储过程
set @temp = 0
转移数据的存储过程
begin tran
转移数据的存储过程
select top (@exeCount* from tableName        --这里是2005的新特性:top中可以加入变量,但是要加"()"的.
转移数据的存储过程--
@temp = @@rowcount
转移数据的存储过程
if @@rowcount = @exeCount
转移数据的存储过程    
set @temp = @exeCount
转移数据的存储过程
commit tran
转移数据的存储过程
if @temp = @exeCount
转移数据的存储过程    
print @temp
转移数据的存储过程
转移数据的存储过程
--test end
转移数据的存储过程

转移数据的存储过程
ALTER PROCEDURE [dbo].[sp_Move_Data] 
转移数据的存储过程    
@beginDate nvarchar(20),
转移数据的存储过程    
@endDate nvarchar(20),
转移数据的存储过程    
@originTableName nvarchar(50),        --要移出数据的表
转移数据的存储过程
        @destinationTableName nvarchar(50)      --要移入数据的表
转移数据的存储过程
AS
转移数据的存储过程    
BEGIN
转移数据的存储过程        
DECLARE @tmpRowCount int --临时存储相应行数
转移数据的存储过程
        DECLARE @exeCount varchar(9--每次循环处理的语句条数 
转移数据的存储过程
        SET @exeCount = '3000'
转移数据的存储过程        
BEGIN TRANSACTION
转移数据的存储过程            
EXECUTE ('INSERT INTO [' + @destinationTableName + '] SELECT TOP ' + @exeCount + 
转移数据的存储过程                
' * FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程            
SET @tmpRowCount = @@ROWCOUNT
转移数据的存储过程            
IF @@ERROR > 0 
转移数据的存储过程            
BEGIN
转移数据的存储过程              
GOTO TranRollBack     --这里直接用RollBack语句就会有error tran begin的错误,我也不知道为什么
转移数据的存储过程

转移数据的存储过程,书上都是用的标签,我也用吧.不过我想写过程式的人都会很讨厌GOTO吧.
转移数据的存储过程            
END
转移数据的存储过程            
print @tmpRowCount    --我真的不知道存储过程用什么Debug,只有用print语句了
转移数据的存储过程
            EXECUTE ('DELETE TOP (' + @exeCount +  
转移数据的存储过程                
') FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程
--            print @@ROWCOUNT    --这里会print 3000
转移数据的存储过程--
            print  @tmpRowCount
转移数据的存储过程--
            print @@ROWCOUNT    --但是这里会print 0 ,@@ROWCOUNT对PRINT语句也起作用.
转移数据的存储过程
            PRINT ('DELETE TOP (' + @exeCount +  
转移数据的存储过程                
') FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程            
IF @@ERROR > 0 OR @tmpRowCount <> @@ROWCOUNT    --错误OR插入删除操作相应行数不同
转移数据的存储过程
            BEGIN 
转移数据的存储过程                TranRollBack:
转移数据的存储过程                
print 'ROLLBACK'
转移数据的存储过程                
ROLLBACK TRAN
转移数据的存储过程            
END
转移数据的存储过程            
ELSE 
转移数据的存储过程            
BEGIN
转移数据的存储过程                
print @tmpRowCount
转移数据的存储过程                
COMMIT TRANSACTION
转移数据的存储过程            
END
转移数据的存储过程        
--TRANSACTION OVER
转移数据的存储过程
        print @tmpRowCount
转移数据的存储过程        
WHILE @tmpRowCount = @exeCount
转移数据的存储过程        
BEGIN
转移数据的存储过程            
BEGIN TRANSACTION
转移数据的存储过程            
EXECUTE ('INSERT INTO [' + @destinationTableName + '] SELECT TOP ' + @exeCount + 
转移数据的存储过程                
' * FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程            
IF @@ERROR > 0 
转移数据的存储过程                
GOTO TranRollBack2 
转移数据的存储过程            
SET @tmpRowCount = @@ROWCOUNT
转移数据的存储过程            
print ('INSERT INTO [' + @destinationTableName + '] SELECT TOP ' + @exeCount + 
转移数据的存储过程                
' * FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程            
EXECUTE ('DELETE TOP (' + @exeCount +  
转移数据的存储过程                
') FROM [' + @originTableName + 
转移数据的存储过程                
'] WHERE logTime between ''' + @beginDate + ''' and ''' + @endDate + '''')
转移数据的存储过程            
IF @@ERROR > 0 OR @tmpRowCount <> @@ROWCOUNT    --错误OR插入删除操作相应行数不同
转移数据的存储过程
            BEGIN 
转移数据的存储过程                TranRollBack2:
转移数据的存储过程                
ROLLBACK TRAN
转移数据的存储过程            
END
转移数据的存储过程            
ELSE 
转移数据的存储过程                
COMMIT TRANSACTION
转移数据的存储过程        
--TRANSACTION OVER
转移数据的存储过程
        END
转移数据的存储过程    
END
转移数据的存储过程
转移数据的存储过程
--其实也可以用事务的嵌套的,只是那个技术我还没有掌握,以后学会了再加上吧.
转移数据的存储过程
请各位多提宝贵意见,大家多交流呀.

相关文章:

  • 2021-10-01
  • 2021-09-04
  • 2021-12-19
  • 2021-07-09
  • 2021-06-13
猜你喜欢
  • 2021-10-11
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-03-11
  • 2022-12-23
  • 2021-09-04
相关资源
相似解决方案