| Author |
Topic |
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2007-05-30 : 15:31:48
|
| HiI have a table like thisCustomer , Invoice , Goods ,seq , etc 33 , 12345 , AA , 0 , xx 33 , 12345 , BB , 0 , xx 33 , 12345 , CC , 0 , xx 36 , 5566 , CC , 0 , xx 36 , 5566 , SS , 0 , xx In the Insert Section , I dont have a sequence Yetafter the Insert i would like to do an UPDATE to putsequence in Column SEQthem my new table would be Customer , Invoice , Goods ,seq , etc 33 , 12345 , AA , 1 , xx 33 , 12345 , BB , 2 , xx 33 , 12345 , CC , 3 , xx 36 , 5566 , CC , 1 , xx 36 , 5566 , SS , 2 , xx The Sequence is broken by Customer+InvoiceAny help will be apreciatedTksCLages |
|
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2007-05-30 : 18:02:05
|
| Hi, O solved by my self using this Cursordespite is not a elegant solutionIf somebody else has a better solution , i would like to knowC. Lages-- =============================================-- Declare and using an UPDATE cursor-- =============================================DECLARE @cgc Numeric (14,0) , @numero Int , @produto Char(14) , @cgc1 Numeric (14,0) , @numero1 Int , @produto1 Char(14) DECLARE cursor_name CURSOR FOR SELECT tmp54_cgc , tmp54_numero, tmp54_codigoproduto FROM teste.dbo.dectmp54x Order BY TMP54_CGC, tmp54_numero ,tmp54_codigoproduto--FOR UPDATE of tmp54_sequenciaDECLARE @count smallintSELECT @count = 0OPEN cursor_nameFETCH NEXT FROM cursor_name INTO @cgc1 , @numero1 , @produto1 SET @cgc = @cgc1 set @numero = @numero1set @produto = @Produto1WHILE (@@fetch_status = 0)BEGIN If @cgc = @cgc1 and @numero = @numero1 BEGIN SELECT @count = @count + 1 UPDATE teste.dbo.dectmp54x SET tmp54_sequencia = @count WHERE @cgc = tmp54_cgc and @numero = tmp54_numero and @Produto = tmp54_codigoproduto FETCH NEXT FROM cursor_name INTO @cgc , @numero , @produto END else BEGIN SELECT @count = 1 UPDATE teste.dbo.dectmp54x SET tmp54_sequencia = @count WHERE @cgc = tmp54_cgc and @numero = tmp54_numero and @Produto = tmp54_codigoproduto SET @cgc1 = @cgc set @numero1 = @numero set @produto1 = @produto FETCH NEXT FROM cursor_name INTO @cgc , @numero , @produto END ENDCLOSE cursor_nameDEALLOCATE cursor_nameGO |
 |
|
|
Lamprey
Master Smack Fu Yak Hacker
4614 Posts |
Posted - 2007-05-30 : 19:05:41
|
You could try something like this:DECLARE @T TABLE (Customer INT, Invoice INT, Goods VARCHAR(2), seq INT)INSERT @TSELECT 33 , 12345 , 'AA' , 0UNION SELECT 33 , 12345 , 'BB' , 0 UNION SELECT 33 , 12345 , 'CC' , 0UNION SELECT 36 , 5566 , 'CC' , 0UNION SELECT 36 , 5566 , 'SS' , 0SELECT *FROM @TUPDATE tSET Seq = ( SELECT COUNT(*) FROM @t AS a WHERE a.Customer = t.Customer AND a.Invoice = t.Invoice AND a.Goods <= t.Goods )FROM @t tSELECT *FROM @T -Ryan |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-05-31 : 01:05:45
|
| Clages1, where do you want to show the data?If you use Reports, you can easily group it by customer and put report number and reset it by each groupMadhivananFailing to plan is Planning to fail |
 |
|
|
Clages1
Yak Posting Veteran
69 Posts |
Posted - 2007-05-31 : 15:26:54
|
| Tks RyanYour solutions works fine for meC.LagesRio de Janeiro |
 |
|
|
madhivanan
Premature Yak Congratulator
22864 Posts |
Posted - 2007-06-01 : 00:39:10
|
quote: Originally posted by Clages1 Tks RyanYour solutions works fine for meC.LagesRio de Janeiro
So, you will run that update statement whenever new data are added?MadhivananFailing to plan is Planning to fail |
 |
|
|
|
|
|