When using user-defined data types (created with CREATE DOMAIN), the usual APIs seem to desribe the according columns with their base type. For example: The SQL Anywhere Demo database has a table called Contacts with a column Surname specified as person_name_t, and the latter is defined as (without specifying nullability or a default):
Now, when selecting from Contacts, ISQL displays that column as char(20), and so does
The same seems to apply when using APIs like ODBC, maybe due to the fact that a data type mapping is only defined for base types and not for user-defined types. So, how can I get the user-defined type of a particular column of a resultset? (Well, obviously I can query the system catalog, but I'm interested in a general approach working with any SELECT statement.) If possible, it should work with ASA 8, too. asked 17 Jun '10, 07:04 Volker Barth |
NOTE: The following works with SA 10 and above, but not with older versions: The system procedure sa_describe_query (introduced in SA 10) returns both the base and the user-defined datatypes. So, the following call
returns
as wanted. This is what I'm trying to accomplish with ASA 8.0.3... Addition: For any kind of computed expression, even sa_describe_query does not return a user-defined type (and one might argue whether the expression still has a user-defined type or not). Examples:
returns person_name_t for the first two columns but not for the computed ones (though at least the left-expression would always fit in the domain type):
This restriction is by design, as clarified in this follow-up Q&A. answered 17 Jun '10, 07:12 Volker Barth |
From an ESQL application, you can use EXEC SQL DESCRIBE USER TYPES to get this information in version 8. Otherwise, if the query's result set is restricted to a list of columns in a table, you could use the column names and table name to join with SYS.SYSCOLUMN and SYS.SYSUSERTYPE. For:
and
use
answered 17 Jun '10, 15:59 Bruce Hay Thanks for your suggestion - unfortunately, I'm neither using ESQL nor dealing with resultsets limited to table columns but with arbitrary queries. As stated in my own answer, the fact that my queries are not limited to select columns from different tables might be not a real restriction here, as SA does not seem to treat computed expressions as user-defined types at all. Therefore I might try to use your system-catalog approach at least for those table-based expressions. |
The reasoing behind this: When exporting data (e.g. to Excel), I would like to format data based on the user-defined type. Say, I have a column with percent values declared with a domain "percent_t" as "double check (@value between 0.0 and 1.0)", and would like to format that column as a percent value. A similar case would be to distinguish monetary and ordinary numerical values to format those accordingly.