Announcement

Collapse
No announcement yet.

Javascript SELECT query to return COUNT()

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Javascript SELECT query to return COUNT()

    Hi

    I'm executing a javascript query to count the number of records in a SQL SELECT query. The simplified version looks like this:

    var sqlQuery = "SELECT COUNT(*) TABLE1";
    var result= dbConn.executeCachedQuery(sqlQuery);

    My question is should I use executeCachedQuery? How do I get the count from the returned object?

    Thanks

  • #2
    if (result.next()) {
    num_records = result.getString(1);
    }
    JJ Mouris
    FFE Luxembourg
    IT-Department

    Comment


    • #3
      try this example...
      var newsql = "SELECT Count(*) FROM tbl_Patient WHERE PatCorpNumber = '" + pi + "'";

      var newresult = dbConn.executeCachedQuery(newsql);

      // Fetch the result row.
      newresult.next();

      //If no match, Insert new row into Patients and visits
      if (newresult.getInt(1) == 0)


      Ed

      Comment


      • #4
        Thanks. Worked great.

        Comment


        • #5
          Originally posted by edwelch4 View Post
          try this example...
          var newsql = "SELECT Count(*) FROM tbl_Patient WHERE PatCorpNumber = '" + pi + "'";

          var newresult = dbConn.executeCachedQuery(newsql);
          newresult.next();
          if (newresult.getInt(1) == 0)


          Ed
          I get com.mirth.connect.server.userutil.MirthCachedRowSe [email protected] as my count... what am I doing wrong:
          var newsql = "SELECT count(*) FROM table WHERE MRN = '+MRN+'";
          var newresult = dbConn.executeCachedQuery(newsql);
          newresult.next();
          if (newresult.getInt(1) == 0)

          Comment


          • #6
            Originally posted by mourisj View Post
            if (result.next()) {
            num_records = result.getString(1);
            }
            When I run this I get a count of 0, I am expecting 143.
            var newsql = "SELECT count(*) FROM table WHERE MRN = '+MRN+'";
            var result = dbConn.executeCachedQuery(newsql);
            if (result.next()) {
            num_records = result.getString(1);
            }

            Running select count(*) from table WHERE MRN = '+MRN+'"; from my server I get 143 so why is this different? This is driving me nuts.

            Comment


            • #7
              OMG, talk about a brain dead mistake..........

              My issue was the MRN variable in my SELECT statement was '+MRN+' and not "+MRN+"
              double quotes for variables and single for text..... that was a 4+ hour mistake.

              Sorry for necroing this post.

              Comment

              Working...
              X