Dev4Snow

Go back to ContentsGo back to previous page

Generate SQL using ChatGPT

Generate SQL using ChatGPT is a feature that works with the OpenAI’s GPT-3.5 artificial-intelligence large language models that powers ChatGPT, which for this use case is utilized to convert natural language queries into SQL statements and scripts. Since this is a new technology and Dev4Snow is an early adopter this feature is still considered to be in “beta” version and subject to changes.

To start Generate SQL using ChatGPT click on the option with the same name from the tree, or from the  icon in the SQL Tab.

 

Icons & actions:

Create a new Query Window: The Generate SQL using ChatGPT screen can have multiple Query Windows. Use this button to manually create a new window.

Delete a Query Window (or all): Remove the current Query Window or all the windows.

Switch between available Query Windows: The label between the arrows indicates which the current window is. If you click on the label you will see part of the content of the query on that window and you can click on it to switch to that window, or use the arrows on the sides to switch sequentially between windows.

Help: Click this icon to open this help file.

 

Input > Supporting objects (tables/views)

The Supporting objects are those objects (tables/views) that will be used by ChatGPT either to generate a query on them, as an example to build new objects, or to replace one of those objects by adding new columns or formulas. 

There are a some scenarios where you may not need to add supporting objects. For example you could ask ChatGPT to “Create a table for Sales Orders” without any supporting object, and it will generate a new table with the most common columns for a Sales Orders table. If you include supporting objects for that request ChatGPT will usually try to match the style of your objects to build the new table.

For most scenarios you will have to add supporting objects. Follow these steps to add or remove those objects:

  • Click on the field Add Supporting object(s).
  • Start typing part of a table or view name –or type the database name or schema name to bring all of the objects in those.
  • A list will display, use the arrow keys to select the object that you are looking for and hit <Enter>.
  • The object will be added to the list of supporting objects.
  • If you need to delete an object, select it from the list and hit <Del>.
  • Keep the list of supporting objects to the minimum possible, that way you will minimize errors and speed up the response.

When clicking one of the objects in the list of Supporting Objects, the DDL of the object will be shown in the right window next to the list. Use this DDL window as a help when writing the request to ChatGPT.

 

Input > Your request

In this area you need to specify two things:

  • Get this statement type: Select a value from the list to define which type of instruction you are looking ChatGPT to generate. Next to this value there is a checkbox to “Suggest beginning of request”, select it to suggest the beginning of the prompt to ChatGPT according to the statement that you want to generate. It is not mandatory to start with the suggested text; you can delete it and write it in a totally different way if you want to.
     
  • Describe your request in natural language: This is the actual request to ChatGPT. Enter the request in this box as if you were asking a person to do it. You don’t need to enter the exact table/view names, just ask your request in a natural language and ChatGPT will relate it to the objects that you selected previously, building the necessary joins and formulas to achieve your request. If the “Suggest beginning of request” checkbox was selected you will be presented with a possible beginning for that request; you can choose to delete it and write it in a different way, or even in a different language –instead of English.

After filling up your request you can click Generate statement to submit it to ChatGPT. If ChatGPT succeeds in generating a SQL statement the tab will be automatically switched to the Output tab and the SQL statement will show up.

If you accept the results click Adopt statement and the code will be automatically moved into a new SQL window where you can execute it. If the result is not what you expect click the Input tab, modify the request and click Generate statement again.

NOTE: Some SQL statements may not be fully functional, this is particularly true when generating Stored Procedures. The generated skeleton for Stored Procedures will usually come incomplete –probably this will improve in newer versions as ChatGPT evolves.

 

Example

In this example we want to produce a query based on this Data Model:

So we need to add the three tables as Supporting Objects. For adding the tables just enter part of the name of a table in the empty box under “Add supporting object(s)” and hit <Enter> after locating it:

 

Since we want to produce a SELECT statement, we have to pick up that statement type from the list:

And finally we just write the request in our natural language, then we click Generate statement:

The request and supporting objects are sent to ChatGPT which returns the following result in seconds:

Now you can click Adopt statement to move the query to a SQL Code window where you can run it to get the results:

 

IMPORTANT INFORMATION

  • Your actual data is not shared with ChatGPT: Only the metadata that you see on the Generate SQL using ChatGPT screen is shared with ChatGPT –including the table/view names, the DDL of the objects and the prompt with your request to ChatGPT.
     
  • Running queries on ChatGPT has costs associated to it: Do NOT abuse this service. Do NOT attempt to get general ChatGPT answers by using this service. This feature is intended to produce only SQL statements and scripts. Attempting to find a backdoor to the full service and/or abusing this feature with unlimited queries will cause the service and/or your Dev4Snow account to be terminated immediately.
     
  • ChatGPT consumption is measured in tokens: One word (input or output) usually represents a bit more than one token. The model in use is limited to 4,000 tokens per request, which means some very large queries may not bring the full result.