I'm working on a data scrubbing program where each field has a set of rules for masking the data. Ex. Leave the first two letters of each word alone and set alpha to 'z' and numeric to 9 unless it's a key word. Leave key words as is and then update the record.
Not being a guru on creating SELECT and UPDATE statements that work together I ended up creating a rowset with a single SELECT statement. Using SQL_Fetch(%NEXT_ROW) I looped through each record and extracted a field to mask into a string variable. I then created an sql statement to UPDATE that row using a 'WHERE' clause and the unique id. I put all the UPDATE sql statements in a string array until I had exhausted the recordset created by the SELECT statement.
I then looped through the array of prepped (UPDATE) sql statements and executed each one.
Worked fine but I was wondering if it could be done using 2 sql statement numbers against the same database as I'm fetching each one from the original SELECT statement.
At the place in the code where I put the strSQL$ for the UPDATE statement into the array element I did execute using the long syntax this statement with statement number 2. The outer loop all used statement number 1. The fetching in the outer loop lost track of where it was and only updated one record.
My question is whether you can update the record you are on while looping through a record set fetching one record at a time? Should I have created a different connection to the database for this approach instead of just a second statement in the first database connection?
Bob Mechler
Not being a guru on creating SELECT and UPDATE statements that work together I ended up creating a rowset with a single SELECT statement. Using SQL_Fetch(%NEXT_ROW) I looped through each record and extracted a field to mask into a string variable. I then created an sql statement to UPDATE that row using a 'WHERE' clause and the unique id. I put all the UPDATE sql statements in a string array until I had exhausted the recordset created by the SELECT statement.
I then looped through the array of prepped (UPDATE) sql statements and executed each one.
Worked fine but I was wondering if it could be done using 2 sql statement numbers against the same database as I'm fetching each one from the original SELECT statement.
Code:
strSQL$ = "SELECT COUNT(*) FROM DNUCLEUS" lresult& = SQL_Stmt(%SQL_STMT_IMMEDIATE,strSQL$) Fetch_Ok& = SQL_Fetch(%NEXT_ROW) Tot_Rows& = SQL_ResColSInt(1) DIM PEND_UPDATE$(SQL_ResColSInt(1)) strSQL$ = "SELECT CODE,REFERENCE FROM DNUCLEUS ORDER BY CODE" lresult& = SQL_Stmt(%SQL_STMT_IMMEDIATE,strSQL$) Fetch_Ok& = SQL_Fetch(%NEXT_ROW) CONTROL SET TEXT CBHNDL,%LBL_SQLTABLE," Creating Update Statements " WHILE Fetch_Ok& = 0 T$ = SQL_ResColStr(2) strSQL$ = "UPDATE DNUCLEUS SET REFERENCE='" + TRIM$(MASKING(T$,2)) + "' WHERE CODE = '" + SQL_ResColStr(1) +"'" 'masking function not shown INCR p_u& PEND_UPDATE$(p_u&) = strSQL$ Fetch_Ok& = SQL_Fetch(%NEXT_ROW) IF (p_u& MOD 100) = 0 THEN lPos& = lPos& + 5 IF lPos& > 100 THEN lPos& = 5 END IF ShowProgress(BYVAL CBHNDL,BYVAL %PRB_MSCTLS_PROGRESS32_1,lPos&) END IF WEND CONTROL SET TEXT CBHNDL,%LBL_SQLTABLE," Executing Update Statements " FOR xp_u& = 1 to p_u& strSQL$ = PEND_UPDATE$(xp_u&) lresult& = SQL_Stmt(%SQL_STMT_IMMEDIATE,strSQL$) IF (xp_u& MOD 100) = 0 THEN lPos& = lPos& + 5 IF lPos& > 100 THEN lPos& = 5 END IF ShowProgress(BYVAL CBHNDL,BYVAL %PRB_MSCTLS_PROGRESS32_1,lPos&) END IF NEXT ShowProgress(BYVAL CBHNDL,BYVAL %PRB_MSCTLS_PROGRESS32_1,100)
My question is whether you can update the record you are on while looping through a record set fetching one record at a time? Should I have created a different connection to the database for this approach instead of just a second statement in the first database connection?
Bob Mechler
Comment