Caché SQL Reference
Data Types
|
|
Specifies the kind of data that an SQL entity (such as a column) can contain.
The following topics are described here:
-
-
Data type precedence used to select the most inclusive data type from data values having different data types
-
-
Usage in SqlCategory of standard and user-defined logical values
-
Configurable support for dates prior to December 31, 1840
-
-
-
-
-
-
-
A data type specifies the kind of value that a column can hold. You specify the data type when defining a field with
CREATE TABLE. When defining SQL expressions, you can specify the DDL data types listed in the following table (left-hand column). When you specify one of these DDL data types, it maps to the Caché data type listed in the right-hand column.
To view the current system data type mappings, go to the
Management Portal, select
[Home] > [Configuration] > [System-defined DDL Mappings].
You can also define additional user data types. To create or view the user data type mappings, go to the
Management Portal, select
[Home] > [Configuration] > [User-defined DDL Mappings].
Important:
Each of the DDL or Caché data type expressions shown above is actually one continuous string. These strings may contain space characters, but generally do not contain white space of any kind. Some white space appears in this table for readability.
Caché SQL should not be affected by an overly large MAXLEN value. When specifying a
%Library.String data type, the MAXLEN value you specify does not have to correspond closely to the actual size of the data. If the field value is "ABC", Caché only uses that much space on disk, in the global buffers, and in private process memory. Even if the field is declared with MAXLEN=1000, the private process memory does not allocate that much space for the field. Caché only allocates memory for the actual size of the field value, regardless of the declared length.
ODBC applications may be affected by an overly large MAXLEN value. ODBC applications try to make decisions about the size of a field needed based on metadata from the server, so the application may allocate more buffer space than is actually needed. For this reason, Caché supplies a system-wide default ODBC VARCHAR maximum length of 4096; this default is configurable. To determine the current setting, call
$SYSTEM.SQL.CurrentSettings(). The Caché ODBC driver takes the data from the TCP buffer and converts it into the applications buffer, so MAXLEN size does not affect our ODBC client.
JDBC applications should not be affected by an overly large MAXLEN value. Java and .Net do not have the application allocate buffers. The clients only allocated what is needed to hold the data as a native type.
Numeric data types such as NUMERIC(6,2) have two integer values (
p,
s) precision and scope. These are mapped to Caché %Library class data types, as described in
Understanding DDL Data Type Mappings. When specified in an SQL data type, the following apply on Windows systems (maximums may differ on other systems):
-
Precision: an integer between 0 and 19+
s (inclusive). This value determines the maximum and minimum permitted value. This is, commonly, the total number of digits in the number; however, its exact value is determined by the %Library class data type mapping. The maximum integer value is 9223372036854775807. A precision larger than 19+
s defaults to 19+
s.
-
Scope: an integer that specifies the maximum number of decimal (fractional) digits permitted. Can be a positive integer, 0, or a negative integer. If
s is larger than or equal to
p, only a fractional value is permitted, the actual
p value is ignored. The largest permitted scope is 18, which corresponds to .999999999999999999. A scope larger than 18 defaults to 18.
The following example shows the maximum values for different combinations of precision and scope:
FOR i=0:1:6 {
WRITE "Max for (",i,",2)=",$$maxval^%apiSQL(i,2),!}
SQL System Data Type Mappings
The syntax shown for DDL and Caché data type expressions in the above table are the default mappings configured for the SQL.SystemDataTypes. There are separate mapping tables available for supplied system data types, and user data types.
To view and modify the current data type mappings, Go to the
Management Portal, select
[Home] > [Configuration] > [System-defined DDL Mappings].
Understanding DDL Data Type Mappings
When mapping data types from DDL to Caché, regular parameters and function parameters follow these rules:
-
Regular Parameters These are identified in the DDL data type and the Caché data type in the format %#. For example:
Hence, a DDL data type of:
-
Function Parameters These are used when a parameter in the DDL data type has to undergo some transformation before it can be put into the Caché data type. An example of this is the transformation of a DDL data type’s numeric precision and scale parameters into a Caché data type’s
MAXVAL,
MINVAL, and
SCALE parameters. For example:
%Numeric(MAXVAL=<|'$$maxval^%apiSQL(%1,%2)'|>,
MINVAL=<|'$$minval^%apiSQL(%1,%2)'|>,
SCALE=%2)
The DDL data type
DECIMAL takes parameters Precision (
%1) and Scale (
%2), but the Caché data type
%Numeric does not have a precision parameter. Therefore, to convert
DECIMAL to
%Numeric, the Precision parameter must be converted to appropriate
%Numeric parameters, in this case by applying the Caché functions
format,
maxval, and
minval to the parameters supplied by
DECIMAL. The special
<|'xxx'|> syntax (as shown above) signals the DDL processor to do parameter replacement and then call the function with the values supplied. The
<|'xxx'|> expression is then replaced with the value returned from the function call.
Considering this example with actual values, there might be a
DECIMAL data type with a precision of 4 digits and a scale of 2:
%Numeric(MAXVAL=<|'$$maxval^%apiSQL(4,2)'|>,
MINVAL=<|'$$minval^%apiSQL(4,2)'|>,
SCALE=2)
%Numeric(MAXVAL=99.99,MINVAL=-99.99,SCALE=2)
When an operation can return several different values, and these values may have different data types, Caché assigns the return value whichever data type has the highest precedence. For example, a NUMERIC data type can contain all possible INTEGER data type values, but an INTEGER data type cannot contain all possible NUMERIC data type values. Thus NUMERIC has the higher precedence (is more inclusive).
For example, if a
CASE statement has a possible result value of data type INTEGER, and a possible result value of data type NUMERIC, the actual result is always of type NUMERIC, regardless of which of these two cases are taken.
The precedence for data types is as follows, from highest (most inclusive) to lowest:
LONGVARBINARY
LONGVARCHAR
VARBINARY
VARCHAR
GUID
TIMESTAMP
DOUBLE
NUMERIC
BIGINT
INTEGER
DATE
TIME
SMALLINT
TINYINT
BIT
The following examples show the
Normalize() method for the %TimeStamp data type:
SET indate=63445
SET tsdate=##class(%Library.TimeStamp).Normalize(indate)
WRITE "%TimeStamp date: ",tsdate
SET indate="2014-2-2"
SET tsdate=##class(%Library.TimeStamp).Normalize(indate)
WRITE "%TimeStamp date: ",tsdate
The following examples show the
IsValid() method for the %TimeStamp data type:
SET datestr="July 4, 2014"
SET stat=##class(%Library.TimeStamp).IsValid(datestr)
IF stat=1 {WRITE datestr," is a valid %TimeStamp",! }
ELSE {WRITE datestr," is not a valid %TimeStamp",!}
SET leapdate="2004-02-29 00:00:00"
SET noleap="2005-02-29 00:00:00"
SET stat=##class(%Library.TimeStamp).IsValid(leapdate)
IF stat=1 {WRITE leapdate," is a valid %TimeStamp",! }
ELSE {WRITE leapdate," is not a valid %TimeStamp",!}
SET stat=##class(%Library.TimeStamp).IsValid(noleap)
IF stat=1 {WRITE noleap," is a valid %TimeStamp",! }
ELSE {WRITE noleap," is not a valid %TimeStamp",!}
Date, Time, and TimeStamp Data Types
You can define date, time, and timestamp data types, and interconvert dates and timestamps through standard Caché SQL date and time functions. For example, you can use
CURRENT_DATE or
CURRENT_TIMESTAMP as input to a field defined with that data type, or use
DATEADD,
DATEDIFF,
DATENAME, or
DATEPART to manipulate date values stored with this data type.
-
%Library.Date classes, and any user-defined data type class that has a logical value of +$HOROLOG (the date portion of $HOROLOG) should use DATE as the SqlCategory.
-
%Library.Time classes, and any user-defined data type class that has a logical value of $PIECE($HOROLOG,,,2) (the time portion of $HOROLOG) should use TIME as the SqlCategory. TIME supports fractional seconds, so this data type can also be used for HH:MI:SS.FF to a user-specified number of digits of precision. To support fractional seconds set the Precision parameter: a value of 0 rounds to the nearest second; a value of "" (the default) retains whatever precision is specified in the data value.
A field using the %Time datatype reports precision and scale to xDBC as follows: If a
precision argument is not specified, or specified as 0 or "", xDBC sets precision=8 and scale=0; fractional seconds are truncated. If a
precision argument is specified, xDBC sets precision=8+
precision and scale=
precision.
-
-
-
%MV.Date classes, or any user-defined data type class that has a logical date value of $HOROLOG-46385, should use MVDATE as the SqlCategory.
-
-
-
-
-
When comparing FMTIMESTAMP category values with DATE category values, Caché no longer strips the time from the FMTIMESTAMP value before comparing it to the DATE. This is identical behavior to comparing TIMESTAMP with DATE values, and comparing TIMESTAMP with MVDATE values. It is also compatible with how other SQL vendors compare timestamps and dates. This means a comparison of a FMTIMESTAMP 320110202.12 and DATE 62124 are equal when compared using the SQL equality (=) operator. Applications must convert the FMTIMESTAMP value to a DATE or FMDATE value to compare only the date portions of the values.
Dates Prior to December 31, 1840
A date is commonly represented by the DATE data type. This data type stores a date in
$HOROLOG format, as a positive integer count of days from the arbitrary starting date of
December 31, 1840.
By default, dates can only be represented by a positive integer (MINVAL=0). However, you can change the MINVAL type parameter to enable storage of dates prior to December 31, 1840. By setting MINVAL to a negative number, you can store dates prior to December 31, 1840 as negative integers. The earliest allowed MINVAL value is -672045. This corresponds to January 1 of Year 1 (CE). DATE data type cannot represent BCE (also known as BC) dates.
Note:
Be aware that these date counts do not take into account changes in date caused by the Gregorian calendar reform (enacted October 15, 1582, but not adopted in Britain and its colonies until 1752).
You can redefine the minimum date for your locale as follows:
SET oldMinDate = ##class(%SYS.NLS.Format).GetFormatItem("DATEMINIMUM")
IF oldMinDate=0 {
DO ##class(%SYS.NLS.Format).SetFormatItem("DATEMINIMUM",-672045)
SET newMinDate = ##class(%SYS.NLS.Format).GetFormatItem("DATEMINIMUM")
WRITE "Changed earliest date to ",newMinDate
}
ELSE { WRITE "Earliest date was already reset to ",oldMinDate}
The above example sets the MINVAL for your locale to the earliest permitted date (1/1/01).
Note:
Caché does not support using
Julian dates with negative logical DATE values (%Library.Date values with MINVAL<0). Thus, these MINVAL<0 values are not compatible with the Julian date format returned by the
TO_CHAR function.
Long string support is enabled by default. You can disable or enable long string support for a Caché instance. If disabled, you can enable long strings using either the
Management Portal or the ObjectScript
EnableLongStrings property of the
Config.Miscellaneous class. In the
Management Portal select
[Home] > [Configuration] > [Memory and Startup]. To enable support for long strings system-wide, select the
Enable Long Strings check box. Then click the Save button. After long strings are enabled, any future invoked process on that system will support long strings. For further details, refer to
Long Strings in the
Data Types and Values chapter of
Using Caché ObjectScript.
With long strings enabled, you can assign a
%Library.String data types a MAXLEN of greater than 16,374 Unicode characters (or 32K 8-bit characters). How such long strings are handled depends on your xDBC protocol:
-
Protocol 46: When ODBC or JDBC accesses
%Library.String data with a MAXLEN greater than 16,374 characters, only the first 16,374 characters are returned. If you need to support data in a single field that is longer than 16,374 characters, you should use a stream data type.
-
Protocol 47: No ODBC or JDBC string length limit.
The protocol that is used is the highest protocol supported by both the Caché instance and the ODBC driver facilities. If all facilities on both host and client support Protocol 47, that protocol is used. If any one facility only supports Protocol 46 that protocol is used, regardless of Protocol 47 support in other facilities. The protocol that was actually used is recorded in the Caché ODBC log.
Caché supports the list structure data type %List (data type class %Library.List). This is a compressed binary format, which does not map to a corresponding native data type for Caché SQL. In its internal representation it corresponds to data type VARBINARY with a default MAXLEN of 32749.
For this reason,
Dynamic SQL cannot use %List data in a
WHERE clause comparison. You also cannot use
INSERT or
UPDATE to set a property value of type %List.
Dynamic SQL returns the data type of list structured data as VARCHAR.
If you use an ODBC or JDBC client, %List data is projected to VARCHAR string data, using LogicalToOdbc conversion. A list is projected as a string with its elements delimited by commas. Data of this type can be used in a
WHERE clause, and in
INSERT and
UPDATE statements. Note that, by default, Caché establishes a system-wide ODBC VARCHAR maximum length of 4096; this
ODBC maximum length is configurable.
A field with a SERIAL data type can take a user-specified positive integer value, or Caché can assign it a sequential positive integer value.
An
INSERT operation specifies one of the following values for a SERIAL field:
-
No value, 0 (zero), or a nonnumeric value: Caché ignores the specified value, and instead increments this field's current serial counter value by 1, and inserts the resulting integer into the field.
-
A positive integer value: Caché inserts the user-specified value into the field, and changes the serial counter value for this field to this integer value.
Thus a SERIAL field contains a series incremental integer values. These values are not necessarily continuous or unique. For example, the following is a valid series of values for a SERIAL field: 1, 2, 3, 17, 18, 25, 25, 26, 27. Sequential integers are either Caché-generated or user-supplied; nonsequential integers are user-supplied. If you wish SERIAL field values to be unique, you must apply a UNIQUE constraint on the field.
An
UPDATE operation can only change a serial field value if the field currently has no value (NULL), or its value is 0. Otherwise, an SQLCODE -105 error is generated.
Caché imposes no restriction on the number of SERIAL fields in a table.
The ROWVERSION data type defines a read-only field that contains a unique system-assigned positive integer, beginning with 1. Caché assigns sequential integers as part of each insert, update, or %Save operation. These values are not user-modifiable.
Caché maintains a single row version counter namespace-wide. All tables in a namespace that contain a ROWVERSION field share the same row version counter. Thus, the ROWVERSION field provides row-level version control, allowing you to determine the order in which changes were made to rows in one or more tables in a namespace.
You can only specify one field of ROWVERSION data type per table.
The ROWVERSION field should not be included in a unique key or primary key. The ROWVERSION field cannot be part of an IDKey index.
Both ROWVERSION and %Counter (
%Library.Counter) assign a sequential integer to a field as part of an
INSERT operation. But these two counters are significantly different and are used for different purposes:
-
The ROWVERSION counter is at the namespace level. The %Counter counter is at the table level. These two counters are completely independent of each other and independent of the RowID counter.
-
The ROWVERSION counter is incremented by insert, update, or %Save operations. The %Counter counter is only incremented by insert operations.
-
A ROWVERSION field value cannot be user-specified; the value is always supplied from the ROWVERSION counter. A %Counter field value is supplied from the table’s %Counter counter during an insert if you do not specify a value for this field. If an insert supplies a %Counter integer value, that value is inserted rather than the current counter value:
-
If an insert supplies a %Counter field value greater than the current counter value, Caché inserts that value and resets the %Counter counter to the next sequential integer.
-
If an insert supplies a %Counter field value lesser than the current counter value, Caché does not reset the %Counter counter.
-
An insert can supply a %Counter field value as a negative integer or a fractional number. Caché truncates a fractional number to its integer component. If the supplied %Counter field value is 0 (or truncates to 0), Caché inserts the current counter value.
You cannot update an existing %Counter field value.
-
A ROWVERSION value is always unique. Because you can insert a user-specified %Counter value, you must specify a UNIQUE field constraint to guarantee unique %Counter values.
-
The ROWVERSION counter cannot be reset. A
TRUNCATE TABLE resets the %Counter counter; performing a
DELETE on all rows does not reset the %Counter counter.
-
Only one ROWVERSION field is allowed per table. You can specify multiple %Counter fields in a table.
DDL Data Types Exposed by Caché ODBC / JDBC
Caché ODBC exposes a subset of the DDL data types, and maps other data types to this subset of data types. These mappings are not reversible. For example, the statement
CREATE TABLE mytable (f1 BINARY) creates a Caché class that is projected to ODBC as
mytable (f1 VARBINARY). A Caché list data type is projected to ODBC as a VARCHAR string.
ODBC exposes the following data types: BIGINT, BIT, DATE, DOUBLE, GUID, INTEGER, LONGVARBINARY, LONGVARCHAR, NUMERIC, OREF, SMALLINT, TIME, TIMESTAMP, TINYINT, VARBINARY, VARCHAR. Note that, by default, Caché establishes a system-wide ODBC VARCHAR maximum length of 4096; this
ODBC maximum length is configurable.
When one of these ODBC/JDBC data type values is mapped to Caché SQL, the following operations occur: DOUBLE data is cast using
$DOUBLE. NUMERIC data is cast using
$DECIMAL.
The GUID data type corresponds to Caché SQL UNIQUEIDENTIFIER data type. Failing to specify a valid value to a GUID / UNIQUEIDENTIFIER field generates a #7212 General Error.
Query Metadata Returns Data Type
You can use Dynamic SQL to return metadata about a query, including the data type of a specified column in the query.
The following Dynamic SQL examples return the column name and the integer code for the ODBC data type for each of the columns in Sample.Person and Sample.Employee:
SET myquery="SELECT * FROM Sample.Person"
SET rset = ##class(%SQL.Statement).%New()
SET tStatus = rset.%Prepare(myquery)
SET x=rset.%Metadata.columns.Count()
WHILE x>0 {
SET column=rset.%Metadata.columns.GetAt(x)
WRITE !,x," ",column.colName," ",column.ODBCType
SET x=x-1 }
WRITE !,"end of columns"
SET myquery="SELECT * FROM Sample.Employee"
SET rset = ##class(%SQL.Statement).%New()
SET tStatus = rset.%Prepare(myquery)
SET x=rset.%Metadata.columns.Count()
WHILE x>0 {
SET column=rset.%Metadata.columns.GetAt(x)
WRITE !,x," ",column.colName," ",column.ODBCType
SET x=x-1 }
WRITE !,"end of columns"
List structured data, such as the FavoriteColors column in Sample.Person, returns a data type of 12 (VARCHAR) because ODBC represents a Caché %List data type value as a string of comma-separated values.
Steams data, such as the Notes and Picture columns in Sample.Employee, return the data types -1 (LONGVARCHAR) or -4 (LONGVARBINARY).
A ROWVERSION field returns data type -5 because %Library.RowVersion is a subclass of %Library.BigInt.
Integer Codes for Data Types
In query metadata and other contexts, the defined data type for a column may be returned as an integer code. There are two sets of integer codes used to represent data types:
-
-
xDBC data type codes (SQLType) are used by ODBC and JDBC. They are returned by
%SQL.Statement.%Metadata.columns.GetAt() method, as shown in the example above. The JDBC codes are the same as the ODBC codes, except in the representation of time and date data types. These ODBC and JDBC values are listed below:
Caché also supports Unicode SQL types for ODBC applications working with multibyte character sets, such as in Chinese, Hebrew, Japanese, or Korean locales.
You can modify the set of data types either by overriding the data type mapping for a system data type parameter value, or by defining a new user data type. You can modify system data types to override the InterSystems default mappings. You can create user-defined data types to provide additional data type mappings that InterSystems does not supply.
The result will be an entry in the list of user-defined DDL data types.
As shown in previous examples, there are several useful routines for entering user-defined DDL data types:
-
maxval^%apiSQL() Given a precision and scale, returns the maximum valid value (MAXVAL) for each of the Caché numeric data types. The syntax is:
maxval^%apiSQL(precision,scale)
where both precision and scale are required.
-
minval^%apiSQL() Given a precision and scale, returns the minimum valid value (MINVAL) for each of the Caché numeric data types. The syntax is:
minval^%apiSQL(precision,scale)
where both precision and scale are required.
Pass-through if No DDL Mapping is Found
If DDL encounters a data type not in the DDL data type column of the
UserDataTypes table, it next examines the
SystemDataTypes table. If no mapping appears for the data type in either table, no conversion of the data type occurs, and the data type passes directly to the class definition as specified in DDL.
For example, the following field definitions could appears in a DDL statement:
CREATE TABLE TestTable (
Field1 %String,
Field2 %String(MAXLEN=45)
)
To convert data from one data type to another, use the
CAST or
CONVERT function.
CAST supports conversion to several character string and numeric data types, as well as to DATE, TIME, and TIMESTAMP data types.
CONVERT has two syntactical forms. Both forms support conversion to and from DATE, TIME, and TIMESTAMP data types, as well as conversion between other data types.
CAST and CONVERT Handling of VARCHAR
The VARCHAR data type (with no specified size) is mapped to a MAXLEN of 1 character, as shown in the above table. However, when you
CAST or
CONVERT a value to VARCHAR, the default size mapping is 30 characters. This default size of 30 characters is provided for compatibility with non-Caché software requirements.