quote: Originally posted by shallu1_gupta Please provide the Complete Code.
Here it is. The basics of using sp_OA extended stored procedures to control DTS packages was taken from Listing B in a Techrepublic article by Jeffrey T. Juday titled "Execute a DTS package from a SQL Server stored procedure". It can be found at http://techrepublic.com.com/5100-6313-5164094.html#Listing%20BThanks for looking at this.NRSET QUOTED_IDENTIFIER ON GOSET ANSI_NULLS ON GO/****** Object: Stored Procedure dbo.p_DTSExportToExcel Script Date: 10/15/2004 9:19:55 AM ******/ALTER PROCEDURE [dbo].p_DTSExportToExcel @ExportWorkbookName VarChar(1024) = 'C:\temp.xls', @ExportWorksheetName VarChar(32) = 'MySheet', @ExportSQLObjectName VarChar(127) = 'tblMySQLTable', @PackageName VarChar(127) = 'Test_ExportToExcelTest3', @DTSResults VarChar(1024) OUTPUTAsBegin-- Set up and run DTS package to import recordsDeclare @HRESULT intDeclare @GVRESULT VarChar(1024) -- used when retrieving value of DTS global variableDeclare @StepResult VarChar(1024)Declare @DTSPackageObject intDeclare @ErrSource intDeclare @ErrDescrip VarChar(255)Declare @ErrMsg VarChar(255)Declare @ServerName VarChar(127)Declare @Flags intSelect @ErrMsg = 'Error running DTS package'-- Create a DTS Package objectSet @DTSResults = 'Creating DTS object...'print @DTSResultsEXEC @HRESULT = sp_OACreate 'DTS.Package2', @DTSPackageObject OUTPUTIF @HRESULT <> 0BEGIN EXEC @HRESULT = sp_OAGetErrorInfo @DTSPackageObject,@ErrSource OUTPUT ,@ErrDescrip OUTPUT RAISERROR (@ErrMsg,11,1) RETURNEND Set @DTSResults = 'Loading DTS package...'print @DTSResultsEXEC @HRESULT = sp_OAMethod @DTSPackageObject, 'LoadFromSqlServer', NULL, @ServerName='MYSQLSERVER', @PackageName=@PackageName, @Flags=256IF @HRESULT <> 0BEGIN PRINT '*** Load Package failed' EXEC @HRESULT = sp_OAGetErrorInfo @DTSPackageObject,@ErrSource OUTPUT ,@ErrDescrip OUTPUT Set @DTSResults = @ErrDescrip print @DTSResults RAISERROR ( @ErrMsg,11,1) RETURNEND-- NOTE: code below is commented out to allow the package to execute and allow us to scan for errors afterwards.-- Set the FailOnError property to true-- Set @DTSResults = 'Setting FailOnError property to TRUE...'-- print @DTSResults-- EXEC @HRESULT = sp_OASetProperty @DTSPackageObject, 'FailOnError', -1 --Set to true-- IF @HRESULT <> 0-- BEGIN-- EXEC @HRESULT = sp_OAGetErrorInfo @DTSPackageObject,@ErrSource OUTPUT ,@ErrDescrip OUTPUT-- RAISERROR ( @ErrMsg,11,1)-- Set @DTSResults = @ErrDescrip-- print @DTSResults-- RETURN-- END -- Call the EXECute methodSet @DTSResults = 'Export Excel worksheet operation begins...' print @DTSResultsEXEC @HRESULT = sp_OAMethod @DTSPackageObject, 'EXECute', NULLIF @HRESULT <> 0BEGIN EXEC @HRESULT = sp_OAGetErrorInfo @DTSPackageObject,@ErrSource OUTPUT ,@ErrDescrip OUTPUT Set @DTSResults = @ErrDescrip print @DTSResults RAISERROR ( @ErrMsg,11,1) RETURNEND -- 2004/10/14 NR *NEW* for testing-- "Translate" this VB code to TSQL-- For i = 1 To oPackage.Steps.Count-- If oPackage.Steps(i).ExecutionResult = DTSStepExecResult_Failure Then-- oPackage.Steps(i).GetExecutionErrorInfo ErrorCode, ErrorSource, ErrorDescription, _-- ErrorHelpFile, ErrorHelpContext, ErrorIDofInterfaceWithError-- MsgBox oPackage.Steps(i).Name & " failed" & vbCrLf & ErrorSource & vbCrLf & ErrorDescription-- End If-- Next iDeclare @Steps intDeclare @stepi VarChar(1024)Declare @stepDesc VarChar(1024)Declare @i intDeclare @StepErrorCode bigintDeclare @StepErrorSource VarChar(1024)Declare @StepErrorDescription VarChar(1024)Declare @StepErrorHelpFile VarChar(1024)Declare @StepErrorHelpContext bigintDeclare @StepErrorIDofInterfaceWithError bigintEXEC @HRESULT = sp_OAGetProperty @DTSPackageObject, 'Steps.Count', @Steps OUTPUTSet @i = 1While @i <= @StepsBegin Set @stepi = 'Steps(' + Cast(@i As VarChar(3)) + ').Description' EXEC @HRESULT = sp_OAGetProperty @DTSPackageObject, @stepi, @StepDesc OUTPUT Set @stepi = 'Steps(' + Cast(@i As VarChar(3)) + ').ExecutionResult' EXEC @HRESULT = sp_OAGetProperty @DTSPackageObject, @stepi, @StepResult OUTPUT print 'Step ' + Cast(@i As VarChar(3)) + ' ' + ISNULL(@StepDesc, '') If @StepResult IS NOT NULL print ' Result:' + Cast(@StepResult As VarChar(10)) Set @stepi = 'Steps(' + Cast(@i As VarChar(3)) + ').GetExecutionErrorInfo' EXEC @HRESULT = sp_OAMethod @DTSPackageObject, @stepi, @StepErrorCode OUTPUT, @StepErrorSource OUTPUT, @StepErrorDescription OUTPUT, @StepErrorHelpFile OUTPUT, @StepErrorHelpContext OUTPUT, @StepErrorIDofInterfaceWithError OUTPUT print ' ErrorCode: ' + IsNull(Cast(@StepErrorCode As VarChar(30)),'') print ' ErrorSource: ' + IsNull(@StepErrorSource,'') print ' ErrorDesc: ' + IsNull(@StepErrorDescription,'') Select @i = @i + 1CONTINUEEnd-- 2004/10/14 NR end of new section for testing--remove the object from memorySet @DTSResults = 'Destroying DTS object...'print @DTSResultsEXEC @HRESULT = sp_OADestroy @DTSPackageObjectIF @HRESULT <> 0BEGIN PRINT '*** Destroy Package failed' EXEC @HRESULT = sp_OAGetErrorInfo @DTSPackageObject,@ErrSource OUTPUT ,@ErrDescrip OUTPUT Set @DTSResults = @ErrDescrip print @DTSResults RAISERROR ( @ErrMsg,11,1) RETURNENDSet @DTSResults = 'Text file successfully imported.'print @DTSResultsENDGOSET QUOTED_IDENTIFIER OFF GOSET ANSI_NULLS ON GOnragamuffin |