
In Part 1, we saw how to configure a JDBC Adapter, and in Part 2, we saw how to update field value using JDBC Adapter but using one entry at a time.
USE [SID] GO
/****** Object: StoredProcedure [dbo].[storedProcedureName] Script Date: 29/9/2021 8:16:52 AM ******/
SET
ANSI_NULLS ON GO
SET
QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[storedProcedureName] @XMLParameterName as XML AS BEGIN
SET
NOCOUNT ON;
begin tran begin try INSERT INTO [dbo].storedProcedureName ([FIELD1], [FIELD2], [FIELD3])
SELECT
CUST.item.value('(FIELD1)[1]', 'nvarchar(250)') as 'FIELD1',
CUST.item.value('(FIELD2)[1]', 'nvarchar(250)') as 'FIELD2',
CUST.item.value('(FIELD3)[1]', 'nvarchar(250)') as 'FIELD3',
GETDATE() AS [PROCESSDATE]
FROM
@XMLParameterName.nodes('/ElementName/item') as CUST(item) commit tran end try begin catch DECLARE @ErrorMessage NVARCHAR(4000);
DECLARE @ErrorSeverity INT;
DECLARE @ErrorState INT;
SELECT
@ErrorMessage = ERROR_MESSAGE(),
@ErrorSeverity = ERROR_SEVERITY(),
@ErrorState = ERROR_STATE();
--Print @ErrorMessage
RAISERROR (
@ErrorMessage, @ErrorSeverity, @ErrorState
);
rollback tran end catch END
Sookriti: Hey Baranee. First of all, thank you so much for writing the stored procedure. I have a few questions, are you ready?
Sookriti: Haha. This meme is a delight for a "The Office" fan. So, please tell our readers a little about the stored procedure.
CREATE PROCEDURE PROCEDURE_NAME @parameter1 varchar(250),
@ parameter2 varchar(250),
@ parameter3 varchar(250) AS BEGIN INSERT INTO TABLE_NAME ([column1], [column2], [column3)
VALUES
(
@parameter1, @ parameter2, @ parameter3
) END
CREATE PROCEDURE PROCEDURE_NAME @XMLparameter as XML AS BEGIN
SET
NOCOUNT ON;
INSERT INTO TABLE_NAME ([column1], [ column2], [ column3)
SELECT
CUST.item.value('(column1)[1]', 'nvarchar(250)') as column1,
CUST.item.value('(column2)[1]', 'nvarchar(250)') as column2,
CUST.item.value('(column3)[1]', 'nvarchar(250)') as column3
FROM
@ XMLparameter.nodes(
'/node1/node2) as CUST(item)
END
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="ElementName">
<xs:complexType>
<xs:sequence>
<xs:element name="StatementName" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element name="storedProcedureName">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="table"/>
<xs:element name="XMLParameterName" maxOccurs="unbounded" minOccurs="0">
<xs:complexType>
<xs:simpleContent>
<xs:extension base="xs:string">
<xs:attribute type="xs:string" name="type"/>
<xs:attribute type="xs:string" name="isInput"/>
</xs:extension>
</xs:simpleContent>
</xs:complexType>
</xs:element>
</xs:sequence>
<xs:attribute type="xs:string" name="action"/>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>
Now, here is what we need to pass into this structure?
<?xml version="1.0" encoding="UTF-8"?>
<ElementName>
<StatementName>
<storedProcedureName action="EXECUTE">
<table>storedProcedureName</table>
<XMLParameterName type="CLOB">
<![CDATA[
<ElementName>
<item>
<Field1>Value1.1</Field1>
<Field2>Value2.1</Field2>
<Field3>Value3.1</Field3>
</item>
<item>
<Field1>Value1.2</Field1>
<Field2>Value2.2</Field2>
<Field3>Value3.2</Field3>
</item>
<item>
<Field1>Value1.3</Field1>
<Field2>Value2.3</Field2>
<Field3>Value3.3</Field3>
</item>
</ElementName>
]]>
</XMLParameterName>
</storedProcedureName>
</StatementName>
</ElementName>
You have already seen the Before and After images, but I am posting it again for special effects. Haha 😂
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
5 | |
5 | |
4 | |
4 | |
4 | |
3 | |
3 | |
3 | |
3 | |
3 |