JustTroItIn
Starting Member
2 Posts |
Posted - 2010-11-18 : 14:20:51
|
I'm not an SQL expert and only know enough to usually get what I need done by trial and error. I created a script to archive records from a production database into an archive database based on a date, then purge the records from the production database. The script runs fine if I run it as a Query, but not when I schedule it as a job. When running it as a job the status shows it's running the "Start job" with a status of "In progress", but never moves on to "Execute job". It will stay in this state until I cancel the job. I'm running SQL 2005 Standard 9.00.1406.00My script is as follows: STEP1: DECLARE @CutOffDate DATEtime; DECLARE @CTIMEMID datetime;BEGINset @CTIMEMID = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)set @CutOffDate = DATEADD (mm, -47, @CTIMEMID)ENDBEGINIF @CutOffDate > DATEADD(mm,-24, CURRENT_TIMESTAMP) BEGIN RAISERROR ('Cannot copy/archive records younger than 2 years', 16, 1) ENDENDBEGIN TRAN INSERT INTO ArchiveDB.dbo.itemlabel( Part_ID, Created_DateTime, Serial_ID, Parent_Serial_ID, Old_Serial_ID, Quantity, Part_Description, Part_Revision_Level, Label_Type, Label_Format, Site_ID, Location_ID, Lot_Serial_ID, Reference_ID, Dock_Code, PO_Nbr, PO_Line, Supplier_ID, Supplier_Serial_ID, Receiver_ID, Work_Order_ID, Backflush_Operation, Backflush_Line, Production_DateTime, Release_ID, Schedule_Type, Sales_Order_ID, Sales_Order_Line, Schedule_Reference, Shipper_ID, Shipto_ID, Customer_Name, Customer_Part_ID, Customer_PO_Nbr, Ship_DateTime, Ship_Type, Dealer_Order, Void_DateTime, Unit_Measure, Wt_Unit_Measure, Container_Weight, Part_Weight, UM_Conversion, KanBan_Reference_ID, Issued_DateTime, Due_DateTime, Printed_DateTime, Printed_On_Printer, Created_By_UserID, Sort_1, Sort_2, Container_Part_ID, PKG_11, PKG_12, PKG_13, PKG_14, PKG_15, PKG_16, PKG_17, User_Field_1, User_Field_2, User_Field_3, User_Field_4, User_Field_5, User_Field_6, User_Field_7, User_Field_8, User_Field_9, User_Field_10, User_Field_11, User_Field_12, User_Field_13, User_Field_14, User_Field_15, User_Field_16, User_Field_17, User_Field_18, User_Field_19, User_Field_20, User_Field_21, User_Field_22, User_Field_23, User_Field_24, User_Field_25, User_Field_26, User_Field_27, User_Field_28, User_Field_29, User_Field_30, Host_Shipper_ID, Work_Order_Reference, Work_Order_Operation, Receipt_ASN, Created_By_Program, Created_By_ScreenID, User_Date_1, User_Date_2, User_Date_3, User_Date_4, User_Date_5, User_Date_6, User_Date_7, bakflushtagdate, visiondate, updateddate, User_Number_2, User_Number_3, User_Number_4, User_Number_5, User_Number_6, User_Number_7, User_Number_8, User_Number_9, leaktestcount, po_errorcode ) select Part_ID, Created_DateTime, Serial_ID, Parent_Serial_ID, Old_Serial_ID, Quantity, Part_Description, Part_Revision_Level, Label_Type, Label_Format, Site_ID, Location_ID, Lot_Serial_ID, Reference_ID, Dock_Code, PO_Nbr, PO_Line, Supplier_ID, Supplier_Serial_ID, Receiver_ID, Work_Order_ID, Backflush_Operation, Backflush_Line, Production_DateTime, Release_ID, Schedule_Type, Sales_Order_ID, Sales_Order_Line, Schedule_Reference, Shipper_ID, Shipto_ID, Customer_Name, Customer_Part_ID, Customer_PO_Nbr, Ship_DateTime, Ship_Type, Dealer_Order, Void_DateTime, Unit_Measure, Wt_Unit_Measure, Container_Weight, Part_Weight, UM_Conversion, KanBan_Reference_ID, Issued_DateTime, Due_DateTime, Printed_DateTime, Printed_On_Printer, Created_By_UserID, Sort_1, Sort_2, Container_Part_ID, PKG_11, PKG_12, PKG_13, PKG_14, PKG_15, PKG_16, PKG_17, User_Field_1, User_Field_2, User_Field_3, User_Field_4, User_Field_5, User_Field_6, User_Field_7, User_Field_8, User_Field_9, User_Field_10, User_Field_11, User_Field_12, User_Field_13, User_Field_14, User_Field_15, User_Field_16, User_Field_17, User_Field_18, User_Field_19, User_Field_20, User_Field_21, User_Field_22, User_Field_23, User_Field_24, User_Field_25, User_Field_26, User_Field_27, User_Field_28, User_Field_29, User_Field_30, Host_Shipper_ID, Work_Order_Reference, Work_Order_Operation, Receipt_ASN, Created_By_Program, Created_By_ScreenID, User_Date_1, User_Date_2, User_Date_3, User_Date_4, User_Date_5, User_Date_6, User_Date_7, bakflushtagdate, visiondate, updateddate, User_Number_2, User_Number_3, User_Number_4, User_Number_5, User_Number_6, User_Number_7, User_Number_8, User_Number_9, leaktestcount, po_errorcode FROM dbo.itemLabel WHERE created_datetime < @CutOffDateIF @@ERROR <> 0 BEGIN ROLLBACK TRAN RAISERROR ('Error occured while copying data to ArchiveDB.dbo.ItemLabel', 16, 1) ENDIF @@TRANCOUNT > 0 BEGIN COMMIT TRAN END----------------------STEP2: DECLARE @CutOffDate DATEtime; DECLARE @CTIMEMID datetime;BEGINset @CTIMEMID = DATEADD(dd, DATEDIFF(dd,0,getdate()), 0)set @CutOffDate = DATEADD (mm, -47, @CTIMEMID)ENDBEGINIF @CutOffDate > DATEADD(mm,-24, CURRENT_TIMESTAMP) BEGIN RAISERROR ('Cannot copy/archive records younger than 2 years', 16, 1) ENDENDBEGIN TRANDELETE dbo.itemlabelwhere created_datetime < @CutoffdateIF @@ERROR <> 0 BEGIN ROLLBACK TRAN RAISERROR ('Error occured while deleting data from ProductionDB.dbo.itemLabel', 16, 1) ENDIF @@TRANCOUNT > 0 BEGIN COMMIT TRAN END-------------------------------------------I have tried remarking out the RAISERROR messages but it made no difference.The owner of the job is the SA account, so I don't think security is the issue. There are other jobs that run without problem on this server.Any assistance is appreciated. |
|