Home Database Mysql Tutorial Sql Server2005 利用XML一次更新多条记录

Sql Server2005 利用XML一次更新多条记录

Jun 07, 2016 pm 03:08 PM
sql xml use renew

很久么写博客了,惭愧。今天终于搞定了很久以来一直郁闷的一个问题,写个日志备注下。 我想很多人都知道,在oracle里面,存储过程里面可以传入数组(如int[]),也就是说,可以传多条 记录 到数据,从而一起 更新 。减少数据库的请求次数。 但SqlServer呢?b

很久么写博客了,惭愧。今天终于搞定了很久以来一直郁闷的一个问题,写个日志备注下。
我想很多人都知道,在oracle里面,存储过程里面可以传入数组(如int[]),也就是说,可以传多条记录到数据,从而一起更新。减少数据库的请求次数。
但SqlServer呢?bulk Insert这个很多人都知道,我也知道,但可惜,我从来没用过,只有导数据的时候才会考虑,但导数据DTS不是更方便吗?
手头的一个项目,有几个功能,每次需要更新N(N记录,记录不多,但如果每次只更新一条,循环insert,那每个功能需要N次请求数据库,如果有1000个并发,那数据库除了做你这个事情,其他的活不用干了。所以,需要尽量减少数据库请求,做到一次更新所有的记录
幸好,SqlServer给我们提供了一个新功能,利用XML(2000好像是没有这个功能的)。
先来假定一个这样的需求:用户更新一个book,同时需要更新N个章节。
一般的思路是这样,先更新book,然后循环章节数,N次更新数据的章节表。大家可以看下这个性能。

那我们用XML试试

Sql Server2005 利用XML一次更新多条记录利用XML更新的存储过程

Create PROCEDURE UP_Book_Insert
(
    
@BookId        INT,
    
@ChapterXml    XML

AS
BEGIN
CREATE TABLE #table
(
    ChapterId        
INT,
    ChapterName        
VARCHAR(255),
    Price            
INT
);
INSERT #table
    
SELECT *
    
FROM   (
               
SELECT X.C.value('Id[1]''int'AS ChapterId,
                      X.C.value(
'Name[1]''varchar(255)'AS ChapterName,
                      X.C.value(
'Price[1]','int'AS Price
               
FROM   @ChapterXml.nodes('Chapter'AS X(C)    --注意:这里的X(C)命名空间是需要的
           ) t;    
    
INSERT INTO tbChapter(BookId,ChapterId,ChapterName,Price) 
    
SELECT @BookId,ChapterId,ChapterName,Price from #table;
END

其实,在存储过程里面可以把临时表去掉的。

然后我们执行下看看

Sql Server2005 利用XML一次更新多条记录执行存储过程

exec UP_Book_Insert 10000,'268第268章100273第273章100275第275章100'

怎么样?不错吧。只需要在存储过程里面对XML格式进行解析。

而在c#里面,XML格式可以传入DbType.String类型就可以了。

再写一个函数来生成XML格式的字符串

Sql Server2005 利用XML一次更新多条记录生成XML格式的函数

public static string FormatXmlInfo(ListChapterInfo> list)
        {
            
if (list==null||list.Count0)
            {
                
return String.Empty;
            }
            StringBuilder sb = new StringBuilder();
            
foreach (ChapterInfo info in list)
            {
                sb.AppendFormat("{0}{1}{2}", info.ChapterId, info.ChapterName, info.Price);
            }
            
return sb.ToString();
        }


 好了,完成了。

性能具体怎么样,还没进行测试,但肯定的一点是,比多次请求数据库,或者在存储过程里面循环分割字符串效率要高。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to fix Blizzard Battle.net update stuck at 45%? How to fix Blizzard Battle.net update stuck at 45%? Mar 16, 2024 pm 06:52 PM

How to fix Blizzard Battle.net update stuck at 45%?

What is the difference between HQL and SQL in Hibernate framework? What is the difference between HQL and SQL in Hibernate framework? Apr 17, 2024 pm 02:57 PM

What is the difference between HQL and SQL in Hibernate framework?

Usage of division operation in Oracle SQL Usage of division operation in Oracle SQL Mar 10, 2024 pm 03:06 PM

Usage of division operation in Oracle SQL

Comparison and differences of SQL syntax between Oracle and DB2 Comparison and differences of SQL syntax between Oracle and DB2 Mar 11, 2024 pm 12:09 PM

Comparison and differences of SQL syntax between Oracle and DB2

How to install Angular on Ubuntu 24.04 How to install Angular on Ubuntu 24.04 Mar 23, 2024 pm 12:20 PM

How to install Angular on Ubuntu 24.04

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Detailed explanation of the Set tag function in MyBatis dynamic SQL tags Feb 26, 2024 pm 07:48 PM

Detailed explanation of the Set tag function in MyBatis dynamic SQL tags

How to update MSI graphics card driver? MSI graphics card driver download and installation steps How to update MSI graphics card driver? MSI graphics card driver download and installation steps Mar 13, 2024 pm 08:49 PM

How to update MSI graphics card driver? MSI graphics card driver download and installation steps

Lantern and Dungeon updated on February 29: Remastered version ╳ 'Legend of Nezha' linkage Lantern and Dungeon updated on February 29: Remastered version ╳ 'Legend of Nezha' linkage Feb 28, 2024 am 08:13 AM

Lantern and Dungeon updated on February 29: Remastered version ╳ 'Legend of Nezha' linkage

See all articles