Solved

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

Posted on 2004-04-28
5
434 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
table fragmentation 40 73
MS SQL Bulk load data error 5 33
SQL Connection (Error 18456) 14 29
SQL Server creating a temp table 7 33
I wrote this interesting script that really help me find jobs or procedures when working in a huge environment. I could I have written it as a Procedure but then I would have to have it on each machine or have a link to a server-related search that …
Introduction SQL Server Integration Services can read XML files, that’s known by every BI developer.  (If you didn’t, don’t worry, I’m aiming this article at newcomers as well.) But how far can you go?  When does the XML Source component become …
This video shows how to set up a shell script to accept a positional parameter when called, pass that to a SQL script, accept the output from the statement back and then manipulate it in the Shell.
Viewers will learn how the fundamental information of how to create a table.

743 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

18 Experts available now in Live!

Get 1:1 Help Now