Solved

record counter (key counter) in t-sql using cursor

Posted on 2004-04-28
5
447 Views
Last Modified: 2012-06-21
I have this function in access and need to move it to sql  

>> Dim d As Database
>> Dim r As Recordset
>> Dim x As Integer
>> Dim CurrKey As String, PrevKey As String
>> 
>> Set d = CurrentDb
>> 
>> Set r = d.OpenRecordset("select autonum_key, autonum_count from table1 order by autonum_key")
>> x = 1
>> Do Until r.EOF = True
>>     CurrKey = r("autonum_key") 'set the current key value
>>     If CurrKey <> PrevKey And r.AbsolutePosition <> 0 Then 'if they're the same, reset the counter
>>         x = 1
>>     End If
>>     r.Edit
>>         r("autonum_count") = x
>>     r.Update
>>     x = x + 1
>>     PrevKey = CurrKey 'save the current key for comparison
>>     r.MoveNext
>> Loop
>> r.Close
>> 

would it look like this ?

SET NOCOUNT ON
PRINT 'POPULATE CHARGE KEY COUNTER'
PRINT GETDATE()
DECLARE @X       INT
DECLARE @CurrKey VARCHAR(10)
DECLARE @PrevKey VARCHAR(10)
SET     @CurrKey = ''
SET     @PrevKey = ''
SET     @X       = 1
DECLARE CK CURSOR
READ_ONLY
FOR SELECT CHARGE_KEY FROM CHPAYADJCRDB  ORDER BY CHARGE_KEY
OPEN CK
FETCH NEXT FROM CK INTO @CurrKey
WHILE (@@fetch_status <> -1)
  BEGIN
    IF (@@fetch_status <> -2)
      BEGIN
        IF @CurrKey <> @PrevKey
          SET @X = 1
        ELSE
          SET @X = @X + 1
        UPDATE CHPAYADJCRDB
        SET CHARGE_KEY_COUNTER = @X WHERE CHARGE_KEY = @CurrKey
        SET @PrevKey = @CurrKey
        END
FETCH NEXT FROM CK INTO @CurrKey
    END
CLOSE CK
DEALLOCATE CK
GO

the result would look like this:

key         count
12345        1
12345        2
12345        3
12346        1
12346        2
12347        1
12348        1
12349        1
12349        2
123410      1

and so on ...

any help would be appreciated
0
Comment
Question by:thbrowne
  • 3
5 Comments
 
LVL 11

Expert Comment

by:rdrunner
ID: 10943835
How about this?


select autonum_key,count(*) autonum_count from table1 group by autonum_key



Its a lot shorter to type ;)
0
 
LVL 11

Expert Comment

by:rdrunner
ID: 10943898
OOOPS....

I think i missunderstood a bit....

You want to update the Autonum key field in all rows to a correct Value?


Ok i assume that chargekey is a "real id" that constantly grows...

update table1 outer
Set autonum_count = (Select count (*) +1 from table1 inner where inner.autonum_key = outer.autonumkey  and autonum_count is null)
where chargekey = (select min(chargekey) from Table1 inner2 where inner2.autonum_key = outer.autonum_key and autonum_count is null)


I think this should work... You need to run this querry several times until no rows are affected..

Hope this helps
0
 
LVL 11

Accepted Solution

by:
rdrunner earned 125 total points
ID: 10944127
Ok try 3.... Sorry... Its past midnight here :)

update table1 outer
Set autonum_count = (Select count (*) +1 from table1 inner where inner.autonum_key = outer.autonumkey  and autonum_count is null)
where chargekey IN (select min(chargekey) from Table1 group by autonum_key having autonum_count is null)

This should work...
0

Featured Post

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Question has a verified solution.

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

When you hear the word proxy, you may become apprehensive. This article will help you to understand Proxy and when it is useful. Let's talk Proxy for SQL Server. (Not in terms of Internet access.) Typically, you'll run into this type of problem w…
JSON is being used more and more, besides XML, and you surely wanted to parse the data out into SQL instead of doing it in some Javascript. The below function in SQL Server can do the job for you, returning a quick table with the parsed data.
Via a live example, show how to shrink a transaction log file down to a reasonable size.
Using examples as well as descriptions, and references to Books Online, show the documentation available for datatypes, explain the available data types and show how data can be passed into and out of variables.

911 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now