以下是visual c#
中SSIS脚本任务的内容public void Main()
{
// TODO: Add your code here
int Var_Flag = (int)Dts.Variables["Var_Flag"].Value; // 1 for manual run, 0 for incremental since last run
DateTime Start_Date = (DateTime)Dts.Variables["Var_StartDate"].Value; // Start DateTime of the previous run. Start at this date if run is set to manual
DateTime End_Date = (DateTime)Dts.Variables["Var_EndDate"].Value; // End DateTime of the previous run. End at this date if run is set to manual
String SQL = "";
// If incremental run, our current run needs to start at the end date from the previous run and include everything up to today's date
// Format the SQL string
SQL = "SELECT * " + " from rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_F"+
// "WHERE " +
//"(convert(datetime2,UPD_GMT_TS) >=CONVERT(DATETIME,'" + Start_Date + "',131) AND convert(datetime2,UPD_GMT_TS) < CONVERT(DATETIME,'" + End_Date + "',131)) ";
//"(convert(UPD_GMT_TS >=cast('" + Start_Date + "',datetime) AND UPD_GMT_TS < cast('" + End_Date + "',datetime) ";
//"UPD_GMT_TS BETWEEN '" + Start_Date + "' and '" + End_Date + "'";
"(UPD_GMT_TS >=CONVERT(datetime,'" + Start_Date.ToString() + "') AND UPD_GMT_TS < CONVERT(datetime,'" + End_Date.ToString() + "')) ";
// Return the SQL string to the global variable
Dts.Variables["Intervention_Parts_Qry"].Value = SQL;
Dts.TaskResult = (int)ScriptResults.Success;
}
此代码的作用是什么。
我收到的错误是:
[OLEDB_SRC- COREIB CONTRACT DATA [1]]错误:SSIS错误代码DTS_E_OLEDBERROR。发生OLE DB错误。错误代码:0x80040E14。 OLE DB记录可用。来源:“Microsoft SQL Server Native Client 10.0”Hresult:0x80040E14描述:“语句无法准备。”。 OLE DB记录可用。来源:“Microsoft SQL Server Native Client 10.0”Hresult:0x80040E14描述:“'&gt;'附近的语法不正确。”。
需要帮助解决此问题:)
答案 0 :(得分:4)
这些情况下的诀窍是双重的。第一种是不对SQL语句使用字符串构建。这就是Parameters的用途。作为奖励,它有助于防止SQL注入攻击。
当你遇到奇怪的事情时要做的第二件事就是显示命令。假设您忠实地代表了SQL
变量将要评估的代码
SELECT * from rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_FWHERE (UPD_GMT_TS...
您需要在表/视图和WHERE子句之间留一个空格。
因此
SQL = "SELECT * "
+ " FROM rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_F AS F
+ "WHERE " ...
使用SqlCommand的更好方法是
SqlCommand command = new SqlCommand();
command.Connection = connection;
command.CommandText = "SELECT F.* FROM rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_F AS F WHERE F.UPD_GMT_TS >= @StartDate AND F.UPD_GMT_TS < @EndDate ";
command.CommandType = CommandType.Text;
SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "StartDate";
parameter.SqlDbType = SqlDbType.DateTime;
parameter.Direction = ParameterDirection.Input;
parameter.Value = Start_Date;
command.Parameters.Add(parameter);
parameter = new SqlParameter();
parameter.ParameterName = "EndDate";
parameter.SqlDbType = SqlDbType.DateTime;
parameter.Direction = ParameterDirection.Input;
parameter.Value = End_Date;
command.Parameters.Add(parameter);
connection.Open();
SqlDataReader reader = command.ExecuteReader();
答案 1 :(得分:1)
将此转化为答案。
取消注释“WHERE”。这就是你的错误所在。
这段代码:
SQL = "SELECT * " + " from rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_F"+
// "WHERE " +
//"(convert(datetime2,UPD_GMT_TS) >=CONVERT(DATETIME,'" + Start_Date + "',131) AND convert(datetime2,UPD_GMT_TS) < CONVERT(DATETIME,'" + End_Date + "',131)) ";
//"(convert(UPD_GMT_TS >=cast('" + Start_Date + "',datetime) AND UPD_GMT_TS < cast('" + End_Date + "',datetime) ";
//"UPD_GMT_TS BETWEEN '" + Start_Date + "' and '" + End_Date + "'";
"(UPD_GMT_TS >=CONVERT(datetime,'" + Start_Date.ToString() + "') AND UPD_GMT_TS < CONVERT(datetime,'" + End_Date.ToString() + "')) ";
应如下所示:
SQL = "SELECT * " + " from rz_ibsv.dbo.INTGR_INSTLD_BASE_SRVC_CNTRCT_F"+
"WHERE " +
//"(convert(datetime2,UPD_GMT_TS) >=CONVERT(DATETIME,'" + Start_Date + "',131) AND convert(datetime2,UPD_GMT_TS) < CONVERT(DATETIME,'" + End_Date + "',131)) ";
//"(convert(UPD_GMT_TS >=cast('" + Start_Date + "',datetime) AND UPD_GMT_TS < cast('" + End_Date + "',datetime) ";
//"UPD_GMT_TS BETWEEN '" + Start_Date + "' and '" + End_Date + "'";
"(UPD_GMT_TS >=CONVERT(datetime,'" + Start_Date.ToString() + "') AND UPD_GMT_TS < CONVERT(datetime,'" + End_Date.ToString() + "')) ";