DEV Community

Peter + AI
Peter + AI

Posted on

๐Ÿ” Mastering the Uniface lookup Statement: A Developer's Guide to Database Record Checking

As a developer working with Uniface 10.4, understanding the lookup statement is crucial for efficient database operations. This powerful statement allows you to check if records exist without actually retrieving data - making it a performance-friendly option for validation scenarios. ๐Ÿš€

Note: This article is based on the official Uniface Documentation 10.4, and I had assistance from AI in structuring this content.

๐Ÿ“‹ What Does lookup Do?

The lookup statement counts the number of occurrences that match the current profile in your database. It's like asking "How many records match this criteria?" without actually loading the data into memory.

๐ŸŽฏ Key Features

  • No Data Transport - Only returns a count, saving bandwidth and memory
  • No Read Trigger Activation - Bypasses trigger logic for pure checking
  • Primary Key Matching - Uses primary key values when available
  • Universal Compatibility - Works in all component types

๐Ÿ“Š Understanding Return Values

The lookup statement returns its result in $status:

Value Meaning
โ‰ฅ0 โœ… Number of matching records found
-1 ๐Ÿ”š End of file encountered (no record found)
-2 ๐Ÿ“ญ Table is empty
-3 โš ๏ธ I/O error (hardware/software issue)
-4 โŒ Table/file open failed
-15 ๐ŸŒ Uniface network error
-16 ๐Ÿ”Œ Unknown network error

๐Ÿ’ก Practical Example: Invoice Number Validation

Here's a real-world example showing how to prevent duplicate invoice numbers:

trigger loseFocus
; field : INVOICE_NUMBER
; Check if another record already has this number
; Prevent user from leaving field if duplicate exists

lookup
if ($status > 0)
    message "โš ๏ธ No! This number has been used before!"
    return (-1)
else
    message "โŒ I/O error %%$status"
endif
end; loseFocus
Enter fullscreen mode Exit fullscreen mode

๐Ÿ† Best Practices & Pro Tips

๐Ÿงน Use clear for Total Counts

Want to count all occurrences in a table? Use clear before lookup:

clear
lookup
; $status now contains total number of records
Enter fullscreen mode Exit fullscreen mode

โšก Performance Consideration

While lookup is efficient, sometimes a retrieve/o operation might be faster, especially since it also checks data already loaded in the component.

๐ŸŽฏ Key Matching Logic

  • If complete primary key is available โ†’ uses primary key values
  • If primary key incomplete โ†’ uses all field values for matching

๐Ÿšจ Error Handling

Always check $procerror for detailed error information:

  • -2 through -12: Database I/O errors (UIOSERR_*)
  • -16 through -30: Network I/O errors (UNETERR_*)

๐ŸŽฏ When to Use lookup

Perfect for:

  • โœ… Validation routines
  • โœ… Checking record existence before operations
  • โœ… Counting records without loading data
  • โœ… Performance-critical duplicate checks

Consider alternatives when:

  • ๐Ÿ”„ You need the actual data anyway
  • ๐Ÿ”„ Component already has relevant data loaded

๐ŸŽ‰ Conclusion

The lookup statement is a powerful tool in the Uniface developer's toolkit. It provides an efficient way to check record existence without the overhead of data retrieval, making it perfect for validation scenarios and existence checks. Remember to always handle the various return codes properly to create robust applications! ๐Ÿ’ช

Have you used lookup in interesting ways in your Uniface applications? Share your experiences in the comments below! ๐Ÿ‘‡

Top comments (0)