Dec 5, 2007

Recently I had a table with no unique ID. I had to create a row then realized I already had 238 records. Instead of touching each and typing in the value (dumb). I came up with this snip of code. The table is in Oracle and the Coldfusion code uses the sudo field rowid to achieve this.

<!---Get records and rowid (oracles pecific)--->
<cfquery datasource="#request.dsn#" name="tester">
select req_incr_id, rowid
from rem_apvl_dtl
</cfquery>

<!---loop through each individual record--->
<cfloop query="tester">

<!---Start the count based on one record I manually altered ad set to 1, you could do this differently--->
<cfif tester.req_incr_id eq 1>
<cfset mark = 1>
<cfelse>
<cfset mark = mark +1>
</cfif>

<!---Now update your unique ID with the incremented number--->
<cfquery datasource="#request.dsn#" name="markit">
UPDATE rem_apvl_dtl
SET req_incr_id = '#mark#'
where rowid = '#rowid#'
</cfquery>

</cfloop>

You can see how easy this is. Now once all of your records have been updated you can go back and fix your queries or add a sequence to your table to increment the id from here on out.

If you inadvertently screw up on the data in the field you can always remove it and add it again.

Labels: , , ,

0 Comments:

Post a Comment

Links to this post:

Create a Link

<< Home