OpenXML for Multiple Trips to the Database

Suppose you need to make multiple inserts (or updates or deletes) to a SQL Server database?for instance, inserting items in a shopping cart. In the past, you needed to loop through your code and make multiple calls to the database. This can be expensive, especially if you’re talking about hundreds of records.

However, a new feature in SQL Server 2000 allows you to pass an XML document as a parameter to a stored procedure where it is treated as a table.

Paste the following code into the query analyzer and run it against the pubs database:

 DECLARE	@sXML varchar(1000)	SET @sXML = ''DECLARE @hDoc int	EXEC sp_xml_preparedocument @hDoc OUTPUT, @sXML		INSERT INTO Stores (stor_id, stor_name)      		SELECT *     		 FROM OPENXML(@hDoc, '/xml/store')             			WITH			(			my_var_storeid char(4) '@store_id',			my_var_storename varchar(40) '@store_name'			)		XMLStore --alias the new xml table     	EXEC sp_xml_removedocument @hDoc

In the real world, you wouldn’t do just 1 record because there is some over head involved in transforming the XML. However, OpenXML is a good solution when multiple calls to the database would be needed.

Share the Post:
Share on facebook
Share on twitter
Share on linkedin


The Latest

iOS app development

The Future of iOS App Development: Trends to Watch

When it launched in 2008, the Apple App Store only had 500 apps available. By the first quarter of 2022, the store had about 2.18 million iOS-exclusive apps. Average monthly app releases for the platform reached 34,000 in the first half of 2022, indicating rapid growth in iOS app development.

microsoft careers

Top Careers at Microsoft

Microsoft has gained its position as one of the top companies in the world, and Microsoft careers are flourishing. This multinational company is efficiently developing popular software and computers with other consumer electronics. It is a dream come true for so many people to acquire a high paid, high-prestige job

your company's audio

4 Areas of Your Company Where Your Audio Really Matters

Your company probably relies on audio more than you realize. Whether you’re creating a spoken text message to a colleague or giving a speech, you want your audio to shine. Otherwise, you could cause avoidable friction points and potentially hurt your brand reputation. For example, let’s say you create a