Oracle Database, SQL Update statement will not work (OLEDB)

别等时光非礼了梦想. 提交于 2021-02-17 02:46:10

问题


I set a numeric primary key, and an alphanumeric field that stores truck FINS, which is just a random mix of numbers and letters. I do not generate the fins, these fins will always be the same as it's the truck fleet identification number.

Here is the code view:

 storeTruckSplit = truckSplit[1];//Stores truck FIN


        //Update truck value
        try
        {
            conn.Open();
            OleDbCommand command;
            command = new OleDbCommand(
                "Update Trucks" +
                " SET Trucks.TruckInUse = ? WHERE TFIN = " + storeTruckSplit.ToString(), conn);
            command.Parameters.Add(new OleDbParameter("@use", "T"));
            command.ExecuteNonQuery();//Commit   
            conn.Close();
        }
        catch (OleDbException exception)
        {
            MessageBox.Show(exception.Message, "OleDb Exception");
        }

Here is the table view:

CREATE TABLE Trucks
(

TruckID number(9)  CONSTRAINT TRUCK_PK PRIMARY KEY,
TFIN char(9) NOT NULL,
TruckCategory varchar(80) NOT NULL,
TruckCodeName varchar(50) NOT NULL,
MaxWeight number(10) NOT NULL,
TruckSize number(10) NOT NULL,
TruckInUse varchar(1) NULL

);

There is also a sequence and trigger for before inserting on this table for the TRUCKID.

I am getting ORA-00904 and note this is showing C6977734D which is a truck fin that is used on the where clause of the update.

Exact Message:

"One or more errors occurred during processing of command. ORA-00904: "C6977734D": invalid indentifer.


回答1:


Make the TFIN value a parameter as well:

    command = new OleDbCommand(
            "Update Trucks" +
            " SET Trucks.TruckInUse = ? WHERE TFIN = ?", conn);
        command.Parameters.Add(new OleDbParameter("@use", "T"));
        command.Parameters.Add(new OleDbParameter("@tfin", storeTruckSplit));
        command.ExecuteNonQuery();//Commit   

As it stands you are not putting quotes around the value you're filtering on, so the query is treating it as an identifier (field, variable, etc) rather than a constant value. Since you're already using a parameter for the "in use" value (which is not necessary since you're providing a constant value) the best fix is to use a parameter for the filter as well.



来源:https://stackoverflow.com/questions/28902859/oracle-database-sql-update-statement-will-not-work-oledb

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!