简体   繁体   English

在通用列表中,值传递给xml。 sql如何执行插入和更新操作?

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

在此处输入图片说明 Scenario : 1.Form having gridview with 5 records. 场景:1.Form具有5条记录的gridview。 (binding records FROM database) 2.one more record am selecting to autocomplete and then adding to gridview. (绑定来自数据库的记录)2.再选择一条记录以选择自动完成,然后添加到gridview。 3.next deleted one more record from that gridview then passing values to DB through XML. 3.next从该gridview中删除了另一条记录,然后通过XML将值传递给DB。 my xml code like this 我的xml代码是这样的

<?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. 这是使用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. 检查Microsoft Technet参考以获取更多信息。

Edit: Here's some example code for using SQL MERGE alongside OPENXML 编辑:这是与OPENXML一起使用SQL MERGE的一些示例代码

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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