Site Sponsored By: SQLDSC - SQL Server Desired State Configuration
Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
HI, i need some help whith this Stored Procedure i need to do.TableName LoteDep Char(03)Code Char(14)Lot Char(15)ExpDate SmalldateTime Qty int Sample Table "Lote"Seq Dep Code Lot ExpDate Qty 1 001 123 AB 01/12/2012 52 001 123 AX 01/11/2013 33 001 123 BW 15/04/2014 44 001 123 TK 02/05/2015 8How can do to do this?The user type Dep, Code and Qty as Input.Option A:user type Qty = 5 I need to get row 1 = 001 123 AB 01/12/2012 5user type Qty = 2 I need to get row 1 = 001 123 AB 01/12/2012 5user type Qty = 7 I need to get row 1 = 001 123 AB 01/12/2012 5 2 = 001 123 AX 01/11/2013 3user type Qty = 9 I need to get row 1 = 001 123 AB 01/12/2012 5 2 = 001 123 AX 01/11/2013 3 3 = 001 123 BW 15/04/2014 4user type Qty = 25 (this is easy) I need a Msg error I have not , i have Only 20 How can do i Script, SP, etc to manager this situation?tksCarlos LagesRio de Janeiro
ehorn
Master Smack Fu Yak Hacker
1632 Posts
Posted - 2012-01-17 : 16:17:30
Hello Clages1,How about something like the following?
SELECT l.* FROM Lote lWHERE l.Seq <= ( SELECT MIN(Seq) Seq FROM ( SELECT Seq, Dep, Code, Lot, ExpDate, Qty, (SELECT SUM(Qty) FROM Lote d WHERE d.Dep = a.Dep and d.Code = a.Code AND d.ExpDate <= a.ExpDate) AS TTLQty FROM Lote a ) d WHERE d.TTLQty >= @Qty)
Clages1
Yak Posting Veteran
69 Posts
Posted - 2012-01-30 : 15:26:43
tks, but there is a problemthe column SEQ doesnt exist, I put in the forun just to clarify , to show what rows is to be returned attCarlos Lages