Query Examples

Innovative does not provide end-user support for creating SQL queries. Sierra Direct SQL Access users assume responsibility for the effects of custom SQL. Poorly constructed queries can compromise system performance.

Innovative provides common SQL query examples that you can use as patterns for your own queries.

General Information for creating Sierra SQL queries:

Example queries for specific system functions:

General Information

Using functions to convert record metadata id numbers to Innovative record numbers

Most Sierra tables identify records by using the value found in the "id" field of the sierra_view.record_metadata table rather than by the Innovative record number. You can use the id2reckey function in your SQL queries to convert a record metadata id number to an Innovative record number. Likewise, you can use the reckey2id function to convert an Innovative record number to a record metadata id number.

Note that you should not include the check digit when using the reckey2id function to process a specific Innovative record number.

The id2reckey and reckey2id functions can impact the speed of query processing. Be particularly careful when using either of these functions in a WHERE clause that instructs the database to call the function repeatedly while processing a large table. Poorly constructed queries can compromise system performance.

Reporting on "varfield" Data

The sierra_view.varfield table stores variable-length fields, "varfields", from the legacy database, and includes fields from all record types. The field_content column contains the same string found in a traditional varfield, including possible subfield contents and subfield delimiters. For MARC fields, the sierra_view.subfield table contains the parsed subfield contents to aid in reporting. See the Generic Record view group in the SierraDNA for details.

Using Keyword Indexes to Facilitate Data Retrieval

Selecting records based on the value in a varfield can be processing-intensive given that the varfield field_content column is not indexed. As an alternative, join to the phrase_entry table for any varfield that has been indexed by your site. (This is how Sierra searches for records by keyword.)

Adjusting Money Formats

This functionality is available in Sierra 5.0 and later.

The existing views in SierraDNA assume a money format with two decimal places for certain currency columns (see the table below). To accomodate libraries that do not use currency containing two decimal places, Sierra offers new adjusted views and a shifting function to use with existing views. Depending on the SQL query you are constructing, you can use one or the other to correct the currency amounts in your reports.

The table below shows the currency columns on which you can use the shifting function and the corresponding views that contain that column.

Currency Columns Existing Views New Adjusted Views
order_total_amt
sierra_view.vendor_record
sierra_view.vendor_view
sierra_view.vendor_record_adjcur
sierra_view.vendor_view_adjcur
invoice_total_amt
sierra_view.vendor_record
sierra_view.vendor_view
sierra_view.vendor_record_adjcur
sierra_view.vendor_view_adjcur
estimated_received_price_amt sierra_view.vendor_record
sierra_view.vendor_view
sierra_view.vendor_record_adjcur
sierra_view.vendor_view_adjcur
estimated_cancelled_price_amt sierra_view.vendor_record
sierra_view.vendor_view
sierra_view.vendor_record_adjcur
sierra_view.vendor_view_adjcur
cost sierra_view.program_record
sierra_view.program_view
sierra_view.program_record_adjcur
sierra_view.program_view_adjcur
estimated_price sierra_view.order_record
sierra_view.order_view
sierra_view.order_record_adjcur
sierra_view.order_view_adjcur
price sierra_view.item_record
sierra_view.item_view
sierra_view.item_record_adjcur
sierra_view.item_view_adjcur
owed_amt sierra_view.patron_record
sierra_view.patron_view
sierra_view.patron_record_adjcur
sierra_view.patron_view_adjcur
debit_amt sierra_view.patron_record
sierra_view.patron_view
sierra_view.patron_record_adjcur
sierra_view.patron_view_adjcur
item_charge_amt
sierra_view.fine sierra_view.fine_adjcur
processing_fee_amt sierra_view.fine sierra_view.fine_adjcur
billing_fee_amt sierra_view.fine sierra_view.fine_adjcur
paid_amt sierra_view.fine sierra_view.fine_adjcur

Adjusted Views

Sierra offers new views that have adjusted the currency columns above based on your library's money format. These views are otherwise identical to the existing views. The adjusted views have the same names as the existing views, but are appended with "_adjcur".

If you are constructing a SQL query with a JOIN clause, Innovative recommends using the adj_moneyfmt() function on the existing views instead of the adjusted views for performance reasons.

Shifting Function

The adj_moneyfmt() function shifts the decimal places on currency amounts based on your system's money format. Innovative recommends using this function in your SQL queries under the following circumstances:

For performance reasons, Innovative recommends applying the adj_moneyfmt() function to the fields listed above after joining to another view.

Reporting on Material Type and Bib Level

Given that material type and bib level are stored in different BCODE columns for different sites, Innovative has explicitly (and redundantly) stored these values in dedicated tables to make writing reports clearer.

Reporting on User-defined Properties

"User-defined properties" are fixed-length data fields, such as PCODE1 or PCODE4, that store values defined by the library.

Reporting on User Permissions

Acquisitions Queries

Cataloging Queries

Circulation Queries