Progress4GL:能否通过变量/参数值命名临时表字段?
Dynamic Temp-Table Field Naming in Progress4GL
Hey Joseph, great question! You're right that the static DEFINE TEMP-TABLE syntax won't let you use a variable's value for a field name directly—Progress resolves those definitions at compile time, so runtime variables can't be referenced there. But luckily, Progress4GL supports dynamic temp-table creation using handles, which lets you define fields with runtime-generated names exactly like you want.
Step-by-Step Working Solution
Here's a practical example that achieves your goal:
DEFINE VARIABLE randomvariable AS CHARACTER INITIAL "fieldname" NO-UNDO. DEFINE VARIABLE hTempTable AS HANDLE NO-UNDO. DEFINE VARIABLE hRecord AS HANDLE NO-UNDO. /* 1. Create a dynamic temp-table handle */ CREATE TEMP-TABLE hTempTable. /* 2. Add your field using the variable's value as the field name */ /* Syntax: ADD-FIELD(field-name, data-type, format, label, extent) */ hTempTable:ADD-FIELD(randomvariable, "CHARACTER", "", "", ?). /* Optional: Set extra field properties (e.g., 20-character format, custom label) */ /* hTempTable:ADD-FIELD(randomvariable, "CHARACTER", "20", "My Dynamic Field", ?) */ /* 3. Prepare the temp-table to make it usable like a static temp-table */ hTempTable:TEMP-TABLE-PREPARE("tt_table"). /* 4. Interact with the temp-table just like you would a static one */ CREATE hRecord. hRecord:BUFFER-FIELD(randomvariable):VALUE = "Hello from a dynamic field!". /* Test the value to confirm it works */ DISPLAY hRecord:BUFFER-FIELD(randomvariable):VALUE. /* Clean up handles to avoid memory leaks in long-running sessions */ DELETE OBJECT hRecord. DELETE OBJECT hTempTable.
Key Details to Keep in Mind
- Dynamic vs Static: Static
DEFINE TEMP-TABLEis processed at compile time, so it can't use runtime variables. Dynamic temp-tables use handles to build the structure on the fly, which gives you the flexibility to define fields with dynamic names. - Handle Management: Always delete handles when you're finished with them—this prevents memory leaks that can cause issues in persistent sessions.
- Compile-Time Alternative: If your field name is known before runtime, you can use a preprocessor macro instead (but this won't work for dynamic runtime values):
&GLOBAL-DEFINE FIELD_NAME "fieldname" DEFINE TEMP-TABLE tt_table field {&FIELD_NAME} AS CHARACTER.
内容的提问来源于stack exchange,提问作者Joseph Betts
相关产品推荐
相关产品推荐

