INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

updating a subset of data

updating a subset of data

(OP)
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

RE: updating a subset of data

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 

RE: updating a subset of data

(OP)
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

RE: updating a subset of data

Logistically there is a problem of making sure the serial numbers actually match the product delivered.

RE: updating a subset of data

(OP)
No, that is not an issue for me, I have got that covered.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close