{"id":1361,"date":"2012-04-14T03:06:02","date_gmt":"2012-04-14T10:06:02","guid":{"rendered":"http:\/\/mclasen.com\/wordpress\/?p=1361"},"modified":"2012-04-14T03:06:02","modified_gmt":"2012-04-14T10:06:02","slug":"filemaker-explore-the-support-group-a-little-bit-of-sql-in-filemaker-12","status":"publish","type":"post","link":"https:\/\/mclasen.com\/wordpress\/2012\/04\/filemaker-explore-the-support-group-a-little-bit-of-sql-in-filemaker-12\/","title":{"rendered":"FileMaker Explore | The Support Group: A little bit of SQL in FileMaker 12"},"content":{"rendered":"<div class='posterous_autopost'>\n<div>\n<div>\n<h2><img height=\"22\" alt=\"read\" style=\"display: inline;\" width=\"22\" \/>&nbsp;&#8211;><a href=\"http:\/\/www.supportgroup.com\/explore\/a-little-bit-of-sql-in-filemaker-12\/\">FileMaker Explore | The Support Group: A little bit of SQL in FileMaker 12<\/a><\/h2>\n<div>\n<p>One of my favorite new features of FileMaker Pro 12 is the <strong>ExecuteSQL<\/strong> function, which allows you to perform a SELECT SQL query against your FileMaker tables. <strong>ExecuteSQL<\/strong> allows you to use SQL to create calculations that otherwise might be very difficult or cumbersome. For example, in the past, if you wanted to get the list of values that had been used anywhere in the field <strong>areaCode<\/strong> in your <strong>contact<\/strong> table, you had to first create a value list using that field, then create the calculation: <strong>ValueListItems ( Get ( FileName ) ; \u201cAreaCodes\u201d )<\/strong>. It worked, but it was a bit of a hack to have to create that extra value list.<\/p>\n<p>Now, all you need is:<\/p>\n<\/p>\n<div>\n<div class=\"CodeRay\">\n<div class=\"code\">\n<pre>ExecuteSQL ( &quot;SELECT DISTINCT areaCode FROM contact&quot; ; &quot;&quot; ; &quot;&quot; ) \/* The last two empty parameters tell FileMaker to use the default comma and carriage return as column and row separators *\/<\/pre>\n<\/div><\/div>\n<\/p><\/div>\n<p>Okay, so it does require learning a bit of SQL (check out the <a href=\"http:\/\/www.w3schools.com\/sql\/default.asp\">W3Schools\u2019 SQL Tutorial<\/a>). But many queries are straightforward, and using SQL this way can simplify the relationship graph. <\/p>\n<p>Say I wanted to get all the area codes used in Massachusetts. Using the old value list method, I would need to create a field somewhere to store the abbreviation \u201cMA\u201d, create a relationship from that field to the <strong>contact::state<\/strong> field, and define my value list to use only related values starting from the table with the new field. With <strong>ExecuteSQL<\/strong>, all I need is to update the formula to:<\/p>\n<div>\n<div class=\"CodeRay\">\n<div class=\"code\">\n<pre>ExecuteSQL ( &quot;SELECT DISTINCT areaCode FROM contact WHERE UPPER(state)=?&quot; ; &quot;&quot; ; &quot;&quot; ; &quot;MA&quot; ) \/* The fourth parameter is used to fill in the ? in the query. It could be a field instead of a text constant *\/<\/pre>\n<\/div><\/div>\n<\/p><\/div>\n<p>Here\u2019s one of my favorites: often it\u2019s useful to know the name of the base table (the ones define on the Tables tab of the Manage Database dialog) that is the source for a table occurrence (the tables that appear in the graph). You may have lots of table occurrences, named <strong>leads<\/strong>, <strong>students<\/strong>, <strong>personsOfInterest<\/strong>, <strong>relatives<\/strong>, and so one, but each is really an instance of the <strong>contact<\/strong> table. Outside the Manage Database dialog, any reference to a table is really a reference to a table occurrence (so <strong>Get ( LayoutTableName )<\/strong> returns <strong>leads<\/strong>, for example, not <strong>contact<\/strong>). <\/p>\n<p>In the past, it would be necessary to name each consistently to include the underlying base table (so <strong>contact_leads<\/strong>, <strong>contact_students<\/strong>, <strong>contact_personsOfInterest<\/strong>, etc.), and then parse the table occurrence name. Be careful changing table names! But with SQL, you can use the following to get the name of the base table of the current layout:<\/p>\n<div>\n<div class=\"CodeRay\">\n<div class=\"code\">\n<pre>ExecuteSQL ( &quot;SELECT BaseTableName FROM FileMaker_Tables WHERE TableName=?&quot; ; &quot;&quot; ; &quot;&quot; ; Get ( LayoutTableName ) )<\/pre>\n<\/div><\/div>\n<\/p><\/div>\n<p> &nbsp; <br \/> <strong>FileMaker_Tables<\/strong> is a special table you can query via SQL to get a list of all table occurrences, and the <strong>BaseTableName<\/strong> column tells you the name of the source table.<\/p>\n<p>You can replace <strong>Get ( LayoutTableName )<\/strong> with any table occurrence name to get it\u2019s base table. I like the following calculation field that stores the name of the table as data, which can be extremely useful when exporting and importing data:<\/p>\n<div>\n<div class=\"CodeRay\">\n<div class=\"code\">\n<pre>Let ( [ FQFN = GetFieldName ( Self ) ; tableOccurrence = GetValue ( Substitute ( FQFN ; &quot;::&quot; ; \u00b6 ) ; 1 ) ] ; ExecuteSQL ( &quot;SELECT BaseTableName FROM FileMaker_Tables WHERE TableName=?&quot; ; &quot;&quot; ; &quot;&quot; ; tableOccurrence ) )<\/pre>\n<\/div><\/div>\n<\/p><\/div>\n<p>The two variables in the <strong>Let<\/strong> function first get the fully qualified field name (including table occurrence name) of the calculation, an then parses out just the table occurrence name for use in the query. Since the calculation never makes reference to anything but Self, you can copy and paste this calculation field to any table without needing any editing.<\/p>\n<p>What\u2019s your favorite use of <strong>ExecuteSQL<\/strong>? <\/p>\n<\/div><\/div>\n<p> <\/p>\n<table border=\"0\" width=\"95%\">\n<tr>\n<td valign=\"bottom\" width=\"62%\">(Unofficial) Planet FileMaker <span>by Chad Novotny (<a href=\"mailto:chad@supportgroup.com\">chad@supportgroup.com<\/a>)<\/span><\/td>\n<td valign=\"bottom\" width=\"38%\">April 13, 2012 1:25 PM<\/td>\n<\/tr>\n<tr>\n<td colspan=\"2\"><\/td>\n<\/tr>\n<\/table><\/div>\n<div>\n<p \/>Sent from my iPad<\/div>\n<p style=\"font-size: 10px;\">  <a href=\"http:\/\/posterous.com\">Posted via email<\/a>   from <a href=\"http:\/\/mclasen.posterous.com\/filemaker-explore-the-support-group-a-little\">mclasen&#8217;s posterous<\/a>  <\/p>\n<\/p><\/div>\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>&nbsp;&#8211;>FileMaker Explore | The Support Group: A little bit of SQL in FileMaker 12 One of my favorite new features of FileMaker Pro 12 is the ExecuteSQL function, which allows you to perform a SELECT SQL query against your FileMaker tables. ExecuteSQL allows you to use SQL to create calculations that otherwise might be very [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[3],"tags":[],"class_list":["post-1361","post","type-post","status-publish","format-standard","hentry","category-maccconsulting"],"aioseo_notices":[],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p834Wu-lX","jetpack-related-posts":[],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/posts\/1361","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/comments?post=1361"}],"version-history":[{"count":1,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/posts\/1361\/revisions"}],"predecessor-version":[{"id":1362,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/posts\/1361\/revisions\/1362"}],"wp:attachment":[{"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/media?parent=1361"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/categories?post=1361"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mclasen.com\/wordpress\/wp-json\/wp\/v2\/tags?post=1361"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}