How do I access SQL column default value via JDBC DatabaseMetaData in Database Toolbox v3.10 (R2011b)?

2 views (last 30 days)
I have a certain SQL table. I would like to access the metadata to determine if a particular column has a default value or not.
I use the following method.
% Define JDBC driver & URL:
>>driver = 'com.microsoft.sqlserver.jdbc.SQLServerDriver';
>>URL = 'jdbc:sqlserver://localhost:1433;database=TickerData;integratedSecurity=true;';
% Connect to database:
>>db = database('TickerInfo','','',driver,URL);
% Get MATLAB metadata object:
>>dbmeta = dmd(db)
dbmeta =
DMDHandle: [1x1 com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData]
% Access Java object within (dbmeta) & call getColumns() method.
% It's known that table 'Ticker' has a column 'isThisRecordActive' which has a default value of (1).
>> dbmeta.DMDHandle.getColumns('','','Ticker','isThisRecordActive')
ans =
SQLServerDatabaseMetaData:3

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 2 Apr 2012
Accessing default values using SQL is not possible using Database toolbox directly.
As a workaround, it is possible to use documented java.sql calls with a simple Java class file called databaseUtils.java.
It has to be compiled with
javac databaseUtils.java –Xlint:unchecked
with the –Xlint flag to get around the warning.
You will need to add the directory where databaseUtils.class lives with the JAVAADDPATH command in MATLAB, for example
javaaddpath c:\
You can access the column default values using the following syntax:
b = databaseUtils;
b.dmdColumns(db.DMDHandle,'dwmart','dbo'); % where db is the database used.

More Answers (0)

Products


Release

R2011b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!