Sql Server2005 利用XML一次更新多条记录
很久么写博客了,惭愧。今天终于搞定了很久以来一直郁闷的一个问题,写个日志备注下。 我想很多人都知道,在oracle里面,存储过程里面可以传入数组(如int[]),也就是说,可以传多条 记录 到数据,从而一起 更新 。减少数据库的请求次数。 但SqlServer呢?b
很久么写博客了,惭愧。今天终于搞定了很久以来一直郁闷的一个问题,写个日志备注下。
我想很多人都知道,在oracle里面,存储过程里面可以传入数组(如int[]),也就是说,可以传多条记录到数据,从而一起更新。减少数据库的请求次数。
但SqlServer呢?bulk Insert这个很多人都知道,我也知道,但可惜,我从来没用过,只有导数据的时候才会考虑,但导数据DTS不是更方便吗?
手头的一个项目,有几个功能,每次需要更新N(N记录,记录不多,但如果每次只更新一条,循环insert,那每个功能需要N次请求数据库,如果有1000个并发,那数据库除了做你这个事情,其他的活不用干了。所以,需要尽量减少数据库请求,做到一次更新所有的记录。
幸好,SqlServer给我们提供了一个新功能,利用XML(2000好像是没有这个功能的)。
先来假定一个这样的需求:用户更新一个book,同时需要更新N个章节。
一般的思路是这样,先更新book,然后循环章节数,N次更新数据的章节表。大家可以看下这个性能。
那我们用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
其实,在存储过程里面可以把临时表去掉的。
然后我们执行下看看
执行存储过程
exec UP_Book_Insert 10000,'
怎么样?不错吧。只需要在存储过程里面对XML格式进行解析。
而在c#里面,XML格式可以传入DbType.String类型就可以了。
再写一个函数来生成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("
}
return sb.ToString();
}
好了,完成了。
性能具体怎么样,还没进行测试,但肯定的一点是,比多次请求数据库,或者在存储过程里面循环分割字符串效率要高。

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



Blizzard Battle.net update keeps stuck at 45%, how to solve it? Recently, many people have been stuck at the 45% progress bar when updating software. They will still get stuck after restarting multiple times. So how to solve this situation? We can reinstall the client, switch regions, and delete files. To deal with it, this software tutorial will share the operation steps, hoping to help more people. Blizzard Battle.net update keeps stuck at 45%, how to solve it? 1. Client 1. First, you need to confirm that your client is the official version downloaded from the official website. 2. If not, users can enter the Asian server website to download. 3. After entering, click Download in the upper right corner. Note: Be sure not to select Simplified Chinese when installing.

HQL and SQL are compared in the Hibernate framework: HQL (1. Object-oriented syntax, 2. Database-independent queries, 3. Type safety), while SQL directly operates the database (1. Database-independent standards, 2. Complex executable queries and data manipulation).

"Usage of Division Operation in OracleSQL" In OracleSQL, division operation is one of the common mathematical operations. During data query and processing, division operations can help us calculate the ratio between fields or derive the logical relationship between specific values. This article will introduce the usage of division operation in OracleSQL and provide specific code examples. 1. Two ways of division operations in OracleSQL In OracleSQL, division operations can be performed in two different ways.

Oracle and DB2 are two commonly used relational database management systems, each of which has its own unique SQL syntax and characteristics. This article will compare and differ between the SQL syntax of Oracle and DB2, and provide specific code examples. Database connection In Oracle, use the following statement to connect to the database: CONNECTusername/password@database. In DB2, the statement to connect to the database is as follows: CONNECTTOdataba

Angular.js is a freely accessible JavaScript platform for creating dynamic applications. It allows you to express various aspects of your application quickly and clearly by extending the syntax of HTML as a template language. Angular.js provides a range of tools to help you write, update and test your code. Additionally, it provides many features such as routing and form management. This guide will discuss how to install Angular on Ubuntu24. First, you need to install Node.js. Node.js is a JavaScript running environment based on the ChromeV8 engine that allows you to run JavaScript code on the server side. To be in Ub

A friend's computer has such a fault. When opening "This PC" and the C drive file, it will prompt "Explorer.EXE Windows cannot access the specified device, path or file. You may not have the appropriate permissions to access the project." Including folders, files, This computer, Recycle Bin, etc., double-clicking will pop up such a window, and right-clicking to open it is normal. This is caused by a system update. If you also encounter this situation, the editor below will teach you how to solve it. 1. Open the registry editor Win+R and enter regedit, or right-click the start menu to run and enter regedit; 2. Locate the registry "Computer\HKEY_CLASSES_ROOT\PackagedCom\ClassInd"

MSI graphics cards are the mainstream graphics card brand on the market. We know that graphics cards need to install drivers to achieve performance and ensure compatibility. So how to update the MSI graphics card driver to the latest version? Generally, MSI graphics card drivers can be downloaded and installed from the official website. Let’s find out more below. Graphics card driver update method: 1. First, we enter the "MSI official website". 2. After entering, click the "Search" button in the upper right corner and enter your graphics card model. 3. Then find the corresponding graphics card and click on the details page. 4. Then enter the "Technical Support" option above. 5.Finally go to “Driver & Download”

Windows updates may cause some of the following problems: 1. Compatibility issues: Some applications, drivers, or hardware devices may be incompatible with new Windows updates, causing them to not work properly or crash. 2. Performance issues: Sometimes, Windows updates may cause the system to become slower or experience performance degradation. This may be due to new features or improvements requiring more resources to run. 3. System stability issues: Some users reported that after installing Windows updates, the system may experience unexpected crashes or blue screen errors. 4. Data loss: In rare cases, Windows updates may cause data loss or file corruption. This is why before making any important updates, back up your
