Topics   All   Mac OS X (Only)   Windows (Only)   Crossplatform   Server (Not)   Client   Components   Old   Guides
New in version: 4.0   4.1   4.2   4.3   4.4   5.0   5.1   5.2   5.3   5.4   6.0   6.1   6.2   6.3    Statistic  

FM.ExecuteSQL

Component: FM FMSQL
Version: 1.2
Mac OS X: Yes
Windows: Yes
Server: Yes

MBS( "FM.ExecuteSQL"; SQL Statement; Column Delimiter; Record Delimiter )

Parameters

Parameter Description Example value
SQL Statement SQL Statement as a Text string
Column Delimiter Columns Separator, only used with SELECT statement. Default is TAB ""
Record Delimiter Record Separator, only used with SELECT statement, Default is Return

Result

Returns result of SQL expression or error.

Description

Execute the SQL Statement against the current FileMaker File.
There must be an occurrence of the Table on the current tables' Graph. SELECT returns the the records in as a tabbed delimited text string. All other SQL commands return "".
Use FM.ExecuteSQL.LastError to check for errors.
Supported Commands: SELECT UPDATE (see limitation below) DELETE INSERT CREATE TABLE DROP TABLE CREATE INDEX DROP INDEX WHERE
String comparisons are Case Senstive! WHERE First_Name="todd" will not find records that have "Todd" in the first Name Field. You can wrap the field you are searching for with "lower" like this WHERE lower(First_Name)="todd" KNOWN LIMITATIONS Schema editing SQL functions are not supported in FileMaker 8 and 8.5.
Record Locking Errors are Not Reported! This means you must exercise caution when using UPDATE. CAUTION! This is a very powerful function! It is relying on very new and relatively untested resources made available by the release of FileMaker 8.
Seems like on FileMaker 11 you get LastError 8310 for any syntax error, like bad braces or missing quotes. You can put field names in double quotes and string values in single quotes.
Use FM.ExecuteSQLOnIdle for special commands which change database like ALTER TABLE.
Use FM.ExecuteSQL.LastErrorMessage and FM.ExecuteSQL.LastError to see error message.
Error 8309 means you did pass wrong data type, like string for a number.

If you need to pass parameters, please use FM.ExecuteFileSQL.

Examples

SELECT records

MBS("FM.ExecuteSQL";"select * from Contacts")

INSERT record

MBS("FM.ExecuteSQL";"INSERT INTO Contacts (FirstName, LastName) Values('Bob', 'Miller')")

Select records using delimiters

MBS("FM.ExecuteSQL";"select * from Contacts"; "#"; "|")

SELECT records with a Where clause

MBS("FM.ExecuteSQL"; "SELECT First_Name, Last_Name from Contacts WHERE ID_Contact_PK ='CN10013'")

Get list of fields in all tables:

MBS("FM.ExecuteSQL";"SELECT * FROM FileMaker_Fields")

Create Record with values via SQL

Set Variable [$r; Value:MBS( "FM.ExecuteSQL"; "INSERT INTO \"Export records\" (\"First Name\", \"Last Name\", \"Street\", \"Zip\", \"City\", \"Country\", \"Phone\") VALUES ('MyFirst', 'MyLast', 'MyStreet', 'MyZip', 'MyCity', 'MyCountry', 'MyPhone')")]
If [MBS("IsError")]
    Show Custom Dialog [MBS( "FM.ExecuteSQL.LastErrorMessage" )]
End If

Set value for all records of a table:

MBS( "FM.ExecuteSQL"; "UPDATE \"My Table\" SET \"My Field\" = 'Hello' ")

Set timestamp to today:

MBS( "FM.ExecuteSQL"; "UPDATE \"my Table\" SET \"Change Time\" = TODAY WHERE \"ID\" = '12345' ")

Update 5 fields:

MBS( "FM.ExecuteSQL"; "UPDATE \"my Table\" SET \"Number Field" = 1, \"Text Field" = 'test', \"Dest Field\" = \"Source Field\", \"Hour Field\" = CAST(HOUR(curtime) AS VARCHAR), \"Empty FIeld\" = NULL WHERE \"ID\" = '12345' ")

Select with passing variable as part of expression:

MBS("FM.ExecuteSQL"; "SELECT First_Name, Last_Name from Contacts WHERE ID_Contact_PK ='" & $Variable & "'")

Update date field:

MBS( "FM.ExecuteSQL"; "UPDATE myTable SET myDateField = CAST('2015-08-15' as DATE) " )

Delete all data in a table:

$Result = MBS( "FM.ExecuteSQL"; "DELETE FROM \"Prova\";")

Create an index:

MBS( "FM.ExecuteSQL"; "CREATE INDEX ON tablename.fieldname" )

Drop an index:

MBS( "FM.ExecuteSQL"; "DROP INDEX ON tablename.fieldname" )

Value in List of values:

MBS( "FM.ExecuteSQL"; "select * from \"Anlagen\" where \"Modell\" in ('test', 'abc')")

Delete all records from Table People:

# no where condition, so deletes all records!
MBS( "FM.ExecuteSQL"; "DELETE FROM \"People\"")

See also


FM.ExecuteFileSQL   -   FM.ExecuteSQL.LastError

Feedback: Report problem or ask question.




Links
MBS Real Studio PDF Plugins - JUZ Nickenich