?
Solved

SQL 2005 Stored Procedure

Posted on 2009-07-10
3
Medium Priority
?
144 Views
Last Modified: 2012-05-07
I have a SP that Picks Sales O)rder lines and updates the stock with Qty Picked.

If the user picks more than the available stock it then goes negative. Is there an eay way to just set StockQty to zero?
If @PickType = 'PickMan' 
	Begin
		Update OrderDetail Set QtyPicked = @PickQty, Pickedby = @pUser, DatePicked = GETDATE ( )
		Where OrderNo = @OrderNo and Barcode = @Bcode
		set @iMsg = 'Picked ordered qty for ' + @Bcode
 
		-- update the stock qty
		Update Stock Set StockQty = StockQty - isnull((@PickQty),0), 
		QtyReserved = QtyReserved - isnull((@PickQty),0)
		Where Barcode = @Bcode
		goto ShowResults
	End

Open in new window

0
Comment
Question by:HKFuey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 31

Accepted Solution

by:
RiteshShah earned 2000 total points
ID: 24821454
can you do something like this?


 Update Stock Set StockQty = case when StockQty - isnull((@PickQty),0)<0 then 0 else StockQty - isnull((@PickQty),0) end ,
                QtyReserved = QtyReserved - isnull((@PickQty),0)
                Where Barcode = @Bcode
                goto ShowResults
0
 

Author Closing Comment

by:HKFuey
ID: 31601987
That works great, thanks!
0
 
LVL 31

Expert Comment

by:RiteshShah
ID: 24821601
>>Author Comments:
That works great, thanks!<<

welcome, glad to help!!!

Ritesh Shah

www.SQLHub.com
0

Featured Post

NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: When running hybrid database environments, you often need to query some data from a remote db of any type, while being connected to your MS SQL Server database. Problems start when you try to combine that with some "user input" pass…
This article explains how to reset the password of the sa account on a Microsoft SQL Server.  The steps in this article work in SQL 2005, 2008, 2008 R2, 2012, 2014 and 2016.
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question