DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Trending

  • The Agentic Agile Office: Streamlining Enterprise Agile With Autonomous AI Agents
  • Building a Vector Index in Azure AI Search: HNSW, Profiles, and RAG Retrieval
  • Orchestrating Zero-Downtime Deployments With Temporal
  • The Hidden Cost of AI Tokens: Engineering Patterns for 10x Resource Efficiency
  1. DZone
  2. Data Engineering
  3. Databases
  4. Send a Table-Valued Parameter to a MS SQL Stored Procedure in Mule 4

Send a Table-Valued Parameter to a MS SQL Stored Procedure in Mule 4

One interesting variation of sending data via table-valued parameter (TVP) is not so commonly used. Let's talk about how to get started.

By 
Rahul Gavande user avatar
Rahul Gavande
·
Dec. 13, 20 · Tutorial
Likes (1)
Comment
Save
Tweet
Share
7.3K Views

Join the DZone community and get the full member experience.

Join For Free

Introduction

Most of us have called a stored procedure in Mule to pass data to or receive data from database tables. However, one interesting variation of sending data via table-valued parameter (TVP) is not so commonly used. This article will show you how to send data in a TVP format to an MS SQL table via a stored procedure in Mule 4.

Before we proceed with the actual Mule development, let's first understand what a table-valued parameter is. As the name suggests, it is a parameter where data is sent in the form of a table, comprised of columns and rows. Within the database, table-valued parameters are declared by using user-defined table types. The main purpose of a TVP is to pass multiple values for a record, e.g. Multiple contact number values for a user record merged in the record itself. The benefit is that it associates the values, maintains the sequence, and reduces code complexity in the stored procedure.

Sending the TVP

Before you can start developing the logic for generating a TVP, you will need to import the Java module in your Mule Project. This is required as we will be invoking methods in a Java class. If you have already imported it or are familiar with the process of importing it, you can skip the next section. 

In Anypoint Studio, open your configuration XML, and in Mule Palette select Add Module. You should notice the Java module under the Featured column. Drag it to the adjacent column to add it to your project. 

mule palette

Now that the module has been imported, we can start building the TVP. There are 3 stages involved in the process: Initialising a TVP, adding columns to it, and finally adding rows of data, after which it can be sent to the related stored procedure.

In this example, we will create AccountTeamsTable TVP for an Account record. The TVP contains 3 columns, viz. EmployeeEmail, Division, and RoleName.

initialize-accountUserRoleDivision-table

To initialize, add a variable as below, vAccountTeamsTableand define the Value as below. Here tvpName dbo.AccountTeam is the user-defined data type (UDDT) for the TVP as created within the database. The class name is com.microsoft.sqlserver.jdbc.SQLServerDataTable.

Initialize TVP

In the next stage, we add columns to our TVP. Ensure that you add columns in the same order as defined in the UDDT, or else the data values will not be aligned. In this example, we will first add EmployeeEmail column. 

Add Invoke operation of the Java module to the flow and name it appropriately. Add TVP variable AccountTeamsTable to the Instance. In the Args section, add two arguments; first is the column name matching the UDDT column name and the second one is SQLType which indicates the data type of that particular column. The Class again is com.microsoft.sqlserver.jdbc.SQLServerDataTableand the Method is addColumnMetadata(String,int). 

add EmployeeEmail column interface

SQLType values can be found on this page. If you cannot locate it easily, search for NVARCHARand the second instance will take you to the appropriate table.

Add all the remaining columns from your TVP in a similar fashion, ensuring you maintain the sequence.

In the third stage, we need to add data into the TVP. Pass the input payload to a For Each which converts each row to the TVP type object and then adds the row to the TVP.

update-accountUserRoleDivision-table

In the transform message, convert the payload record to an Array. Again ensure that sequence matches the UDDT.

Generate Row from Payload

The Convert to Object[] step, also an Invoke operation of the Java module:

Convert to Object[]

Finally, the Add rowstep, an Invoke operation as well, the variable vAccountTeamsRowis added to the TVP variable vAccountTeamsTable.

Add row 

Let's test this code with the below payload:

payload variables

As seen in the below debug snapshot, the TVP was properly generated and a row added for the sample value. ThecolumnMetadatamust align with the UDDT defined in the database.

debug snapshot with properly generated TVP

After adding all the rows, the TVP parameter can be passed to the stored procedure as any other parameter.

Microsoft SQL Server Database sql

Opinions expressed by DZone contributors are their own.

Related

  • How to Recover a Deleted Table in a SQL Server Database
  • Restoring the MS SQL Server Database in Easy Steps
  • How To Convert MySQL Database to SQL Server
  • How To Fix SQL Database Restore Failed, Database Is in Use

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook