0

Pretty basic sql Command. Just want to get the count from different tables I am looping through. However, if I alter the sqlCommand and add a ';' at the end I get exception, SQL command not properly ended.

     sqlCommand = String.Format("SELECT COUNT(1) FROM SO.{0} where DR_ADDRESS_ID = {1};", table, drAddr);

I am curious why this semi-colon makes this exception thrown since commands are suppose to end with a ';'

 sqlCommand = String.Format("SELECT COUNT(1) FROM SO.{0} where DR_ADDRESS_ID = {1}", table, drAddr);
     try
     {      
        using(OracleCommand ocCommand = new OracleCommand(sqlCommand,CommandType.Text))
        {
           ocCommand.Connection = dbConnection;
           recordCounter = Convert.ToInt64(ocCommand.ExecuteScalar());
        }               
     }
     catch (Exception er)
     {
        MessageBox.Show(String.Format("Error in RecordCount for table {0}: Reference {1} for log. err = {2}",table, logFilePath,er.ToString()));
        recordCounter = -1;

        using (StreamWriter writer = new StreamWriter(logFilePath, true))
        {
           writer.WriteLine(String.Format("Table: {0}. Command {1}", table,sqlCommand.ToString()));
        }
     }
Zohar Peled
  • 79,642
  • 10
  • 69
  • 121
Korey
  • 3
  • 2
  • Which database product are you using? Probably not al of MySQL, Oracle and SQL Server. Please edit the tags to keep only the pertinent one(s). –  Mar 16 '17 at 18:27
  • 1
    Based on the code and error message I've removed the irrelevant tags. – Zohar Peled Mar 16 '17 at 18:29
  • @mathguy my apologize, Oracle Database is the db product. – Korey Mar 16 '17 at 18:30
  • No apology needed - just remember next time you post to inspect the tags the web site adds for you by default - they are rarely 100% accurate. Cheers! –  Mar 16 '17 at 18:32
  • 1
    @Korey - take a look at this question: http://stackoverflow.com/questions/10728377/wheres-my-invalid-character-ora-00911 - the delimiter is a property of the connection/driver, not the database itself. While SQL*Plus treats the semicolon as the end of a query, Oracle itself doesn't require it. – Phylyp Mar 16 '17 at 18:32

1 Answers1

0

Semi colons are usually just used as a command terminator for interactive tools like sqlplus.

BobC
  • 4,208
  • 1
  • 12
  • 15