Package的每一次执行都是一个Execution Instance,都有一个唯一的ExecutionID;可以指定package执行时parameter的值。
1,使用 catalog.create_execution 存储过程创建一个Execution Instance
Creates an instance of execution in the Integration Services catalog.
Syntax
create_execution [ @folder_name = folder_name , [ @project_name = ] project_name , [ @package_name = ] package_name [ , [ @reference_id = ] reference_id ] [ , [ @use32bitruntime = ] use32bitruntime ] , [ @execution_id = ] execution_id OUTPUT
Arguments
- [ @folder_name = ] folder_name
-
The name of the folder that contains the package that is to be executed. The folder_name is nvarchar(128). - [ @project_name = ] project_name
-
The name of the project that contains the package that is to be executed. The project_name is nvarchar(128). - [ @package_name = ] package_name
-
The name of the package that is to be executed. The package_name is nvarchar(260). - [ @reference_id = ] reference_id
-
A unique identifier for an environment reference. This parameter is optional. The reference_id is bigint. - [ @use32bitruntime = ] use32bitruntime
-
Indicates if the 32-bit runtime should be used to run the package on a 64-bit operating system. Use the value of 1 to execute the package with the 32-bit runtime when running on a 64-bit operating system. Use the value of 0 to execute the package with the 64-bit runtime when running on a 64-bit operating system. This parameter is optional. The Use32bitruntime is bit. - [ @execution_id = ] execution_id
-
Returns the unique identifier for an instance of execution. The execution_id is bigint.
Remarks
An execution is used to specify the parameter values that are a package uses during a single instance of package execution.
If an environment reference is specified with the reference_id parameter, the stored procedure populates the project and package parameters with literal values or referenced values from the corresponding environment variables. If environment reference is specified, default parameter values are used during package execution. To determine exactly which values are used for a particular instance of execution, use the execution_id output parameter value from this stored procedure and query the execution_parameter_values view.
Only packages that are marked as entry point packages can be specified in an execution. If a package that is not an entry point is specified, the execution fails.
2,使用 catalog.start_execution 存储过程启动一个Execution Instance
start_execution [ @execution_id = ] execution_id
Arguments
- [ @execution_id = ] execution_id
-
The unique identifier for the instance of execution. The execution_id is bigint.
Remarks
An execution is used to specify the parameter values that will be used by a package during a single instance of package execution. After an instance of execution has been created, before it has been started, the corresponding project might be redeployed. In this case, the instance of execution will reference a project that is outdated. This will cause the stored procedure to fail.
3,使用catalog.set_object_parameter_value 存储过程来修改Parameter 的Server value。
Sets the value of a parameter in the Integration Services catalog. Associates the value to an environment variable or assigns a literal value that will be used by default if no other values are assigned.
set_object_parameter_value [ @object_type = ] object_type , [ @folder_name = ] folder_name , [ @project_name = ] project_name , [ @parameter_name = ] parameter _name , [ @parameter_value = ] parameter_value [ , [ @object_name = ] object_name ] [ , [ @value_type = ] value_type ]
Arguments
- [ @object_type = ] object_type
-
The type of parameter. Use the value 20 to indicate a project parameter or the value 30 to indicate a package parameter. The object_type is smallInt. - [ @folder_name = ] folder_name
-
The name of the folder that contains the parameter. The folder_name is nvarchar(128). - [ @project_name = ] project_name
-
The name of the project that contains the parameter. The project_name is nvarchar(128). - [ @parameter_name = ] parameter_name
-
The name of the parameter. The parameter_name is nvarchar(128). - [ @parameter_value = ] parameter_value
-
The value of the parameter. The parameter_value is sql_variant. - [ @object_name = ] object_name
-
The name of the package. This argument required when the parameter is a package parameter. The object_name is nvarchar(260). - [ @value_type = ] value_type
-
The type of parameter value. Use the character V to indicate that parameter_value is a literal value that will be used by default of no other values are assigned prior to execution. Use the character R to indicate that parameter_value is a referenced value and has been set to the name of an environment variable. This argument is optional, the character V is used by default. The value_type is char(1).
Remarks
- If no value_type is specified, a literal value for parameter_value is used by default. When a literal value is used, the value_set in the object_parameters view is set to 1. A NULL parameter value is not allowed.
- If value_type contains the character R, which denotes a referenced value, parameter_value refers to the name of an environment variable.
- The value 20 may be used for object_type to denote a project parameter. In this case, a value for object_name is not necessary, and any value specified for object_name is ignored. This value is used when the user wants to set a project parameter.
- The value 30 may be used for object_type to denote a package parameter. In this case, a value for object_name is used to denote the corresponding package. If object_name is not specified, the stored procedure returns an error and terminates.
4,使用 catalog.set_execution_parameter_value 存储过程来修改Parameter的 Execution Value
Sets the value of a parameter for an instance of execution in the Integration Services catalog.
set_execution_parameter_value [ @execution_id = execution_id , [ @object_type = ] object_type , [ @parameter_name = ] parameter_name , [ @parameter_value = ] parameter_value
Arguments
[ @execution_id = ] execution_id
-
The unique identifier for the instance of execution. The execution_id is bigint. - [ @object_type = ] object_type
-
The type of parameter.For the following parameters, set object_type to 50
- LOGGING_LEVEL
- CUSTOMIZED_LOGGING_LEVEL
- DUMP_ON_ERROR
- DUMP_ON_EVENT
- DUMP_EVENT_CODE
- CALLER_INFO
- SYNCHRONIZED
Use the value 20 to indicate a project parameter or the value 30 to indicate a package parameter.
The object_type is smallint.
- [ @parameter_name = ] parameter_name
-
The name of the parameter. The parameter_name is nvarchar(128). - [ @parameter_value = ] parameter_value
-
The value of the parameter. The parameter_value is sql_variant.
Remarks
To find out the parameter values that were used for a given execution, query the catalog.execution_parameter_values view.
5,MSDN的Example
Declare @execution_id bigint EXEC [SSISDB].[catalog].[create_execution] @package_name=N‘Child1.dtsx‘, @execution_id=@execution_id OUTPUT, @folder_name=N‘TestDeply4‘, @project_name=N‘Integration Services Project1‘, @use32bitruntime=False, @reference_id=Null Select @execution_id DECLARE @var0 sql_variant = N‘Child1.dtsx‘ EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id, @object_type=20, @parameter_name=N‘Parameter1‘, @parameter_value=@var0 DECLARE @var1 sql_variant = N‘Child2.dtsx‘ EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id, @object_type=20, @parameter_name=N‘Parameter2‘, @parameter_value=@var1 DECLARE @var2 smallint = 1 EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id, @object_type=50, @parameter_name=N‘LOGGING_LEVEL‘, @parameter_value=@var2 EXEC [SSISDB].[catalog].[start_execution] @execution_id GO
6,示例
6.1, 查看部署到Sql server的packages,folers,projects
--查看部署到Sql Server的packages,folers,projects select * from [catalog].[packages] select * from [catalog].[folders] select * from [catalog].[projects]
6.2,创建一个Execution Instance
--Create Execution Instance Declare @execution_id bigint EXEC [catalog].[create_execution] @package_name=N‘Package1.dtsx‘, @execution_id=@execution_id OUTPUT, @folder_name=N‘TestISProject‘, @project_name=N‘TestISProject‘, @use32bitruntime=False, @reference_id=Null --Select @execution_id
参数@package_name,@folder_name,@project_name 的值是从6.1查询获取的。
6.3 查看Package使用的parameter
--查看Package使用的Parameter select * from catalog.object_parameters op
6.4 设置Parameter的Execution Value
--set parameter new execution value DECLARE @ParameterA sql_variant set @ParameterA= 11 EXEC [catalog].[set_execution_parameter_value] @execution_id, @object_type=30, @parameter_name=N‘ParameterA‘, @parameter_value=@ParameterA
6.5 执行package
启动Package的一个Execution Instance
EXEC [catalog].[start_execution] @execution_id
7,catalog.start_execution的异步性
存储过程 catalog.start_execution Starts an instance of execution in the Integration Services catalog,仅仅是开始一个Execution Instance,但是并不等待Execution Instance 执行完成,存储过程 catalog.start_execution 就会返回。
Scenario1:通过轮询方式查看Execution Instance的status
Calling [catalog].[start_execution] does not wait for the SSIS package to finish executing
It should be noted that after calling [catalog].[start_execution], your script does not wait for the SSIS package to finish executing before continuing. If any subsequent script requires the SSIS package to be complete, you must pause execution with a WHILE loop, using WAITFOR, and checking the status of the execution, before allowing your script to continue.
Example:
DECLARE @ssis_execution_status BIGINT = 1; DECLARE @ssis_execution_id bigint set @ssis_execution_id=xxx EXECUTE [catalog].[start_execution] @ssis_execution_id; WHILE(@ssis_execution_status NOT IN (3,4,6,7,9)) BEGIN --The possible values are created (1), running (2), canceled (3), failed (4), pending (5), --ended unexpectedly (6), succeeded (7), stopping (8), and completed (9) --Pause for 1 second. WAITFOR DELAY ‘00:00:01‘; --Refresh the status. SELECT @ssis_execution_status=[executions].[status] FROM [catalog].[executions] WHERE [executions].[execution_id] = @ssis_execution_id END
Scenarion2:通过设置特殊 Parameter 的Execution Value。
package execution is async by default, but can be sync by setting a parameter
exec catalog.set_execution_parameter_value @execution_id, @object_type= 50, @parameter_name = N‘Synchronized‘, @parameter_value = 1;
Synchronized 是一个特殊的Parameter,设置Execution Value为1,那么存储过程 catalog.start_execution 将等到Package执行完成之后返回。
In order to call the package synchronously, and therefore keep the caller waiting until the package finishes, you have to set a parameter value to true. This parameter, SYNCHRONIZED
To call it, simply call catalog.set_execution_parameter_values before calling catalog.start_execution and set the SYNCHRONIZED parameter to true (1). Like so:
EXEC [SSISDB].[catalog].[set_execution_parameter_value] @execution_id, -- execution_id from catalog.create_execution @object_type=50, @parameter_name=N‘SYNCHRONIZED‘, @parameter_value= 1; -- turn on synchronized execution
推荐文档:
http://www.ssistalk.com/2012/07/24/quick-tip-run-ssis-2012-packages-synchronously-and-other-execution-options/
参考文档:
https://msdn.microsoft.com/en-us/library/ff878034.aspx
https://msdn.microsoft.com/en-us/library/ff878160.aspx
https://msdn.microsoft.com/en-us/library/ff877990.aspx
https://msdn.microsoft.com/en-us/library/ff878089.aspx