Reputation: 195
I use JDBC to update a SQL Server table in java. I update a nvarchar column. The thing is I don't know how long this column is. If it has only 30 characters and I update it with 35 characters then it will fail.
Is there a way to find out how long a text column is before I update it?
Thanks
Upvotes: 0
Views: 3309
Reputation: 20320
You can get the size using the sp_columns stored proc
e.g
exec sp_columns @table_name = 'MyTable', @column_name = 'MyColumn'
There's a few others including querying the information_schema as well.
Upvotes: 2
Reputation: 1330
use getPrecision(int)
method of ResultSetMetaData
class.
ResultSet rs;
// more code
ResultSetMetaData rsmd = rs.getMetaData();
int size=rsmd.getPrecision(COLUMN_INDEX);
http://docs.oracle.com/javase/7/docs/api/java/sql/ResultSetMetaData.html#getPrecision(int)
Upvotes: 2