简体   繁体   中英

SQL Server Agent and SSIS packages

I'm trying to pass a variable value from SQL Server Agent job to SSIS package but the variable contains an apostrophe in it causing the SQL Server Agent job to fail

eg In SQL Server Agent at Job Step Properties I'm entering the following details:

Property Path: \Package.Variables[User::VariableName].Properties[Value] Property 
Value: Michael O'Callaghan.

Any idea how to resolve this issue?

If the package is deployed to SSISDB and executed from there, use SSISDB stored procedures to set the value and escape the quote like how you would via T-SQL. The SQL Agent job can then use a T-SQL script for this step instead. The example below uses the set_execution_parameter_value stored procedure to set this value and will still result in "Michael O'Callaghan" being passed in.

DECLARE @execution_id BIGINT
EXEC [SSISDB].[catalog].[create_execution] @package_name=N'Package.dtsx', @execution_id=@execution_id OUTPUT, 
@folder_name=N'Project Folder', @project_name=N'Project', @use32bitruntime=False, @reference_id=Null

DECLARE @var0 SQL_VARIANT = N'Michael O''Callaghan'
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id,  @object_type=30, @parameter_name=N'Name', @parameter_value=@var0

DECLARE @var1 SMALLINT = 1
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id,  @object_type=50, @parameter_name=N'LOGGING_LEVEL', @parameter_value=@var1

EXEC [SSISDB].[catalog].[start_execution] @execution_id

Escape it. Just use a double apostrophe. '' (Not a quotation " , but a apostrophe apostrophe) .

尝试维护配置文件的标准方法(如果您使用的是2008或更少)并通过文件传递变量值。

An alternative way to handle this, and frankly I think the best way, is to use Environment Variables. To my knowledge, this was introduced when Microsoft rolled out the project deployment model with SQL Server 2012 as a replacement to the package deployment model. The package deployment model required that package parameters be specified in a separate XML file to be deployed to the server. With the project deployment model, Microsoft has created a user-friendly user interface in SQL Server to manage this - the XML file has been removed.

In short, Environment Variables allow developers to link package parameters, but not package variables as those are internal to the package itself , to SQL Server and thus expose them on the server. This makes management of identical package parameters that exist across packages (eg, connection managers, network folder locations in FQDN format, etc.) incredibly easy to manage. The idea here is that if packages need to be pointed to a new server or new network folder, then developers can simply change a single value in SQL Server, which would then propogate out to all packages without the need to open, change, and re-deploy the package.

For detailed steps on how to do this, see the following references:

Microsoft: This is a bit dry, but is comprehensive and from the horse's mouth.

SQL Chick: More intuitive and provides screenshots, which I found helpful.

Thanks for your all you suggestions but unfortunately they didn't work, however I built a clever workaround for this.

SQL server agent wraps a variable value in single quote eg specifying Jon Doe in sql server agent, the agent wraps it like this 'Jon Doe' and passes it to the SSIS package, so if you were to enter a value with an apostrophe it would break the sql server agent job and won't execute the SSIS package it would look like this EG passing this value: 'John O' Doe' this would cause the agent the job to break so you need to pass your variable value as : John O''Doe and the agent wraps it as follows: 'John O''''Doe' so you would need to include the following logic in your SSIS package:

Declare @TempVar nVarchar(50) SET @TempVar = REPLACE(?, '''''', CHAR(39))

The above code creates a variable to store the parameter value. It replaces the 4 single quotes to one. CHAR(39) is the ASCII representation of a single quote. This would then cause the variable value to look like John O'Doe. Hope this helps.

The reason I wanted to pass a variable value from the agent as I had to change the variable value very often from the SSIS package it would need to be deployed every time. So this way is faster.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM