vendredi 14 août 2015

parameterized insertion of data half selected from other column gives invalid pseudocolum

I have the following query

     "INSERT INTO t1 select $v1,c2 FROM t2 WHERE c3= $v2";

which is executed as

    SqlCommand cmd= new SqlCommand(query, conn);
    cmd.Parameters.AddWithValue("$v2", data);
    foreach (string value in list)
    {
        cmd.Parameters.AddWithValue("$v1", value);
        cmd.ExecuteNonQuery();
    }

however this results in an error:

An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Invalid pseudocolumn "$v1".

This is based on the following question: SQL Insert into ... values ( SELECT ... FROM ... ) I suspect that it's not understanding where the were clause goes or that $v1 is not the name of a column but an actual value, but does anybody know how to fix this (t1 only has 2 columns both ints c2 is an int and c3 is also an int).



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire