Question

I am using JTDS JDBC Driver. Version is 1.2.4 Suppose I'm getting column metadata for particular a table:

ResultSet columnsRs = meta.getColumns(null, [pattern], [table name], null);
while(columnRs.next()){
    // I would like to know here if the current column is marked computed
}

Here is the table definition for ID computed field

CREATE TABLE [dbo].[C_Currencies](
    [CurrencyID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](150) NOT NULL,
    [ID]  AS ([CurrencyID]),
 CONSTRAINT [pk_currency] PRIMARY KEY CLUSTERED 
(
    [CurrencyID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]
Was it helpful?

Solution

According to the DatabaseMetaData.getColumns JavaDoc from Java 7, this information is returned in the column IS_GENERATEDCOLUMN with either value YES, NO or empty string (which means: unknown).

This column was added in JDBC 4.0, and as far as I know the jTDS driver only implements JDBC 3.0. If this column does not exist in the ResultSet with jTDS, then your options are either to switch to the Microsoft SQL Server JDBC driver (I assume their driver does support this column), or query the system views of SQL Server yourself using something like (for column ID in table C_Currencies):

SELECT is_computed
FROM sys.tables t
INNER JOIN sys.columns c
    ON c.object_id = t.object_id
WHERE t.name = 'C_Currencies'
    AND c.name = 'ID'
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top