Quantcast
Channel: Transact-SQL forum
Viewing all articles
Browse latest Browse all 23857

Updating SQL Server within a Fetch loop

$
0
0

The application provides sales personnel with the ability to create and save shopping carts for their customer accounts, until such time the customer is ready to place the order. The unit price of the line items in the shopping cart must be recalculated each time the shopping cart is retrieved because there i) could be some days elapsed since the time the shopping cart was created and saved, and ii) the part prices might have changed during the elapsed time.

The SQL Server 2008 database is remotely located, so I'm trying to avoid application logic that would i) SELECT shopping cart items from the server and send to the client application, ii) the client application would iterate through the results set and for each returned shopping cart entry iii) call the remote server with a price recalculation request, and iv) get the revised price back so that the UI can display the updated price.

Instead, I'd like SQL Server to do all the work and send back the shopping cart with updated prices. I'm a bit of a newbie in SQL Server, and so I'm thinking to do an update of the shopping cart line items in a FETCH loop as follows:

DECLARE
	@LineItemId INT
	,@CustomerId INT
	,@SKUMasterId INT
	,@QtyRequest INT
	,@UnitPrice MONEY
	,@UnitPriceOverride MONEY
	,@SpecialNote NVARCHAR(4000)
	,@LastUpdate DATETIME;

-- Select shopping cart entries to update
DECLARE ShoppingCart_Cursor CURSOR FOR
	SELECT
		dbo.ShoppingCart.LineItemId
		,dbo.ShoppingCart.CustomerId
		,dbo.ShoppingCart.SKUMasterId
		,dbo.ShoppingCart.QtyRequest
		,dbo.ShoppingCart.UnitPrice
		,dbo.ShoppingCart.UnitPriceOverride
		,dbo.ShoppingCart.SpecialNote
		,dbo.ShoppingCart.LastUpdate
	FROM
		dbo.ShoppingCart
	WHERE
		(dbo.ShoppingCart.CustomerId = -2147483222)
	ORDER BY
		dbo.ShoppingCart.LineItemId ASC;

BEGIN TRAN ReviseCart;

-- Iterate through the selected cart items to update unit price
OPEN ShoppingCart_Cursor;
FETCH NEXT
	FROM
		ShoppingCart_Cursor
	INTO
		@LineItemId
		,@CustomerId
		,@SKUMasterId
		,@QtyRequest
		,@UnitPrice
		,@UnitPriceOverride
		,@SpecialNote
		,@LastUpdate;

WHILE @@FETCH_STATUS = 0
BEGIN
	UPDATE dbo.ShoppingCart
	SET
		dbo.ShoppingCart.SpecialNote = 'an example of updating in Fetch loop'
	WHERE
		(dbo.ShoppingCart.LineItemId = @LineItemId);
	
	FETCH NEXT
	FROM
		ShoppingCart_Cursor
	INTO
		@LineItemId
		,@CustomerId
		,@SKUMasterId
		,@QtyRequest
		,@UnitPrice
		,@UnitPriceOverride
		,@SpecialNote
		,@LastUpdate;
END;
CLOSE ShoppingCart_Cursor;
DEALLOCATE ShoppingCart_Cursor;

COMMIT TRAN ReviseCart;

-- Select updated shopping cart items for processing by the SqlDataReader 
SELECT
	dbo.ShoppingCart.LineItemId
	,dbo.ShoppingCart.CustomerId
	,dbo.ShoppingCart.SKUMasterId
	,dbo.ShoppingCart.QtyRequest
	,dbo.ShoppingCart.UnitPrice
	,dbo.ShoppingCart.UnitPriceOverride
	,dbo.ShoppingCart.SpecialNote
	,dbo.ShoppingCart.LastUpdate
FROM
	dbo.ShoppingCart
WHERE
	(dbo.ShoppingCart.CustomerId = -2147483222)
ORDER BY
	dbo.ShoppingCart.LineItemId ASC;

In reality, it's the unit price that would be updated; not the special note. That was just temporary code to test if the concept would work. The unit price would be recalculated by calling a stored procedure that would do its own various SELECT statements to obtain part pricing based on quantity ordered, get customer profile information to see if special prices apply, etc.

This works, but I have the feeling that I'm doing something bizarre or dangerous by starting a cursor, executing stored procedures and updating the database within the FETCH loop. I realize that I need to obtain the @@ERROR property after the UPDATE so that I can determine if the COMMIT or ROLLBACK should be performed. But for the SQL Server guru's out there, is this the right approach to the problem?



Randy


Viewing all articles
Browse latest Browse all 23857

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>