简体   繁体   English

数据流中的SSIS变量任务使用不正确

[英]SSIS Variable in Data Flow Task used incorrectly

In the SSIS Package I have several Data flow Tasks where I want to use one input global variable named KOERSEL to go back in time and call data set specifically in time. 在SSIS包中,我有几个数据流任务,我想使用一个名为KOERSEL的输入全局变量来及时返回并专门调用数据集。

When I try to run it I am getting the error: 当我尝试运行它时,我收到错误:

Syntax error, permission violation, or other nonspecific error. 语法错误,权限违规或其他非特定错误。

when I change ? 当我改变? to 1 in the SQL command text, the code is running fine. 1在SQL命令文本,代码运行良好。 So what am I missing? 那我错过了什么?

DECLARE @dt DATETIMEOFFSET = SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETDATE()), '-04:00')
DECLARE @interval INT = ?
SET @interval = -1 * @interval
DECLARE @DATE_OPG DATE
SELECT @DATE_OPG = A.DWH_PR_DATO
FROM TABLE AS A
WHERE YEAR(A.DWH_PR_DATO)=YEAR(DATEADD(MONTH,@interval,@dt)) AND
      MONTH(A.DWH_PR_DATO)=MONTH(DATEADD(MONTH,@interval,@dt)) 
ORDER BY A.DWH_PR_DATO DESC

SELECT DISTINCT COLUMN 1,
                COLUMN 1,
                COLUMN 1,
FROM TABLE 1
WHERE DATE_OPG=@DATE_OPG
UNION ALL
SELECT DISTINCT COLUMN 2,
                COLUMN 2,
                COLUMN 2,
FROM TABLE 2
WHERE DATE_OPG=@DATE_OPG
...

Screenshot 截图

程序代码

I don't think that the following error is the real issue. 我不认为以下错误是真正的问题。

Incorrect syntax near ')'. ')'附近的语法不正确。

The query parser was not able to parse the query because you have added a minus sign before the question mark ? 查询解析器无法解析查询,因为您在问号前添加了减号? . In this answer i will try to clarify the main cause of the error you are seeing. 在这个答案中,我将尝试澄清您所看到的错误的主要原因。

Parameter data type vs Variable data type 参数数据类型与可变数据类型

Based on the official OLEDB Source - Documentation : 基于官方OLEDB来源 - 文档

The parameters are mapped to variables that provide the parameter values at run time. 参数映射到在运行时提供参数值的变量。 The variables are typically user-defined variables, although you can also use the system variables that Integration Services provides. 变量通常是用户定义的变量,但您也可以使用Integration Services提供的系统变量。 If you use user-defined variables, make sure that you set the data type to a type that is compatible with the data type of the column that the mapped parameter references. 如果使用用户定义的变量,请确保将数据类型设置为与映射的参数引用的列的数据类型兼容的类型。

This implies that the parameter datatype is not related to the variable data type. 这意味着参数数据类型与变量数据类型无关。

So when you are using -? 所以当你使用-? inside the SQL Command the query parser are not able to identify the parameter metadata even if it is mapped to an integer variable. 在SQL命令内部,即使查询解析器映射到整数变量,它也无法识别参数元数据。

You can check my answer on the link below, it contains much details with experiments: 您可以在下面的链接中查看我的答案,其中包含许多有关实验的详细信息:


Solving the problem 解决问题

(1) Force parameter data type (1)强制参数数据类型

Try using CAST() function to force the parameter data type and assign it to a variable in the same way you have declared @dt : 尝试使用CAST()函数强制参数数据类型,并以与声明@dt相同的方式将其分配给变量:

DECLARE @interval INT = CAST(? as INT)
--If you want to get a negative number else ignore the row below
SET @interval = -1 * @interval
DECLARE @dt DATETIMEOFFSET = SWITCHOFFSET(CONVERT(DATETIMEOFFSET,GETDATE()),'-04:00');
DECLARE @DATE_OPG DATE;
SELECT @DATE_OPG = DWH_PR_DATEO
FROM TableName
WHERE YEAR(DWH_PR_DATO) = YEAR(DATEADD(MONTH,@interval ,@dt)) AND
MONTH(DWH_PR_DATO) = MONTH(DATEADD(MONTH,@interval ,@dt))
ORDER BY DWH_PR_DATO DESC

(2) Using Expressions (2)使用表达式

You can use Expressions while building the SQL Command: 您可以在构建SQL命令时使用表达式:

  1. Add a variable of type string (Example: @[User::strQuery] ) 添加string类型的变量(例如: @[User::strQuery]
  2. Define an Expression within this variable: 在此变量中定义表达式:

     "DECLARE @dt DATETIMEOFFSET = SWITCHOFFSET(CONVERT(DATETIMEOFFSET,GETDATE()),'-04:00'); DECLARE @DATE_OPG DATE; SELECT @DATE_OPG = DWH_PR_DATEO FROM TableName WHERE YEAR(DWH_PR_DATO) = YEAR(DATEADD(MONTH,-" + @[User::KOERSEL] + ",@dt)) AND MONTH(DWH_PR_DATO) = MONTH(DATEADD(MONTH,-" + @[User::KOERSEL] + ",@dt)) ORDER BY DWH_PR_DATO DESC" 
  3. In the OLEDB Source choose SQL Command from variable and Select @[User::strQuery] 在OLEDB Source中从变量中选择SQL Command并选择@[User::strQuery]


Experiments 实验

I tried a similar query using the AdventureWorks database: 我使用AdventureWorks数据库尝试了类似的查询:

DECLARE @dt DATETIMEOFFSET = SWITCHOFFSET(CONVERT(DATETIMEOFFSET, GETDATE()), '-04:00')
DECLARE @interval INT = CAST(? as INT)
SET @interval = -1 * @interval
DECLARE @DATE_OPG DATE
SELECT @DATE_OPG = A.[ModifiedDate]
FROM [AdventureWorks2016CTP3].[HumanResources].[Employee] AS A
WHERE YEAR(A.[ModifiedDate])=YEAR(DATEADD(MONTH,@interval,@dt)) AND
      MONTH(A.[ModifiedDate])=MONTH(DATEADD(MONTH,@interval,@dt)) 
ORDER BY A.[ModifiedDate] DESC

SELECT * FROM [AdventureWorks2016CTP3].[HumanResources].[Employee]
WHERE [ModifiedDate] = @DATE_OPG 

And the query is parsed successfully 并且查询已成功解析

在此输入图像描述

Instead of -? 而不是-? use the following logic: 使用以下逻辑:

-1 * (CAST(? as int))

if you just want to pass the variable as parameter without a negative sign then just use: 如果您只想将变量作为参数传递而没有负号,那么只需使用:

(CAST(? as int))

You cannot assign a negative sign to the parameter because it will cause some conflict since the query parser will not be able to define the parameter data type. 您不能为参数指定负号,因为它会导致一些冲突,因为查询解析器将无法定义参数数据类型。

If it still throwing and exception, check the following link it contains a workaround: 如果仍然抛出异常,请检查以下链接是否包含变通方法:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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