简体   繁体   中英

In genric list values passing to xml . How to perform Insert and update operations sql?

在此处输入图片说明 Scenario : 1.Form having gridview with 5 records. (binding records FROM database) 2.one more record am selecting to autocomplete and then adding to gridview. 3.next deleted one more record from that gridview then passing values to DB through XML. my xml code like this

<?xml version="1.0"?>
<ArrayOfLanuageInfo xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <SpokenLanuageInfo>
    <LanguageId>154</LanguageId>  
  </SpokenLanuageInfo>
  <SpokenLanuageInfo>
    <LanguageId>46</LanguageId>
  </SpokenLanuageInfo>
  <SpokenLanuageInfo>
    <LanguageId>53</LanguageId>
  </SpokenLanuageInfo>
</ArrayOfLanuageInfo>

here one recoded am deleted ,one record added so how to do ??
 -deleted record should remove from table
 -new record insert into table

Stored Procedure like this : 
// here am deleting all records based on id every time and then inserting table so i don't want like this //
 DELETE FROM [dbo].LANGUAGE WHERE  [HOSPITAL_ID]=@HOSPITAL_ID

                                  INSERT INTO [dbo].LANGUAGE
                                  (
                                     HOSPITAL_ID, LANGUAGE_ID
                                  )
                                 SELECT  @HOSPITAL_ID,LanguageId
                           FROM   
                              OPENXML(@XmlHandleSAVELANGUAG,'/ArrayOfLanuageInfo/LanuageInfo',2)  
                                   WITH   
                                    (  LanguageId INT )   
                    END

This is an ideal candidate for the use of a SQL MERGE. It allows you to insert/update/delete based on matching criteria in a single statement.

Check the Microsoft Technet reference for further information.

Edit: Here's some example code for using SQL MERGE alongside OPENXML

declare @idoc int;
declare @inputXml xml = N'
<data>
    <item>
        <id>1</id>
        <name>first</name>
    </item>
    <item>
        <id>2</id>
        <name>second</name>
    </item>
    <item>
        <id>5</id>
        <name>fifth</name>
    </item>
</data>';
-- idoc is the xml representation we work with
EXEC sp_xml_preparedocument @idoc OUTPUT, @inputXml;


-- setup our table to merge
DECLARE @myTable TABLE (id int primary key, name varchar(50))
INSERT INTO @myTable (id, name)
VALUES 
    (1, 'one'),
    (2, 'two'),
    (3, 'three');

SELECT * FROM @myTable

-- merge the selected XML content into myTable
-- if we match on ID, update the name
-- if we don't find a match in the table, insert the entry in the XML
-- if we don't find a match in the XML, delete the entry in the table

MERGE INTO @myTable AS Target
USING 
    OPENXML(@idoc, 'data/item', 2) 
    WITH (id int 'id/text()', name nvarchar(50) 'name/text()')
    AS source
ON Target.id = source.id
WHEN MATCHED THEN UPDATE SET name = source.name
WHEN NOT MATCHED BY TARGET THEN INSERT (id, name) VALUES (id, name)
WHEN NOT MATCHED BY SOURCE THEN DELETE;

SELECT * FROM @myTable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM