SQL Form Connectors – Custom SQL

SQL Form Connectors – Custom SQL

There are two ways to control the flow of data to your SQL tables using one of our SQL form connectors (SQL Server, MySQL, or PostgreSQL): “Auto Maintain” and “Custom SQL“.

In this article, we’ll look at “Custom SQL” and how you can use it in your SQL form connectors for even greater control of your SQL data.

Custom SQL Overview

When managing your data through an SQL form connector, the first and most popular option is to enable the Auto Maintain option on the form connector. When this option is set on your form connector, our platform will (as the name suggests) auto-maintain the SQL table to which it writes data. This can be very convenient, as if, for example, you remove various fields from your form and add some new ones, “Auto Maintain” will ensure that the SQL table connected to your form is also updated so that the records can be written there without you having to do anything.

For most use cases, this will suffice; however, some users need the option to do more advanced data processing by writing their own custom SQL scripts providing total control over what data goes to what tables in their SQL configuration. For these users, we recommend the “Custom SQL” option on their SQL form connector.

Custom SQL is available on all of our SQL form connectors. To enable it, untick the Auto Maintain Table option on your respective SQL form connector, and the Custom SQL Template text input box will appear.

Usage Scenarios

To give you an idea of typical use cases when you might find Custom SQL on our SQL form connectors to be the best option, consider these scenarios:

  • Whenever form data is submitted, you wish to write data to various tables for data analytics and reporting purposes. Custom SQL lets you write only the data you need to the tables you wish to write to, ensuring your production and reporting systems have the data they need to support your operations.
  • You need to maintain a Platform Sync (Hosted GET) table with _lastupdated and/or _deleted values. Custom SQL gives you the granular control necessary to maintain your data with the greatest precision.

Custom SQL allows you to write multiple queries, which will be executed upon form submission. This opens up a number of possible use cases that are far too many to list in this article.

Custom SQL Details

Our SQL Form Connectors can execute multiple queries simultaneously in a Custom SQL statement.

SQL Connector Performance

The total time for this SQL connector to execute is about 15 seconds. All form connectors are subject to a 5 second timeout delay for establishing a connection. If our platform is not able to connect to your database within 5 seconds, then the connector will fail. All SQL connectors have a 10 second maximum execution timeout. This means that if all of your SQL commands don’t execute within 10 seconds after establishing a connection to your database, then the connector will fail.

To add Custom SQL code to your SQL form connector, you enter the custom SQL you want the form connector to execute in the “Custom SQL Template” text area.

Custom SQL Transaction

The Custom SQL code supports the execution of more than one statement; however, all SQL statements must execute within a single SQL transaction.

Custom SQL Syntax

While the syntax used to populate the “Custom SQL Template” text area is regular SQL, you can use our platform’s data templating syntax to inject Form field placeholders as well as define repeating sections in your Custom SQL syntax, as shown in the example below:

Sample SQL Syntax
DELETE FROM [MyTableName] WHERE Id = CAST({{%ENTRYGUIDFULL}} as uniqueidentifier);
INSERT INTO [MyTableName] (
[id],
[itemno],
[formversion],
[myField1],
[myfield2],
[myfieldn]
)
VALUES {{!REPEATSTART}} (
CAST({{%ENTRYGUIDFULL}} as uniqueidentifier),
CAST({{%ITEMNO as int}}),
CAST({{%FORMVERSION}} as int),
CAST({{myField1}} as datetime),
CAST({{myfield2}} as nvarchar),
CAST({{myfield2}} as int)
), {{!REPEATEND}};

A special built-in data command is available to SQL connectors, namely {{%UTCNOW}}, which will contain the date and time (in UTC) of when the query is executed. This can be useful for updating the _lastupdated column if you use Platform Sync.

This built-in data command, along with ENTRYGUID, ENTRYGUIDFULL, and ITEMNO, is always guaranteed to have a value regardless of whether “Fill Repeat Rows” is unchecked (i.e., using NULL in Repeat Rows).

Sample Custom SQL Code

To give you an idea of a typical use case for Custom SQL, we’ve added some sample code that you can use as a guide for writing your own SQL scripts and controlling the flow of your platform-generated data to your SQL databases and tables.

Conditional SQL Statements

There may come a time when you’d like to use SQL’s conditional logic to give you the flexibility to execute various SQL statements only when a certain condition has been met. To do this, you can take advantage of SQL’s native IF ELSE logic to control the execution of your Custom SQL scripts, as shown in the example below:

Sample SQL Syntax
IF EXISTS (SELECT * FROM tb_project WHERE projectID = 'o}xoxprojectID}}')
BEGIN
UPDATE tb_projects
SET project = '{{project}}', description= '{{description}}', client = '{{client}}', projecttype = '{{projecttype}}', user = '{{user}}', foreman = '{{foreman}}'
WHERE projectID = '{{projectID}}'
END
ELSE
BEGIN
INSERT INTO tb_projects (projectID, project, description , client, projecttype, user, foreman)
VALUES('{{projectID}}', '{{project}}', '{{description}}', '{{client}}', '{{projecttype}}', '{{creator}}', '{{foreman}}')
END

Repeatable Rows in Custom SQL

If you ever want to write data from a repeatable table/page in your form to an SQL table, the syntax below will help you do so.

Sample SQL Syntax
BEGIN
INSERT INTO SQL_Test_form (Id, firstName, lastName)
{{!REPEATSTART}}
VALUES({{h_id}}, {{firstName}}, {{lastName}}),
{{!REPEATEND}}
END
    • Related Articles

    • Form Connector – SQL Server

      After adding and authenticating an SQL Server Connection, you can add Data Source Connectors to pull data from a database or push data on form submissions using Form Connectors, which is what this article is about. Our platform lets you quickly and ...
    • Form Connector – PostgreSQL

      After adding and authenticating a PostgreSQL Connection, you can add Data Source Connectors to pull data from a database or push data on form submissions using Form Connectors, which this article is about. Our platform lets you quickly and easily ...
    • Form Connector – Google BigQuery

      Our platform’s Premium BigQuery connector provides a direct link to Google’s BigQuery database service. This tool allows you to automatically insert your form entries as new rows in a target BigQuery database table. This creates a one-way ...
    • Form Connector – MySQL

      After adding and authenticating a MySQL Connection, you can add Data Source Connectors to pull data from a database or push data on form submissions using Form Connectors, which is what this article is about. Our platform lets you quickly and easily ...
    • Connecting to SQL Server

      Authenticating a connection to Microsoft SQL Server enables communication between the two platforms. This must be successfully added before Form Connectors can push or Data Sources can pull data. Adding The Data Hub area of the platform is where you ...