Jump to content
Server Maintenance This Week. ×

ExecuteSQL failing


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

Recommended Posts

How come this works:

ExecuteSQL ( "

SELECT "_ID"

FROM Courses

WHERE "_CourseName" = ?

" ; "|" ; "" ; "Git Training")

But I can't seem to get these forms to work?

[These two return: "Specified table cannot be found" and it highlights the pipe in quotes]

ExecuteSQL ( "

SELECT "__ID"

FROM Courses

WHERE " & Quote("_CourseName") & " = " & Quote("Git Training") &

" ; "|" ; "" )

ExecuteSQL ( "

SELECT "__ID"

FROM Courses

WHERE "_CourseName" = " & Quote("Git Training") &

" ; "|" ; "" )

[This returns: ? ]

ExecuteSQL ( "

SELECT "__ID"

FROM Courses

WHERE "_CourseName" = "Git Training"

" ; "|" ; "" )

Been staring at these too long.

- J

Link to comment
Share on other sites

The quote function puts real quotes (") around the WHERE value. SQL expects single quotes ('). Which is why it is easier to pass it in as a parameter, when you do that, FM takes care of putting the single quotes there when it evaluates the sql syntax.

  • Like 1
Link to comment
Share on other sites

Personally, I use three custom functions to assist with creating SQL statements. They would allow me to write the statement like this...


ExecuteSQL (

"SELECT " & SQLField( Courses::__ID )

& " FROM " & & SQLTable( Courses::__ID )

& " WHERE " & SQLField( Courses::_CourseName ) & "=" & SQLQuote( "Git Training" )

)





Here are the custom functions:



/* SQLField ( name )



PURPOSE:

Return only the name of the field, without the relationship portion. As opposed to GetFieldName() which also returns the relationship. This is to be used in SQL queries to protect the field references from breaking if the field is renamed.

The Quote() function allows us to reference "SQL reserved" field names without having to manually escape them.



PARAMETERS:

(field) field field to return name of



DEPENDENCIES:

FileMaker Pro 10+



REVISIONS:

2011-MAR-25 - Created by Kevin Frank http://www.filemakerhacks.com/?p=1065

2011-JUL-01 - Dan Smith [email protected]

- renamed from GFN to SQLField

- added to documentation section

####################################################################################################*/



Let (

[

a = GetFieldName ( field ) ;

b = Substitute ( a ; "::" ; ¶ )

] ;

Quote ( GetValue ( b ; 2 ) )

)







/* SQLTable ( name )



PURPOSE:

Return only the relationship portion of a field, quoted for use in a FileMaker SQL statement.



PARAMETERS:

(field) field field to get table occurence name from



DEPENDENCIES:

FileMaker Pro 10+



REVISIONS:

2011-JUL-01 - Created by Dan Smith [email protected]

####################################################################################################*/



Let (

[

a = GetFieldName ( field ) ;

b = Substitute ( a ; "::" ; ¶ )

] ;

Quote ( GetValue ( b ; 1 ) )

)







/* SQLQuote ( name )



PURPOSE:

Wrap text in single quotes, for use with SQL queries.

The purpose of the Substitute is to "escape" any internally embedded apostrophes, e.g., O'Malley becomes 'O''Malley', which SQL will convert into O'Malley



PARAMETERS:

(text) text text to quote



DEPENDENCIES:

none



REVISIONS:

2011-MAR-25 - Created by Kevin Frank http://www.filemakerhacks.com/?p=1065

2011-JUL-01 - Dan Smith [email protected]

- renamed from Q to SQLQuote

- added to documentation section

####################################################################################################*/



"'" & Substitute ( text ; "'"; "''" ) & "'"

  • Like 3
Link to comment
Share on other sites

  • 6 months later...

Also, your _ID field must be quoted since in begins with an underscore.  While I understand you want to take advantage of field sorting to put your ID on top, lately I have been calling my keys simply ID.  Then the ID does not have to be quoted for the SELECT to work.  Same goes for CourseName

ExecuteSQL ( "
            SELECT ID
            FROM Courses
             WHERE CourseName = ?
            " ; "|" ; "" ; "Git Training")

 

I prefer to avoid having to escape quotes in the select statement to improve readability.

 

Darren Burgess

http://www.mightydata.com

Link to comment
Share on other sites

This topic is 4095 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.