I'm creating a procedure that allow us to make mass-updates on our database.
We need this because we are starting to implement a new field from a table that has registers since 2002.
The idea behind this procedure is to keep it around, because we are going to use this in several differents bases.
DEFINE INPUT PARAMETER cValueToModify AS CHARACTER NO-UNDO.
CREATE QUERY hSourcedbTableQuery.
hSourcedbTableQuery:SET-BUFFERS(hSourcedbTableBuffer).
hSourcedbTableQuery:QUERY-PREPARE(cSearchMode + cDatabaseTableName + cWhereConditions + "EXCLUSIVE-LOCK").
hSourcedbTableQuery:QUERY-OPEN().
txnBlk:
REPEAT TRANSACTION:
DO WHILE hSourcedbTableQuery:GET-NEXT():
iRecordsProcessed = iRecordsProcessed + 1.
hSourcedbTableBuffer:BUFFER-FIELD(cFieldToModify):BUFFER-VALUE = cValueToModify.
IF iRecordsProcessed GT 5000 THEN DO:
iRecordsProcessed = 0.
hSourcedbTableBuffer:BUFFER-RELEASE().
NEXT txnBlk.
END.
END.
hSourcedbTableBuffer:BUFFER-RELEASE().
LEAVE txnBlk.
END.
My question is: When using BUFFER-FIELD and BUFFER-VALUE, do I need to pass a value that matches the type of the field? Or I can pass a CHARACTER with the value "03/23/2023" to a field of DATE type?
If not possible, what would be the best way to make this transparent for other codes? Leaving the process entire into the .P.
I expect so. But you will need to make sure that the character values are those expected by the session date and numeric formats (for those values).
If you want to convert to the native datatype in that internal procedure, you can check the buffer field's DATA-TYPE attribute and act accordingly.
define variable hBufferField as handle no-undo.
hBufferField = hSourcedbTableBuffer:buffer-field(cFieldToModify).
case hBufferField:data-type:
/* this is a very simple example - for dates you may always get it in ISO
format (eg CCYY-MM-DD) and if your -D is DMY the DATE function will throw
an error. */
when "date" then hBufferField:buffer-value = convert-to-date(cValueToModify).
when "decimal" then hBufferField:buffer-value = convert-to-decimal(cValueToModify).
otherwise hBufferField:buffer-value = cValueToModify.
end case.