Jump to content

Simple ExecuteSQL expression is wrong.


This topic is 2774 days old. Please don't post here. Open a new topic instead.

Recommended Posts

This is just about as simple as an ExecuteSQL gets, but I can't figure out why it generates a "?".

ExecuteSQL ( 
"SELECT Count (\"voteYes\") FROM \"MotionVote\" WHERE \"motionYear\" = '2016' ";
 "" ; "" 
)

MotionVote is a TO; voteYes and motionYear are fields therein; voteYes can either be 1 or blank. The objective is to display a count of "yes" votes. What did I miss?

Link to comment
Share on other sites

Answer #1: you don't need to wrap the year in single quotes.

Answer #2: you don't need to escape the names with double quotes (since they aren't reserved words, don't contain spaces or apostrophes, or begin with an underscore character), and you can use a dynamic parameter, which frees you up from having to worry about whether to use single quotes or not, e.g.,

ExecuteSQL ( 
"SELECT Count ( voteYes ) 
FROM MotionVote 
WHERE motionYear = ?" 
; "" ; "" ; 2016
)

Answer #3: for more info, check out this blog posting -- https://filemakerhacks.com/2012/05/02/fm-12-executesql-dynamic-parameters/

Edited by Kevin Frank
  • Like 1
Link to comment
Share on other sites

On reflection, having re-read your message, I would be inclined to construct the query like this:

ExecuteSQL ( 
"SELECT Count ( * ) 
FROM MotionVote 
WHERE motionYear = ? 
   AND voteYes = ?" 
; "" ; "" ; 2016 ; 1 
)

It makes more sense to me to constrain the results (via the WHERE clause), and then simply count them... but really that's just my preference.

Link to comment
Share on other sites

Quote

Would your second construct be a better generic template, then?

I think so. It would never have occurred to me to approach it the way you did.

It makes more sense to me to do all the heavy lifting in the WHERE clause.

Also, for the sake of argument, if voteYes contained 0 and 1 rather than nothing or 1, my second version would return the correct answer but the first would not (since the zeros would be counted also).

Link to comment
Share on other sites

  • 1 month later...

This topic is 2774 days old. Please don't post here. Open a new topic instead.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.