执行脚本任务抛出错误“包执行返回DTSER_FAILURE(1)”

时间:2015-09-03 11:35:21

标签: ssis

我有一个SSIS包,它将从一组excel文件加载数据,然后将这些文件存档到指定的文件夹。

excel文件存储在一个文件夹中,在该文件夹中我有存档文件夹。

以下是我的脚本任务代码供参考。

public void Main()
        {
            // TODO: Add your code here
            string sourceDir =  Dts.Variables["User::strFilePath"].Value.ToString();
            string destDir = Dts.Variables["User::strArchivePath"].Value.ToString();

            DirectoryInfo di = new DirectoryInfo(sourceDir);
            string[] sDirFiles = Directory.GetFiles(sourceDir);
            FileInfo[] fi = di.GetFiles("*.xls");
            int fileCnt = fi.Length;
                for (int i = 0; i < fileCnt; i++)
                {
                    String filename = fi[i].Name;
                    string[] splitFilename = filename.Split('.');

                    DateTime dt = DateTime.Now;
                    string ArchiveDate = String.Format("{0:ddMMMyyyy}", dt);

                    string sourceFileName = filename;
                    string sourceFilePath = sourceDir + filename;
                    string destinationFileName = splitFilename[0] + '_' + ArchiveDate + '.' + splitFilename[1];
                    string destinationPath = destDir + destinationFileName;


                    //MessageBox.Show("Source File " + sourceFilePath + " to destination " + destinationPath);
                    if (File.Exists(destinationPath))
                        File.Delete(destinationPath);
                    // To move a file or folder to a new location:
                    System.IO.File.Move(sourceFilePath, destinationPath);
                }
            Dts.TaskResult = (int)ScriptResults.Success;
        }

sourceDir和destDir是提供源文件文件夹和存档文件夹路径的变量。当我从visual studio运行它时,该包正常工作。

我已将其部署为使用部署实用程序通过创建清单文件作为作业运行。当我运行作业时,我的归档脚本任务中出现错误。下面是它的截图。

enter image description here

我在stackoverflow中搜索了解决方案但是提供的解决方案并没有解决我的问题。

Problem using SQL Agent to run SSIS Packages - fails with “DTSER_FAILURE(1)”

DTSX package runs in Visual Studio but not when called from a Database Job

我已经为这两个文件夹授予了对SQL服务器的读写访问权限。仍然得到同样的错误?

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:0)

尝试在您的项目中选择64位 - &gt;属性 - &gt; Dubugging - &GT; Run64BitTime这可以解决问题。

答案 1 :(得分:0)

我用文件系统任务替换了脚本任务,但它工作正常。我还创建了一个代理帐户来运行包含我的系统凭据的包,以便它可以访问指定的文件夹。

创建用于运行SQL作业的代理帐户的步骤,以便对于寻找它的用户有所帮助..

在SQL中创建一个用户帐户,您需要在该帐户下运行作业 为创建的用户帐户创建凭据。

    --Script #1 - Creating a credential to be used by proxy
USE MASTER
GO 
--Drop the credential if it is already existing 
IF EXISTS (SELECT 1 FROM sys.credentials WHERE name = N'SSISProxyCredentials') 
BEGIN 
DROP CREDENTIAL [SSISProxyCredentials] 
END 
GO 
CREATE CREDENTIAL [SSISProxyCredentials] 
WITH IDENTITY = N'<Insert the Username>', 
SECRET = N'abcd@0987' 
GO

创建代理帐户并关联创建的凭据

--Script #2 - Creating a proxy account 
USE msdb
GO 
--Drop the proxy if it is already existing 
IF EXISTS (SELECT 1 FROM msdb.dbo.sysproxies WHERE name = N'SSISProxyDemo') 
BEGIN 
EXEC dbo.sp_delete_proxy 
@proxy_name = N'SSISProxyDemo' 
END 
GO 
--Create a proxy and use the same credential as created above 
EXEC msdb.dbo.sp_add_proxy 
@proxy_name = N'SSISProxyDemo', 
@credential_name=N'SSISProxyCredentials', 
@enabled=1 
GO 
--To enable or disable you can use this command 
EXEC msdb.dbo.sp_update_proxy 
@proxy_name = N'SSISProxyDemo', 
@enabled = 1 --@enabled = 0 
GO

向SQL Server代理子系统授予代理帐户

USE msdb
GO 
--You can view all the sub systems of SQL Server Agent with this command
--You can notice for SSIS Subsystem id is 11 
EXEC sp_enum_sqlagent_subsystems 
GO

--Grant created proxy to SQL Agent subsystem 
--You can grant created proxy to as many as available subsystems 
EXEC msdb.dbo.sp_grant_proxy_to_subsystem 
@proxy_name=N'SSISProxyDemo', 
@subsystem_id=11 --subsystem 11 is for SSIS as you can see in the above image 
GO 
--View all the proxies granted to all the subsystems 
EXEC dbo.sp_enum_proxy_for_subsystem

授予对安全主体的代理访问权限

USE msdb
GO 
--Grant proxy account access to security principals that could be
--either login name or fixed server role or msdb role
--Please note, Members of sysadmin server role are allowed to use any proxy 
EXEC msdb.dbo.sp_grant_login_to_proxy 
@proxy_name=N'SSISProxyDemo' 
,@login_name=N'<Insert the Username>' 
--,@fixed_server_role=N'' 
--,@msdb_role=N'' 
GO 
--View logins provided access to proxies 
EXEC dbo.sp_enum_login_for_proxy 
GO

最后将代理帐户与包步骤相关联。这也可以通过工作向导来完成。

EXEC msdb.dbo.sp_add_jobstep @job_id=@jobId, @step_name=N'SSISPackageCall', 
@step_id=1, 
@cmdexec_success_code=0, 
@on_success_action=1, 
@on_success_step_id=0, 
@on_fail_action=2, 
@on_fail_step_id=0, 
@retry_attempts=0, 
@retry_interval=0, 
@os_run_priority=0, @subsystem=N'SSIS', 
@command=N'/FILE "C:\Package.dtsx" /CHECKPOINTING OFF /REPORTING E', 
@database_name=N'master', 
@flags=0, 
@proxy_name = N'SSISProxyDemo';

感谢我对帖子的有价值的回复..