Jump to content
View in the app

A better way to browse. Learn more.

FMForums.com

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.
Juggernaut

ExecuteSQL count of external sql table not working (count with relationship works)

Featured Replies

I need some info from many tables from an SQL system and I have ODBC working and I can use the data through relationships with filemaker commands and almost everything is fine.

 

I'd like to count the records in various SQL tables without setting up relationships and I've tried a large number of variations and searched the posts and web with no luck.

 

For example In a calculation:

ExecuteSQL ("Select Count () from Dflt.epCLC_Step3_RAN50";"";"")

yields a ?

 

This is a simple table of one field of scrambled IDs, just 50 records.

 

Eventually, I plan to have the table specified by a filemaker field to present record counts for different tables that might be of interest.

 

What do I need to do to get a record count without establishing a relationship?

 

ExecuteSQL ("Select Count () from Dflt.epCLC_Step3_RAN50";"";"")

yields a ?

 

That's because your syntax isn't correct; you need to count something, even if it is anything (note the asterisk):

ExecuteSQL (" SELECT COUNT ( * ) FROM "Dflt.epCLC_Step3_RAN50" " ; "" ; "" )
assuming that Dflt.epCLC_Step3_RAN50 is the name of an existing TO.
 
You need to escape the TO name since a dot is a delimiter in SQL syntax; best to get hold of some Custom Functions (or write them yourself) that not only protect your FileMaker element references against schema changes, but also preemptively escape those names for use in a SELECT statement.
  • Author

That solved it instantly!

I had initially used the * and I also had tried escaping the table name, as well as too many things to mention, but never hit the correct combination.

 

Now, I just tried playing with getting the TO name entered into the calculation from another field, and I tried escaping that as well, but can't get that to work.

I also tried using the ?:

ExecuteSQL ("Select Count (*) From ?";"";"";Table Name)

Table Name is a field in the current table.  I suspect I have to add a calculation to generate the appropriately escaped TO name.

 

Do you have a suggestion for that syntax, with the TO name is in a field called Table Name?

 

Thank you very much,

-Bruce

Do you have a suggestion for that syntax, with the TO name is in a field called Table Name?

 

I think you cannot use the optional ExecuteSQL parameters for table or field references.

 

But the SELECT statement you pass is simply a text string, so write:

ExecuteSQL ( " SELECT Count (*) FROM " & Quote ( Table Name ) ; "" ; "" )

btw, you don't need to store the table name:

Let ( [
to = GetValue ( Substitute ( GetFieldName ( anyFieldFromYourTable ) ; "::" ; ¶ ) ; 1 ) ;
~sql = ExecuteSQL ( " SELECT Count (*) FROM " & Quote ( to ) ; "" ; "" )
] ;
ExecuteSQL ( ~sql ; "" ; "" )
)

which has the added benefit that you work with a TO name, which it was ExecuteSQL actually uses as reference.

 

As you may imagine, it would make sense to put the logic for querying the TO name (and wrapping it in quotes, while you're at it) into a Custom Function; change the GetValue argument to 2 to get the field name.

Also, since you're referencing a name from the internal FileMaker schema, changing the field or TO name won't break any existing SQL statements.

  • Author

That totally does it.  And I greatly appreciate your speedy replies.

I never would have come up with that syntax.  I don't think I've ever used the Quote function.

 

In my current incarnation, I have Table Name fed by a drop down list populated by a field calculation:  TableNames("") and now, when I select the desired table, I instantly see the total record count in the SQL table desired.

 

I'll investigate the other points that you mentioned.

 

Thank you!!!

Bruce

Create an account or sign in to comment

Important Information

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

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.