I want to lookup the default value of a column in my SQL2005 database. I only have the SQL server connection string, so I can only use the Sql library namespace (can’t use the Oledb GetSchema stuff). I know there is a GetSchema()… but how do I use it to get a column schema, and thus the default value? I can’t fillSchema on a datatable, because that doesn’t appear to fill the default value property. I can use “exec sp_columns ‘ADDRESS_MASTER'” and that returns default values, but they are not in a clean format… for example, one of my text column default comes back as “(N’test)”.
I also want to get column size… sp_columns seems to give me everything… but I don’t know how to decode the default values in all cases?
Your problem here is the default value is an expression and it seems like you want it to just return a value such as ‘bob’ or 5.
This pretty much falls over the moment you have GetDate() or call a custom function such as dbo.MyFunc(). At this point what should it return as the default value?
As such the only reliable thing the sql server can do is return to you the expression it will use to generate the default value, you could run this through the sql server again to determine the result, ie:
Of course, if the expression isn’t constant such as in the case of GetDate this will be incorrect by the time you come to insert actual data. Also it would cause problems if using a custom function with side-effects, but a custom function with side-effects used in a default I’d consider a problem already.