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 |
|
scelamko
Constraint Violating Yak Guru
309 Posts |
Posted - 2006-04-19 : 10:15:16
|
| GuysI have following data INUM IGRP____________3A 5100003A 5100003010 5100013010 510001You would notice that INUM column has varchar values, so I am trying to create populate a column IGRP (INT)with corresponding numeric values which I generate based on the following logicCREATE PROCEDURE [ntest97nos] ASDECLARE @totalRecords INTDECLARE @ctr INT, @key INT, @subID varchar(20), @subid_prev varchar(20)SET @ctr = 1SELECT @key = 510000SELECT @totalRecords = MAX([ID]) FROM [tablea]WHILE @ctr <= @totalRecords BEGIN SELECT @subID = INUM FROM [tablea] WHERE [ID] = @ctr ORDER BY INUM IF @subID <> @subid_prev SET @key = @key + 1 ---- Updates the Table UPDATE [tablea] SET igrp = @key WHERE [ID] = @ctr --increment the counter SET @ctr = @ctr + 1 SET @SUBID_PREV = @SUBID ENDGOafter I run the stored procedure count from the below 2 queries are not matching, which you expect to matchSELECT DISTINCT INUM FROM TABLEASELECT DISTINCT IGRP FROM TABLEAAm I missing a trick hereAny suggestions/inputs as to why SP is not running correctlyThanks |
|
|
RyanRandall
Master Smack Fu Yak Hacker
1074 Posts |
Posted - 2006-04-19 : 10:37:22
|
Hi scelamko,It's much easier for us to understand what you need if you give an example (with data structure, sample data, and the desired results), rather than a solution which doesn't quite work.If I understand what you need, then this trick might help...  --datadeclare @t table (id int identity(1, 1), INUM varchar(10), IGRP varchar(10))insert @t (INUM) select '3A'union all select '3A'union all select '3010'union all select '3010'union all select '1'union all select '2'union all select '2'union all select '2'union all select '3'--calculationdeclare @previousINUM varchar(10)declare @i intset @i = 510000update @t set @i = case when INUM = @previousINUM then @i else @i + 1 end, IGRP = @i, @previousINUM = INUM--resultsselect * from @t Ryan Randallwww.monsoonmalabar.com London-based IT consultancy Solutions are easy. Understanding the problem, now, that's the hard part. |
 |
|
|
|
|
|
|
|