Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

updating a subset of data 1

Status
Not open for further replies.

wbodger

Programmer
Apr 23, 2007
769
US
I am writing a stored procedure that will insert an order and then assign N serial numbers to that order id and I am wondering if you kind folk can give me any insight into which approach would be better practice (or if I am missing a third option?).

The options that I have come up with are below

Code:
CREATE PROCEDURE dbo.InsertOrder (
	@Quantity int,
	@MerchantId int,
	@ShippingFirstName varchar(30),
	@ShippingLastName varchar(30),
	@ShippingAddress1 varchar(50),
	@ShippingAddress2 varchar(50),
	@ShippingCity varchar(50),
	@ShippingState varchar(20),
	@ShippingZip varchar(12),
	@ProductId varchar(20)
	)
AS

BEGIN
	SET NOCOUNT ON;

Declare @InsertedOrderId int,
		@i int = 0;

INSERT INTO dbo.OrderTable(ShippingFirstName,ShippingLastName,ShippingAddress1,ShippingAddress2,ShippingCity,ShippingState,ShippingZip,MerchantId,Quantity,OrderDate)
Values(@ShippingFirstName,@ShippingLastName,@ShippingAddress1,@ShippingAddress2,@ShippingCity,@ShippingState,@ShippingZip,@MerchantId,@Quantity,SYSDATETIME())
SELECT @InsertedOrderId = SCOPE_IDENTITY();

While @i <= @Quantity
BEGIN
	update dbo.KeyMaster
	set MerchantId = @MerchantId, PurchaseDate = SysDateTime(), OrderId = @InsertedOrderId
	WHERE AccessKey in (select top 1 AccessKey 
	from dbo.KeyMaster
	where MerchantId IS NULL
	and ProductId = @ProductId)

	SET @i = @i+1
END
END

Or

Code:
CREATE PROCEDURE dbo.InsertOrder (
	@Quantity int,
	@MerchantId int,
	@ShippingFirstName varchar(30),
	@ShippingLastName varchar(30),
	@ShippingAddress1 varchar(50),
	@ShippingAddress2 varchar(50),
	@ShippingCity varchar(50),
	@ShippingState varchar(20),
	@ShippingZip varchar(12),
	@ProductId varchar(20)
	)
AS

BEGIN
	SET NOCOUNT ON;

Declare @InsertedOrderId int,
		@i int = 0;

INSERT INTO dbo.OrderTable(ShippingFirstName,ShippingLastName,ShippingAddress1,ShippingAddress2,ShippingCity,ShippingState,ShippingZip,MerchantId,Quantity,OrderDate)
Values(@ShippingFirstName,@ShippingLastName,@ShippingAddress1,@ShippingAddress2,@ShippingCity,@ShippingState,@ShippingZip,@MerchantId,@Quantity,SYSDATETIME())
SELECT @InsertedOrderId = SCOPE_IDENTITY();

While @i <= @Quantity
BEGIN
	; with cte as
	(select top @Quantity AccessKey 
	from dbo.KeyMaster
	where MerchantId IS NULL
	and ProductId = @ProductId
	)

	update cte
	set MerchantId = @MerchantId, PurchaseDate = SysDateTime(), OrderId = @InsertedOrderId


	SET @i = @i+1
END
END

Thanks in advance!!

Willie
 
Can you just perform an UPDATE TOP() and bypass the loop?

Code:
UPDATE TOP (@Quantity) dbo.KeyMaster
   SET MechantId = @MerchantId,
	   PurchaseDate = SYSDATETIME(),
	   OrderId = @InsertedOrderId
 WHERE MerchantId IS NULL
   AND ProductId = @ProductId
 
It balked at me when I tried that earlier. Except I didn't try quite that, I missed the parentheses around @Quantity. That works great, thanks!

wb
 
Logistically there is a problem of making sure the serial numbers actually match the product delivered.
 
No, that is not an issue for me, I have got that covered.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top