简体   繁体   中英

Missing Comma, oracle ORA-00917

I have this code for inserting values into a oracle database, and I get "Missing comma" Exception, and I can't see from where it comes.

 "insert into comandadvd values('" + txt_idComanda.Text + "','" +
                    txtFormat.Text + "','" + "', to_date('" + txtData.Text + "','DDMMYYY')'" + txtIdTipPlata.Text + "','" + txtPret.Text + "')";

Alse here it is the Oracle code for it:

INSERT INTO ComandaDVD (Id_Comanda,Id_Format,Data_Comanda,Id_TipPlata,Pret)
VALUES ('1','1','12-11-2011','1','200');

Thanks in advance!

  • 尝试一次

"insert into comandadvd values('" + txt_idComanda.Text + "','" + txtFormat.Text + "','" + "' to_date('" + txtData.Text + "','DDMMYYY')'" + "','" + txtIdTipPlata.Text + "','" + txtPret.Text + "')";

  1. You have an extra comma before date. The date part to_date should not be inside quotes.
  2. The date format is wrong. You mentioned DD-MM-YYYY in example.

Try this as I havn't tested it and let me know if you get error.

"insert into comandadvd values('" + txt_idComanda.Text + "','" + txtFormat.Text + "','" + "to_date('" 
+ txtData.Text + "','DD-MM-YYY')" + txtIdTipPlata.Text + "','" + txtPret.Text + "')";

The immediate cause of the error is in the

"','" + "', to_date('"

fragmanent. The best solution is using parametrized query:

  @"insert into comandadvd (
      Id_Comanda,
      Id_Format,
      Data_Comanda,
      Id_TipPlata,
      Pret)
    values ( 
      :prm_Id_Comanda,
      :prm_Id_Format,
      :prm_Data_Comanda,
      :prm_Id_TipPlata,
      :prm_Pret)";

In case that for whatever reason you can't use parametrized query, use formatted one:

   String.Format(
     @"insert into comandadvd (
         Id_Comanda,
         Id_Format,
         Data_Comanda,
         Id_TipPlata,
         Pret)
       values ( 
         {0},
         {1},
         to_date('{2}', 'DDMMYYY'),
         {3},
         {4})",
      txt_idComanda.Text,
      txtFormat.Text, 
      txtData.Text,
      txtIdTipPlata.Text,
      txtPret.Text);

The guiding principle is Make your SQL readable

You added extra comma before to_date( in your query. The corrected code as below:

"INSERT INTO ComandaDVD (Id_Comanda,Id_Format,Data_Comanda,Id_TipPlata,Pret) VALUES (" +
    "'" + txt_idComanda.Text + "', " +
    "'" + txtFormat.Text + "', " +
    "to_date('" + txtData.Text + "', 'DDMMYYY'), " +
    "'" + txtIdTipPlata.Text + "', " + 
    "'" + txtPret.Text + "')";

There appear to be multiple problems:

  1. The biggest problem is that you did not run the code to see what it really does - witness the fact that the table name used in the code and the table name shown in the SELECT have different capitalization, and there's a field list in the posted SELECT which the code does not generate; therefore, the SELECT text you posted was obviously not generated by this code. If you had actually run the code and generated the INSERT statement you'd have found that it doesn't work as you think it does, and you could have fixed it yourself.
  2. This code will put multiple commas before to_date .
  3. The code will not generate a comma before txtIdTipPlata.Text .
  4. INSERT statements should ALWAYS include a list of the names of the fields to be inserted.

Best of luck.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM