Import a Stored Procedure

Digitise Forms allows you to access existing databases using SQL Stored Procedures as an alternative to accessing the database directly. The Stored Procedure must be, or have been, written outside Form Studio, stored in a SQL Server database and then be imported into the Studio. If the Stored Procedure is later modified, you can reread the Stored Procedure and change the Datasource and Datasets accordingly.

This topic describes how to create a Datasource and Dataset(s) from an existing Stored Procedure; if you want to access an existing database directly refer to the topic Create an Imported Datasource instead or if you want to create a Digitise Forms Datasource see: Create a Digitise Forms Datasource.

 

Before you import a Stored Procedure, it will help to know some basic information about importing Stored Procedures with Form Studio:

When you import a Stored Procedure, Form Studio will create a special Datasource to handle data input and output via that Stored Procedure. As with other Datasources, this Datasource will need one or more Datasets which define the data items relevant to the Stored Procedure. Each Stored Procedure you import will have a separate Datasource with its own Dataset. The Datasource will be named:

SP_<stored procedure schema>_<stored procedure name>

e.g. the Stored Procedure dbo.ReportsByDate would have a Datasource called SP_dbo_ReportsByDate.

Stored Procedures can be used to add new records to a database or update existing records. They can also send data back to your form, in a so called Result Set. An individual Stored Procedure may only insert or update records, only return one or more Result Sets or do a combination of both.

In the case of inserting and updating records, the Stored Procedure will include one or more SQL INSERT or UPDATE statements and will generally require the values to be inserted/updated in the database to be passed into it. These values are passed in using parameters, with each item of data to be passed in requiring a separate parameter. The required parameters are defined within the Stored Procedure, with each definition specifying a name for the parameter, the type of data it can hold, e.g. a number, date or text string, and optionally a maximum length for the data. Parameter names always start with '@', such as @Start_Date, or @Name.

Where a Stored Procedure returns one or more Result Sets, it will contain one or more SQL SELECT statements. In order to select the records to be returned, the Stored Procedure may require one or more values to be passed into it which specify the records to be returned. For example, a Result Set could return a set of records relating to the user of the form, such as a list of their appointments or purchases, and so would require the appropriate information to identify the current user. Once again, this information is passed into the Stored Procedure, in the same way as values for inserting/updating, via parameters.

If a Stored Procedure only performs inserts and/or updates, you will need a Dataset, called Parameters, whose data fields match the Stored Procedure's parameters. This Dataset must contain a data field for each of the parameters and the data field definitions must match those of the parameter definitions in the Stored Procedure. You can't have only a sub-set of the required parameters.

If a Stored Procedure returns one or more Result Sets, however, you can choose which Result Sets you want to use within your form and which columns from each Result Set you want to be downloaded to your form. For each Result Set you want to use, you will need a corresponding Dataset with data fields matching columns returned in the Result Set. The Dataset data fields must have the same names as their corresponding Result Set columns and have matching definitions in terms of data type and, for text string fields, max length. In addition, you will also need a SQL query which will be used to execute the Stored Procedure. If the Stored Procedure also requires parameters to be passed in, whether to perform inserts and updates, as search criteria or for any other reason, a Dataset IS NOT created for these parameters but they are passed in via the SQL query. These Datasets will be named after the database tables from which the data is extracted or the Stored Procedure, if an empty Dataset is created. If this results in Datasets having the same names, a consecutive sequence of numbers is appended to the end of the second and subsequent Datasets, e.g. MyDataset2.

When you import a Stored Procedure, Form Studio uses a Microsoft function to attempt to read and interpret the Stored Procedure to discover which parameters, if any, the Stored Procedure needs and whether it returns any Result Sets, and if so the format of those Result Sets. It will then present the results to you for you to check and, if all is well, it will create an appropriate Datasource, Dataset and SQL query for you.

Note, however, that Stored Procedures can be complex and the function may not always be able to correctly or fully interpret the Stored Procedure. Input parameters should be fine, but Result Sets may cause difficulties. For this reason you will need to know what your Stored Procedure should return, if anything, so that you can check what Form Studio thinks will be returned against what you expect. If the two don't match, you will have various choices open to you to allow you to create appropriate Datasets, including manually creating Datasets - these options will be discussed further below. If you are familiar with, and have access to, SQL Server Management Studio (SSMS), you may be able to check the Result Sets returned by a Stored Procedure by executing it in SSMS.

Note, also, that if a Stored Procedure returns more than one Result Sets, Form Studio will only be told about the first one found when the Stored Procedure is analysed. If you want to use this Stored Procedure but none of the others, you won't need to do anything extra. However, if you don't want to use this Result Set in your form or you want to use one or more other Result Sets as well, you will have various choices about how to create Datasets for the other Result Sets, including manually creating them - these options will be discussed later. Where you want to work with multiple Result Sets from a Stored Procedure, you will need to create a separate Dataset for each Result Set within the Stored Procedure's Datasource.

Finally, the function which interprets the Stored Procedure's functionality, may experience problems and return an error to Form Studio. In this situation, Form Studio will display the error and, if a Result Set can't be determined, present you with a choice of options, similar to those available when Result Sets can't be correctly interpreted, to allow you to create the required Dataset.

It will help to import a Stored Procedure if you are familiar with SSMS and have permissions to view, edit and execute the Stored Procedure within it. If you aren't able to do this, you will need to get a description of any Result Sets you want to use from your SQL administration team.

For information about using the Stored Procedure Datasource and Datasets within your form, see Use an Imported Stored Procedure.

 

To import a Stored Procedure:

  • Click on the lower half of the New Datasource button on the Ribbon's Data tab and then choose Import Stored Procedure,
  • Right-click on the background of the Project Explorer's Datasources tab and choose Import Stored Procedure

    or

  • In the Project Explorer's Project tab, right-click on the Datasources node and choose Import Stored Procedure.

 

The SQL Database Connection dialog will appear, allowing you to specify the connection details for the remote database housing the Stored Procedure you want to import. Click on Save to continue or Cancel to abandon the import.

If you clicked Save, Form Studio will then connect to your chosen database and present a list of the Stored Procedures available:

Picture showing the Select Stored Procedure dialog box.

Select the Stored Procedure you want to import and click the OK button. Alternatively, to close the dialog without importing a Stored Procedure, click Cancel.

 

If you choose the OK button, Form Studio will query the content of the chosen Stored Procedure and display the results in the Import Stored Procedure dialog box. This dialog box will vary, depending upon the type of Stored Procedure. The image thumbnails below show the dialog box reporting different results, click on an image to display it larger or on the links below each thumbnail to open the image in a separate window. If an error message is displayed, click Continue to display the Stored Procedure Import dialog or Close to abandon the import.

 

  • If you want to be able to execute a Stored Procedure from your form but you are not interested in using any of the Result Sets it contains, you will still need to create a Datasource and Dataset for it, in order to generate a SQL query which can be used to execute the Stored Procedure.
  • Whilst, you may be able to rename the Datasources created when you import a Stored Procedure, we recommend that you leave the default names as they are. You can, however, change the names of the Datasets to something more meaningful if you want to.

 

 


See also:

Change an Imported Stored Procedure

Use an Imported Stored Procedure