查询有一些问题

时间:2015-09-15 06:03:12

标签: asp.net oracle

当我在代码中编写此查询按钮时,单击以在mkey表中插入xxacl_pn_new_cha_part_h 它给了我错误

  

" ORA-00904:" A":无效的标识符"

这是我的代码: -

try
    {
        conn.Open();
        OracleDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part where mkey= " + sdr[0].ToString();  // this query gives error
            cmd.ExecuteNonQuery();
            strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=convert(datetime,'" + System.DateTime.Now.ToString() + "',103) WHERE mkey = " + sdr[0].ToString();
            cmd.CommandText = strQuery;
            cmd.ExecuteNonQuery();
        }

        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);

    }

我正在使用Oracle

更新

 protected void btnUpdate_Click(object sender, EventArgs e)
{
    OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
    string strQuery = "SELECT distinct ab.mkey, ab.broker_id,CASE WHEN SYSDATE - la.creation_date <= 180 THEN 'A' WHEN SYSDATE - cef_dt <= 30 THEN " +
                       "'B' ELSE 'C'END rating FROM xxacl_pn_new_cha_part ab,xxacl_pn_lease_det ld,xxacl_pn_leases_all la, " +
                       "xxcus.xxacl_pn_customer_enquiry_v ce WHERE ab.broker_id = ld.broker_id(+) AND ld.booking_no = la.booking_no(+) " +
                       " AND ab.broker_id = ce.broker_id(+)";

    OracleCommand cmd = new OracleCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = conn;
    try
    {
        conn.Open();
        OracleDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
            cmd.ExecuteNonQuery();
            strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=convert(datetime,'" + DateTime.Now.ToString() + "',103) WHERE mkey = " + sdr[0].ToString();
            cmd.CommandText = strQuery;
            cmd.ExecuteNonQuery();
        }

        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
        conn.Dispose();
    }
}

2 个答案:

答案 0 :(得分:1)

您在这里缺少别名,试试这个

cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString(); 

更新:

 protected void btnUpdate_Click(object sender, EventArgs e)
{
    OracleConnection conn = new OracleConnection(ConfigurationManager.ConnectionStrings["OracleConn"].ConnectionString);
    string strQuery = "SELECT distinct ab.mkey, ab.broker_id,CASE WHEN SYSDATE - la.creation_date <= 180 THEN 'A' WHEN SYSDATE - cef_dt <= 30 THEN " +
                       "'B' ELSE 'C'END rating FROM xxacl_pn_new_cha_part ab,xxacl_pn_lease_det ld,xxacl_pn_leases_all la, " +
                       "xxcus.xxacl_pn_customer_enquiry_v ce WHERE ab.broker_id = ld.broker_id(+) AND ld.booking_no = la.booking_no(+) " +
                       " AND ab.broker_id = ce.broker_id(+)";

    OracleCommand cmd = new OracleCommand();
    cmd.CommandType = CommandType.Text;
    cmd.CommandText = strQuery;
    cmd.Connection = conn;
    try
    {
        conn.Open();
        OracleDataReader sdr = cmd.ExecuteReader();
        while (sdr.Read())
        {
            cmd.CommandText = "insert into xxacl_pn_new_cha_part_h select 0, sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();
            cmd.ExecuteNonQuery();
            strQuery = "UPDATE xxacl_pn_new_cha_part set Rating='" + sdr[2].ToString() + "', RATING_UPDATE_DATE=to_date('" + DateTime.Now.ToString() + "','dd-mm-yyyy hh:mi:ss am') WHERE mkey = " + sdr[0].ToString();
            cmd.CommandText = strQuery;
            cmd.ExecuteNonQuery();
        }

        ScriptManager.RegisterStartupScript(this, GetType(), "alertMessage", "alert('Broker rating updated succesfully');", true);

    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        conn.Close();
        conn.Dispose();
    }
}

答案 1 :(得分:1)

您错过了表格的别名

 "insert into xxacl_pn_new_cha_part_h select sysdate, a.* from xxacl_pn_new_cha_part a where a.mkey= " + sdr[0].ToString();