Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Here's the scenario.

After page loaded user is getting List of his for example Phones. He can modify, delete, add some records without databse interaction during this process. After this he can click "Save" button to populate all changes to database.

My problem is:

At the end of user's acting i have:

List<userphones> dbPhones //phones from database before user's interaction

List<userphones> userPhones //modified in any way list of phones.

First kinda obvious solution is to clear database entries and add all new items from userPhones. Thats not thing im looking for. I figured out some other ways like storing deleted Id's or Union, Merge, Except Linq utils but its still not efficient enough. I guess its pretty common issue so hoping for some help from more experienced programmers.

Thanks in advance.
Posted

1 solution

My technique is to pass in all the ids to the stored procedure. You can do this with XML.

Using the XML data type, you can DELETE the ids in dbPhones that are NOT IN the ids for userPhones, UPDATE the matching records and INSERT the new ones in one procedure call.

Here is a SQL Server 2005 XML Example. Hope this helps. You can cut and paste it into a query window and it will work (for SQL 2005+)

--Assume this is your data table. In real life it would be a SQL server table
DECLARE @DBData TABLE ( DataName varchar(50), DataDesc varchar(50))
INSERT INTO @DBData
VALUES ('One', 'Number One')
INSERT INTO @DBData
VALUES ('Two', 'Number Two')
INSERT INTO @DBData
VALUES ('Three', 'Number Three')
INSERT INTO @DBData
VALUES ('Four', 'Numero Quatro')

SELECT * FROM @DBData

-- This is the data you would pass in.
-- We would expect that One will update, Two, Three and Four
-- will be deleted (since they are not in the data) and
-- Five will be added.

DECLARE @XMLData xml
SELECT @XMLData = '
<root>
	<item>
		<DataName>One</DataName>
		<DataDesc>Number 1 Renamed</DataDesc>
	</item>
	<item>
		<DataName>Five</DataName>
		<DataDesc>Adding Five</DataDesc>
	</item>
</root>'

--Update DataDesc where the items match
UPDATE DBTable
SET DataDesc = CONVERT(varchar(50), XTable.Item.query('./DataDesc/text()'))
FROM @XMLData.nodes('/root/item') AS XTable(Item)
INNER JOIN
	@DBData DBTable ON CONVERT(varchar(50), XTable.Item.query('./DataName/text()')) = DBTable.DataName

SELECT * FROM @DBData

--Insert items that are not in the table yet.
INSERT INTO @DBData(DataName, DataDesc)
SELECT	CONVERT(varchar(50), XTable.Item.query('./DataName/text()')),
		CONVERT(varchar(50), XTable.Item.query('./DataDesc/text()'))
FROM @XMLData.nodes('/root/item') AS XTable(Item)
WHERE CONVERT(varchar(50), XTable.Item.query('./DataName/text()')) NOT IN (SELECT DataName FROM @DBData)

SELECT * FROM @DBData

--Remove items that are not in the XML document/table

DELETE FROM @DBData
WHERE DataName NOT IN (
	SELECT	CONVERT(varchar(50), XTable.Item.query('./DataName/text()'))
	FROM @XMLData.nodes('/root/item') AS XTable(Item))
SELECT * FROM @DBData


Further reading: at MSDN[^]
 
Share this answer
 
v5
Comments
helianthus87 31-Aug-10 17:15pm    
I forgot to mention that user is not operating on entities but other objects mapped from/to entietes at the begining and end editing process so any model update stuff wont work. I'm using SQL Server 2008 and Linq to entities model. All i know about stored procedures is that something like this exists so it can be pretty difficult for me but nothing i couldnt handle.
Gordon Kushner 1-Sep-10 9:01am    
I'm sorry but I haven't had any experience working with Linq so I don't know if it will work for you. However, I've updated my reply with some code for the Stored Procedure. Hope it helps.
helianthus87 6-Sep-10 7:55am    
I solved it other (ugly) way but deadline is comming so hadn't much time to dig in. Thanks alot for this example ill try to apply this when product came back from client after tests with alot efficiency complains xD Im also going to start learning bit more about SQL for better understanding stuff im working with.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900