An SQL SELECT statement can have a FOR XML clause that returns the query result as XML instead of a rowset. You can also have a stored procedure that has a SELECT statement with a FOR XML clause. FOR XML (SQL Server) has more information.
You can use the WCF-based SQL adapter to execute such stored procedures.
The “native” SQL adapter available with BizTalk Server requires stored procedures to have the FOR XML clause as part of the SELECT statement. You can use such stored procedures with the WCF-based SQL adapter without making any changes to the stored procedure definition.
You can always use the schemas generated using the ‘native’ SQL adapter provided with earlier versions of BizTalk Server. For more information, see Using FOR XML queries with the WCF-SQL Adapter.
When you invoke a stored procedure with FOR XML clause in SQL Server Management Studio or using the SQL adapter available with BizTalk Server, the output is in the form of an xml message. To use these procedures with the WCF-based SQL adapter, you must have the schema for the output message. The WCF-based SQL adapter requires this schema while receiving the response message from SQL Server after executing a stored procedure with FOR XML clause. Note that the request message to invoke this stored procedure will be generated by the adapter itself.
Apart from having the schema for the response message, you must also perform certain tasks to invoke a stored procedure with FOR XML clause using the WCF-based SQL adapter.
You do not need to perform this step if you have the response schema generated by the SQL adapter available with BizTalk Server.
You can generate the schema for the response message for the stored procedure, provided the SELECT statement in the stored procedure has the xmlschema clause with the for xml clause. In this topic, we use the GET_EMP_DETAILS_FOR_XML stored procedure that retrieves the employee details for a given employee ID. To retrieve the schema by executing the stored procedure, the SELECT statement looks like the following:
SELECT [Employee_ID] ,[Name] ,[DOJ] ,[Designation] ,[Job_Description] ,[Photo] ,cast([Rating] as varchar(100)) as Rating ,[Salary] ,[Last_Modified] ,[Status] ,[Address] FROM [Adapt_Doc].[dbo].[Employee] for xml auto, xmlschema
Execute this stored procedure to get the schema for the response message. Note that the response from the stored procedure contains the schema as well as the data from executing the stored procedure. You must copy the schema from the response and save it to a text pad. For this example, you can name this schema as ResponseSchema.xsd. You must now create a BizTalk project in Visual Studio and add this schema to the project.
Make sure you remove the xmlschema clause after you have executed the stored procedure to generate the schema. If you fail to do this, when you finally execute the stored procedure through BizTalk, you will again generate the schema in the response message. So, to get the response message as xml you must remove the xmlschema clause.
import schemaLocation=”sqltypes.xsd”
To generate schema for the request message you can use the Consume Adapter Service Add-in from a BizTalk project in Visual Studio. For this topic, generate the schema for the GET_EMP_DETAILS_FOR_XML stored procedure. For more information about how to generate the schema using Consume Adapter Service Add-in, see Retrieving Metadata for SQL Server Operations in Visual Studio using the SQL adapter.
You must generate the schema by selecting the procedure only from the Procedures node in Consume Adapter Service Add-in.
The schema that you generated earlier describes the “types” required for the messages in the orchestration. A message is typically a variable, the type for which is defined by the corresponding schema. You must now create messages for the orchestration, and link them to schemas that you generated in the previous step.
Use this | To do this |
---|---|
Identifier | Type Request |
Message Type | From the drop-down list, expand Schemas, and then select ForXMLProcedure.Procedure_dbo.GET_EMP_DETAILS_FOR_XML, where ForXMLProcedure is the name of your BizTalk project. Procedure_dbo is the schema generated for invoking the GET_EMP_DETAILS_FOR_XML procedure. |
Use this | To do this |
---|---|
Identifier | Type Response |
Message Type | From the drop-down list, expand Schemas, and then select ForXMLProcedure.ResponseSchema, where ResponseSchema is the name of the response schema generated by executing the stored procedure as described under Generating Schema for the Response Message for Stored Procedure. |
You must create a BizTalk orchestration to use BizTalk Server for executing a stored procedure in SQL Server. In this orchestration, you drop a request message at a defined receive location. The SQL adapter consumes this message and passes it on to SQL Server. The response from SQL Server is saved to another location. You must include Send and Receive shapes to send messages to SQL Server and receive responses, respectively. A sample orchestration for invoking a procedure resembles the following:
Make sure you specify the following properties for each of the message shapes. The names listed in the Shape column are the names of the message shapes as displayed in the just-mentioned orchestration.
Shape | Shape Type | Properties |
---|---|---|
ReceiveMessage | Receive | - Set Name to ReceiveMessage - Set Activate to True |
SendMessage | Send | - Set Name to SendMessage |
ReceiveResponse | Receive | - Set Name to ReceiveResponse - Set Activate to False |
SendResponse | Send | - Set Name to SendResponse |
Make sure you specify the following properties for each of the logical ports. The names listed in the Port column are the names of the ports as displayed in the orchestration.
Port | Properties |
---|---|
MessageIn | - Set Identifier to MessageIn - Set Type to MessageInType - Set Communication Pattern to One-Way - Set Communication Direction to Receive |
LOBPort | - Set Identifier to LOBPort - Set Type to LOBPortType - Set Communication Pattern to Request-Response - Set Communication Direction to Send-Receive |
ResponseOut | - Set Identifier to ResponseOut - Set Type to ResponseOutType - Set Communication Pattern to One-Way - Set Communication Direction to Send |
The following table specifies the properties and their values that you should set to specify messages for action shapes and to link the messages to the ports. The names listed in the Shape column are the names of the message shapes as displayed in the orchestration mentioned earlier.
Shape | Properties |
---|---|
ReceiveMessage | - Set Message to Request - Set Operation to MessageIn.FOR_XML.Request |
SendMessage | - Set Message to Request - Set Operation to LOBPort.FOR_XML.Request |
ReceiveResponse | - Set Message to Response - Set Operation to LOBPort.FOR_XML.Response |
SendResponse | - Set Message to Response - Set Operation to ResponseOut.FOR_XML.Request |
After you have specified these properties, the message shapes and ports are connected and your orchestration is complete.
You must now build the BizTalk solution and deploy it to a BizTalk Server. For more information, see Building and Running Orchestrations.
After you have deployed the BizTalk project, the orchestration you created earlier is listed under the Orchestrations pane in the BizTalk Server Administration console. You must use the BizTalk Server Administration console to configure the application. For a walkthrough, see Walkthrough: Deploying a Basic BizTalk Application.
Configuring an application involves:
XmlProcedure//So, for this topic where we are executing the GET_EMP_DETAILS_FOR_XML procedure, the action will be:
XmlProcedure/dbo/GET_EMP_DETAILS_FOR_XML
For more information about setting action, see Configure the SOAP action for the SQL adapter . You must also set the following binding properties when executing a stored procedure with the FOR XML clause.
Binding property name | Set this to |
---|---|
XmlStoredProcedureRootNodeName | Specify the name of the root node that you added to the response schema you generated for the stored procedure, as described under Generating Schema for the Response Message for Stored Procedure. For this topic, set this to Root. |
XmlStoredProcedureRootNodeNamespace | Specify the target namespace for the response schema you generated for the stored procedure, as described under Generating Schema for the Response Message for Stored Procedure. For this topic, set this to http://ForXmlStoredProcs/namespace . |
For more information about specifying values for binding properties, see Configure the binding properties for the SQL adapter.
Note Generating the schema using the Consume Adapter Service BizTalk Project Add-in also creates a binding file that contains information about the ports and the actions to be set for those ports. You can import this binding file from the BizTalk Server Administration console to create send ports (for outbound calls) or receive ports (for inbound calls). For more information, see Configure a physical port binding using a port binding file to use the SQL adapter.
You must start the BizTalk application for invoking procedures in SQL Server database. For instructions on starting a BizTalk application, see How to Start an Orchestration.
At this stage, make sure:
After you run the application, you must drop a request message to the FILE receive location. The schema for the request message must conform to the request schema for the procedure you generated using the Consume Adapter Service Add-in. For example, the request message to invoke the GET_EMP_DETAILS_FOR XML is:
10765
See Message Schemas for Procedures and Functions for more information about the request message schema for invoking procedures in SQL Server database using the SQL adapter.
The orchestration consumes the message and sends it to SQL Server database. The response from SQL Server database is saved at the other FILE location defined as part of the orchestration. For example, the response from SQL Server database for the preceding request message is:
Notice that the response is received in the same schema as generated by executing the stored procedure. Also note that the root node and the namespace is the same you specified as values for XmlStoredProcedureRootNodeName and XmlStoredProcedureRootNodeNamespace binding properties respectively.
After you have deployed and configured the BizTalk project, you can export configuration settings to an XML file called the binding file. Once you generate a binding file, you can import the configuration settings from the file, so that you do not need to create items such as send ports and receive ports for the same orchestration. For more information about binding files, see Reuse adapter bindings.