CREATE TABLE [dbo].[SpecialCustomers](
[CustomerID] [nchar](5) NOT NULL,
[CompanyName] [nvarchar](40) NOT NULL
CONSTRAINT [PK_SpecialCustomers] PRIMARY KEY CLUSTERED
(
[CustomerID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, OPTIMIZE_FOR_SEQUENTIAL_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
UPDATE SpecialCustomers SET CompanyName = 'A Name from Update'
WHERE CustomerID = 'AAAAA';
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO SpecialCustomers (CustomerID, CompanyName)
VALUES ('AAAAA', 'A Name from Insert');
END
CREATE PROCEDURE Upsert_SpecialCustomer
@CustomerID varchar(5),
@CompanyName varchar(40)
AS
BEGIN
UPDATE SpecialCustomers SET CompanyName = @CompanyName
WHERE CustomerID = @CustomerID;
IF @@ROWCOUNT = 0
BEGIN
INSERT INTO SpecialCustomers (CustomerID, CompanyName)
VALUES (@CustomerID, @CompanyName);
END
END
Upsert_SpecialCustomer @CustomerID = 'AAAAA', @CompanyName = 'A Name Upsert';
<root>
<statement>
<storedProcedureName action="EXECUTE">
<table>Upsert_SpecialCustomer</table>
<CustomerID isInput="true" type="VARCHAR">AAAAA</CustomerID>
<CompanyName isInput="true" type="VARCHAR">A Name from SP</CompanyName>
</storedProcedureName>
</statement>
</root>
CREATE PROCEDURE XMLBatchUpsert_SpecialCustomers
@XMLData XML
AS
BEGIN
SELECT
CUST.item.value('(CustomerID)[1]', 'varchar(5)') as CustomerID,
CUST.item.value('(CompanyName)[1]', 'varchar(40)') as CompanyName
INTO #TempSpecialCustomers
FROM
@XMLData.nodes('/root/item') as CUST(item)
UPDATE SpecialCustomers
SET CompanyName = T.CompanyName
FROM SpecialCustomers AS C, #TempSpecialCustomers AS T
WHERE C.CustomerID = T.CustomerID
INSERT INTO SpecialCustomers (CustomerID, CompanyName)
SELECT CustomerID, CompanyName
FROM #TempSpecialCustomers AS T
WHERE NOT EXISTS(SELECT * FROM SpecialCustomers WHERE CustomerID = T.CustomerID)
END
<root>
<statement>
<storedProcedureName action="EXECUTE">
<table>XMLBatchUpsert_SpecialCustomers</table>
<XMLData isInput="true" type="CLOB"><![CDATA[<root>
<item>
<CustomerID>AAAAA</CustomerID>
<CompanyName>AA Name</CompanyName>
</item>
<item>
<CustomerID>BBBBB</CustomerID>
<CompanyName>BB Name</CompanyName>
</item>
<item>
<CustomerID>CCCCC</CustomerID>
<CompanyName>CC Name</CompanyName>
</item>
<item>
<CustomerID>DDDDD</CustomerID>
<CompanyName>DD Name</CompanyName>
</item>
</root>]]></XMLData>
</storedProcedureName>
</statement>
</root>
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
12 | |
11 | |
8 | |
7 | |
5 | |
4 | |
4 | |
4 | |
3 |