Forum Discussion

vlad230's avatar
vlad230
Contributor
13 years ago

[TC8 + JScript + ADO] SQL statement not returning the proper values

Hi all,



I'm trying to do some SQL selects on a Oracle Database and I'm not getting the proper results.



The odd thing is that if I do a query within Oracle SQL Developer I get 2 results and when I try to do this with TC8 I get no results although the query string is the same.



I've tried to do this using 2 methods using TC8 and JScript:





function test1(){

  var query = "SELECT promoname FROM database.promotion WHERE promoenddate = '08-09-01'";

  var field = "promoname";

 

  var Qry;

  // Create a query

  Qry = ADO.CreateADOQuery();

  // Specify the connection string

  Qry.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=localhost:1521;User Id=username;Password=pass;OLEDB.NET=True;";

  // Specify the SQL expression

  Qry.SQL = query;

  // Execute the query

  Qry.Open();

  // Process results and insert data into the test log

  Qry.First();

  while (! Qry.EOF){

    Log.Message(Qry.FieldByName(field).Value);

    Qry.Next();

  }

  // Closes the query

  Qry.Close();

}





This doesn't log any messages.





function test2(){

  var query = "SELECT promoname FROM database.promotion WHERE promoenddate = '08-09-01'";

  var field = "promoname";

 

  var RecSet, Cmd, query;

  //create output array

  var output = new Array();

  // Create a new object

  Cmd = ADO.CreateADOCommand();

  // Specify the connection string

  Cmd.ConnectionString = "Provider=OraOLEDB.Oracle;Data Source=localhost:1521;User Id=username;Password=pass;OLEDB.NET=True;";

  // Specify the command text (the SQL expression)

  Cmd.CommandText = query;

  // Specify the command type

  Cmd.CommandType = cmdText;

  //Execute command

  RecSet = Cmd.Execute();

  //Process result

  RecSet.MoveFirst(); 

  while (! RecSet.EOF){

    Log.Message(RecSet.Fields(field).Value);

    RecSet.MoveNext();

  }

}



This fails at the RecSet.MoveFirst(); line (because the RecSet is empty I guess).



Also, I would like to mention that I did get result for other select queries, but it seems that querying something related to dates doesn't return anything.



Can anyone help?



Thanks.

4 Replies

  • I managed to solve this issue after all.



    The problem was with the query indeed, although it's still strange because it worked in SQL developer.



    The year needs to be defined with 4 digits like so:





    var query = "SELECT promoname FROM database.promotion WHERE promoenddate = '2008-09-01'";





    Thanks.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    The problem is in your SQL query I think.  Because the single quote is a special character for JScript, you need to specify that you're actually wanting to include the quote.  So, change your query string to



      var query = "SELECT promoname FROM database.promotion WHERE promoenddate = \'08-09-01\'";




    I think this will resolve both methods as it seems that in the first case you have nothing logged because you're already at EOF and the second is because of the empty record set due to a malformed SQL query string.



    Try this and see if this fixes it.
  • Hi Martin,



    I've tried your suggestion but it still doesn't work. I'm getting the same result as before.



    Do you have any other ideas on this?



    Thanks.
  • tristaanogre's avatar
    tristaanogre
    Esteemed Contributor
    Actually, that makes sense.  Probably SQL developer knows a bit more how to indicate which format the string is in to know what is the year part... you're using ADO which is much more low level.  Technically, if the field is an actual DateTime field, it's not actually stored as a string but as a numeric value so it needs to convert that string to a numeric.



    Anyways, glad you found a solution.