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

  • DuckDB for Python Developers
  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Chat with Your Oracle Database: SQLcl MCP + GitHub Copilot
  • Push Filters Down, Not Up: The Data Layer Design Principle Most Developers Learn Too Late

Trending

  • Persistent Memory for AI Agents Using LangChain's Deep Agents
  • Jakarta EE 12: Entering the Data Age of Enterprise Java
  • Liquid Glass, Material 3, and a Lot of Plumbing
  • Advanced Error Handling and Retry Patterns in Enterprise REST Integrations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Call a Stored Procedure From Mule ESB

Call a Stored Procedure From Mule ESB

In this article, I am going to explain how to call a stored procedure that accepts custom defined table types.

By 
Baranee Manoharan user avatar
Baranee Manoharan
·
Updated Dec. 17, 19 · Tutorial
Likes (6)
Comment
Save
Tweet
Share
30.4K Views

Join the DZone community and get the full member experience.

Join For Free

Man talking on cell phone outside

In this article, I am going to explain how to call a stored procedure that accepts custom-defined table types. If you have to send multiple records in a single time to the DB from your front end, that's where we use custom defined table types. 

These types allow multiple rows, which contain different data types like varchar, boolean, etc.

I have a table type below:

SQL
x
11
 
1
USE [myown] GO 
2
/****** Object: UserDefinedTableType [dbo].[tblcustomers] Script Date: 6/2/2018 4:11:08 PM ******/
3
create TYPE [dbo].[tblcustomers] AS TABLE(
4
  [Title] [nvarchar](8) NULL, 
5
  [Suffix] [nvarchar](10) NULL, 
6
  [CompanyName] [nvarchar](128) NULL, 
7
  [SalesPerson] [nvarchar](256) NULL, 
8
  [EmailAddress] [nvarchar](50) NULL, 
9
  [PasswordHash] [nvarchar](128) NOT NULL, 
10
  [PasswordSalt] [nvarchar](10) NOT NULL
11
) GO


A stored procedure, which has table typed parameter is as follows:

SQL
xxxxxxxxxx
1
 
1
USE [myown] GO create PROCEDURE [dbo].[fetchtblCusomters](
2
  @tblcustomers tblcustomers READONLY
3
) AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON -- Insert statements for procedure here select * from @tblcustomers END


Now we have to call this stored procedure in Mule, and my flow looks like below:

Image title

XML
xxxxxxxxxx
1
26
 
1
<?xml version="1.0" encoding="UTF-8"?>
2
<mule
3
    xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking"
4
    xmlns:db="http://www.mulesoft.org/schema/mule/db"
5
    xmlns:json="http://www.mulesoft.org/schema/mule/json"
6
    xmlns:http="http://www.mulesoft.org/schema/mule/http"
7
    xmlns="http://www.mulesoft.org/schema/mule/core"
8
    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
9
    xmlns:spring="http://www.springframework.org/schema/beans"
10
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd http://www.mulesoft.org/schema/mule/db http://www.mulesoft.org/schema/mule/db/current/mule-db.xsd http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
11
    <db:generic-config name="Generic_Database_Configuration" url="jdbc:sqlserver://baraneesql.database.windows.net:1433;database=myown;user=baranee@baraneesql;password=****;encrypt=true;trustServerCertificate=false;hostNameInCertificate=*.database.windows.net;loginTimeout=30;" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" doc:name="Generic Database Configuration"></db:generic>
12
    <flow name="tabletypeflow">
13
        <http:listener config-ref="HTTP_Listener_Configuration" path="/tabletype" allowedMethods="GET" doc:name="HTTP"></http:listener>
14
        <set-variable variableName="records" value="#[new com.microsoft.sqlserver.jdbc.SQLServerDataTable();]" doc:name="Variable"></set>
15
        <expression-component doc:name="create-records">
16
            <![CDATA[records.addColumnMetadata("Title" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("Suffix" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("CompanyName" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("SalesPerson" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("EmailAddress" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("PasswordHash" ,java.sql.Types.NVARCHAR); records.addColumnMetadata("PasswordSalt" ,java.sql.Types.NVARCHAR); records.addRow('Dr', 'Hr', 'Mindtree', 'tiptop', '[email protected]', '', ''); ]]>
17
        </expression-component>
18
        <db:stored-procedure config-ref="Generic_Database_Configuration" doc:name="Database">
19
            <db:parameterized-query>
20
                <![CDATA[{CALL fetchtblCusomters(:tblcustomers )}]]>
21
            </db:parameterized-query>
22
            <db:in-param name="tblcustomers" value="#[flowVars.records]"></db:in>
23
        </db:stored-procedure>
24
        <json:object-to-json-transformer doc:name="Object to JSON"></json:object>
25
    </flow>
26
</mule> 

Mule Steps 

  1. Create a flow Variable object for the class SQLServerDataTable

  2. Add columns to the table type object you have created.

    1. records.addColumnMetadata("Suffix" ,java.sql.Types.NVARCHAR); — In here, Suffix is a column table and it is NVARCHAR SQL type

    2. Once you are done adding columns, add rows to the table

      1. records.addRow('Dr', 'Hr', 'Mindtree', 'tiptpop', '[email protected]', '', '');

    3. Now, using database connector, call the Stored Procedure, which has table type parameter.    

Image title

Your SQL Server DB connection will be:

Image title

That's it, you have your multiple records flying through to SQL Server DB in a single time.

Database Enterprise service bus sql

Opinions expressed by DZone contributors are their own.

Related

  • DuckDB for Python Developers
  • Custom Model Context Protocol (MCP) for NL2SQL: A Rigorous Evaluation Framework on Oracle Database
  • Chat with Your Oracle Database: SQLcl MCP + GitHub Copilot
  • Push Filters Down, Not Up: The Data Layer Design Principle Most Developers Learn Too Late

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