必须定义Sql查询参数名称

时间:2014-02-15 09:11:55

标签: c# mysql sql

您好我有这个SQL查询:

SQL = "SELECT SUM( " + "CASE WHEN rn =1" + "THEN v.FirstAmount" 
      + "WHEN rn =2" + "THEN v.SecondAmount " + "ELSE v.ThirdAmount " + "END ) " 
      + "FROM (" + "SELECT cv. * , @rn := IF( @vi = `Violation ID` , @rn +1, 1 ) AS rn,          
       @vi := `Violation ID` " + "FROM class_violation cv" + "CROSS JOIN (" 
      + "SELECT @rn :=0, @vi := ''" + ")CONST" + "ORDER BY `Violation ID`" + ")cv" 
      + "JOIN violation v ON cv.`Violation ID` = v.`Violation ID` " 
      + "JOIN class_record tr ON cv.`Class No.` = tr.`Class No.` " 
      + "WHERE tr.`Class ID` = '" + where + "'";

我收到了这个错误:

MySql.Data.MySqlClient.MySqlException: Fatal Error encountered during
command execution ---> MySql.Data.MySqlClient.MySqlException: Parameter
'@rn' must be defined
 at
MySql.Data.MySqlClient.Statement.SerializeParameter(MySqlParameterCollection
parameters, MySqlPacket packet, String parmName, Int32 parameterIndex)
    at MySql.Data.MySqlClient.Statement.InternalBindParameters(String sql,
    MySqlParameterCollection parameters,MySqlPacket packet)

如何更正SQL查询并定义参数?

1 个答案:

答案 0 :(得分:4)

我想我知道如何回答我的问题。在我的连接字符串中,我刚刚添加了Allow User Variables = True,它现在可以正常工作了!