Problem:
How to execute and get response from SQL stored procedure using BizTalk SQL adapter and orchestration
Scenario Details:
We have a stored procedure that accepts customer-id as input parameter and returns customer record from Customer table based upon input customer-id. We want to execute this stored procedure inside an orchestration and get customer record back using BizTalk SQL adapter.
Solution:
For demo purpose we will first create Customer table in db followed by stored procedure creation using following sql:
Table SQL script:
Stored Procedure SQL Script:
Note:
Xmldata keyword (encircled in red color) is added to sql select statement in order to enable BizTalk “Generate Adapter Metadata” wizard generate required scripts, schemas and orchestration so that we can execute this stored procedure within the orchestration and get response back via SQL adapter.
Now create new empty BizTalk project, name it “SQLAdapterTesting”. Right click the project and click on Add -> Add Generated Items… option
On Add Generated Item screen, click on “Add Adapter Metadata” and click “Add” button
On Add Adapter Wizard, select “SQL” under the list of registered adapters, specify SQL Server name and select “BizTalkMgmtDB” as database
On next screen, set connection string by specifying SQL Server name, login type (Integrated security or username & password) and then select the database which contains the stored procedure in question
On next screen, specify “Target namespace” and select port type “Receive port” if you want to receive response (only) from SQL server or select port type “Send port” if you want to send request to SQL server and receive back response
On next screen, select statement type “Stored Procedure”
On next screen, select the specific stored procedure in dropdown list and click on “Generate” button
On next screen, click on “Finish” button to complete wizard
This will result in following two new artifacts added to same BizTalk solution:
- BizTalk Orchestration.odx
- SQLService.xsd
SQLService.xsd schema will contain schema for a Multipart message that will be used to send request to SQL adapter and receive response back
BizTalk Orchestration.odx orchestration contains required port types and multi part message types, needed to communicate with SQL adapter and send/receive data
Now go back to SQL server and modify the stored procedure created above by removing “xmldata” keyword from the select statement as it is no longer required after completion of Adapter Metadata wizard.
Now Add new schema (CustomerInquirySchema.xsd) to current project. This schema will be the schema of input message/request with CustomerId in it against which record from the Customer table will be retrieved
Add new schema (CustomerInquiryResponseSchema.xsd) to current project. This schema will represent the final message that will be output to FileSystem
Now create new messages in orchestration as following:
InputMsg
OutputMsg
sqlRequestMsg
sqlResponseMsg
Now add new map (InputMsgToSQLInputMsgMap.btm) to the project with mapping as following:
Above map will be added to the Transform shape in orchestration to convert input message to SQL adapter request message
Add another map file (SQLRespToFileSystem.btm) to the project with mapping as following:
Above map will be added to the transform shape in orchestration to transform SQL adapter response to final output message
Now add following ports to the orchestration:
- Receive port (to receive input message)
- Two-way SQL Adapter based port (to send request to SQL Adapter and receive response back)
- One way Send port to output receive response (from SQL) to local file system.
After adding above mentioned ports and two transform shapes (for two maps mentioned above respectively), final and complete orchestration should look like:
Now strongly sign project and build + deploy project
Open deployed orchestration in BizTalk and bind send/receive ports. While configuring SQL adapter based send-receive (two way) port, select “PassThruTransmit” as Send Pipeline & “XMLReceive” as Receive Pipeline and finally specify document target namespace and Response document root element node as following:
Note:
Make sure that the document Target Namespace specified in SQL adapter port configuration matches 100% with the Target namespace that was specified in Adapter Metadata Wizard configurations (at the start of this article) and there should be no discrepancy of any letter including / or _ etc
Now Start newly deployed orchestration (after completing port and host bindings) and test it with following sample input message:
Sample Input Message:
Upon successful execution, the orchestration will output following XML response to specified filesystem path:
Final Output Response:
Conclusion:
BizTalk uses adapters to communicate with outside world. BizTalk SQL Adapter fells under BizTalk DB adapters category. Using SQL adapter to execute stored procedure and get result back has certain advantages over using a component to execute sql stored procedure. Advantages include but not limited to retry attempts option, reliable delivery, success/failure delivery notifications, comprehensive adapter port tracking in BizTalk and the option to specify alternative delivery mechanism in case of failure/error