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.
| Author |
Topic |
|
shebert
Yak Posting Veteran
85 Posts |
Posted - 2004-07-26 : 11:02:47
|
| Hi ALL lets say you had a table with a PK containing random number, invoice Number , price columns.and you needed to add a forth column that will increment each itme on like invoice number and start back at 1 for new invoice number.....example PK Inv# $ ?867 A005 $5.00 1986 A005 $3.00 2989 A005 $6.00 3968 B008 $4.00 1969 C009 $5.00 1do I need to create a complex cursor or is there a function like compute or something that can handle thisThanks Steve |
|
|
X002548
Not Just a Number
15586 Posts |
Posted - 2004-07-26 : 11:47:11
|
| Nope....Let me ask...how do you know which one to order to be the first, last, ect.Why is this important to you?Brett8-)EDIT: And how many rows are we discussing? |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-26 : 11:47:28
|
| [code]DECLARE @ItemNo int, @LastInvoiceNumberSELECT @LastInvoiceNumber = 0 -- Force first item to start at Item 1UPDATE MyTableSET @ItemNo = CASE WHEN @LastInvoiceNumber = MyInvoiceNumber THEN 0 ELSE @ItemNo END, @ItemNo = MyItemColumn = @ItemNo+1, @LastInvoiceNumber = MyInvoiceNumber[/code]But you would have to do something to order the records by InvoiceNo - which probably means using a temporary table, updating that with the new ItemNo information, and then JOINing that to the original table to UPDATE that.Kristen |
 |
|
|
Kristen
Test
22859 Posts |
Posted - 2004-07-26 : 11:48:56
|
quote: Originally posted by X002548 Let me ask...how do you know which one to order to be the first, last, ect.Why is this important to you?
Perhaps he just wants a decent PK on the invoice items - I would :)Kristen |
 |
|
|
shebert
Yak Posting Veteran
85 Posts |
Posted - 2004-07-26 : 12:20:52
|
| Hello I beleive that is the idea of this column to create an order where none exist......not alot of data 1000 maybee.Steve |
 |
|
|
shebert
Yak Posting Veteran
85 Posts |
Posted - 2004-07-27 : 10:58:23
|
| I was able to modify the logic to get what I needed. DECLARE @IncrNo varchar(50), @LastInvoiceNumber floatSELECT @LastInvoiceNumber = 0 -- Force first item to start at Item 1UPDATE @orderdetailSET @IncrNo = CASE WHEN @LastInvoiceNumber <> ordermastercounter THEN 1 ELSE @IncrNo+1 END, U_RegNo =u_RegNo+'/'+@IncrNo, @LastInvoiceNumber = ordermastercounter |
 |
|
|
|
|
|
|
|