Altova MapForce 2025 Enterprise Edition

Using Stored Procedures to Generate Keys

Home Prev Top Next

This example shows you how to insert some key (ID) generated by a stored procedure into another table, with the help of local relations.

 

Let us first create the demo stored procedure in the "AdventureWorks" database. To do this, run the script below against the database. You can do this from a query window of Microsoft SQL Server Management Studio, or directly from the DB Query pane of MapForce, see Browsing and Querying Databases. In either case, make sure that your database user account has permission to create stored procedures.

 

CREATE PROCEDURE Production.uspAddProductModelEx
 @ModelName nvarchar(50)

 ,@Inst xml

 ,@ProductModelID int OUTPUT
AS

BEGIN
INSERT INTO [Production].[ProductModel]
          ([Name]
          ,[Instructions]
          ,[rowguid]
          ,[ModifiedDate])
    VALUES
          (@ModelName
          ,@Inst
          ,NEWID()
          ,GETDATE())
  SELECT @ProductModelID = SCOPE_IDENTITY()
END

 

The stored procedure above takes two parameters (@ModelName, @Inst) as input and performs an INSERT operation into the ProductModel table. It then returns the generated @ProductModelID as output parameter. The requirement is to insert the @ProductModelID returned by the stored procedure into the ProductModelIllustration table.

 

The following steps show you how to create a mapping that satisfies the requirement above.

 

1.Connect to the "AdventureWorks" database from MapForce, as described in Adding Stored Procedures to the Mapping. Make sure that your database user account has permission to view and execute stored procedures.

2.When prompted to choose database objects, select the ProductModelIllustration table and the uspAddProductModelEx stored procedure.

mf_sp_26

3.Click the Show Context Menu mf_ic_storedproc_menu button next to the stored procedure and select Show Nodes As Target. The stored procedure now appears as target component on the mapping, where the left side lists the input parameters.

mf_sp_27

4.Optionally, if you want to execute the stored procedure inside a transaction, click the Show Context Menu mf_ic_storedproc_menu button again, select Procedure Settings, and then select the Use Transactions check box. Defining the transaction for the stored procedure ensures that retrieving the key and inserting the record occur during the same transaction.

5.Right-click the title bar of the database component and select Add/Remove/Edit Database Objects from the context menu.

6.Click Add/Edit Relations, and then click Add Relation. Define the relationships as shown below.

mf_sp_28

7.Click OK to close the dialog box. Notice that the ProductModelIllustration table now appears as a child of the stored procedure. The stored procedure output parameter (@ProductModelID) is displayed as an indicator that it will be used in the local relation, but it does not have any input or output connectors.

mf_sp_29

8.In this example, the @Inst parameter is of XML type. Right-click the @Inst parameter on the component and select Assign XML Schema to Field from the context menu. Next, select the Production.ManuInstructionsSchemaCollection schema from the database. When prompted to select a root element, leave the default value as is, and click OK. For more information about mapping data to database XML fields, see Mapping XML Data to / from Database Fields.

mf_sp_30

9.Add the source components that provides data to be inserted into the database. In this example, the source data is supplied by constants; however, any other source component supported by MapForce could act as input. For more information about constants, see Add a Constant to the Mapping.

mf_sp_31

Since this mapping updates a database, you do not preview its output directly like with other mappings. Instead, click the Output button to display the pseudo-SQL containing hints about how the database will be modified. If you enabled transactions, these will occur as indicated by the comments.

mf_sp_32

The pseudo-SQL displayed in the Output pane does not show the actual transaction commands, only hints (as comments). The actual SQL commands are sent to the underlying database API, however.

 

To run the mapping against the database, do one of the following:

 

On the Output menu, click Run SQL-Script.

Click the Run SQL-Script mf_ic_run-sql toolbar button.

© 2018-2024 Altova GmbH