Is there a quick way to get a string containing the sql datatype required for a column declaration, based on column/column information in the database.
For example I’d want strings returned such as:
varchar(200)
datetime
numeric(35,5)
Obviously I can get the information required to generate this myself from SYS.COLUMNS or from INFORMATION_SCHEMA.COLUMNS and start handling those, but I wanted a (preferably MS provided) way to turn the datatype/maxlength/precision/scale information into the column declaration data type automatically.
I would guess that if there is a standard way to do this it would handle all possible data types, which would be a pain to try to cover manually.
EDIT: Sorry – It seems I’ve not been clear enough on what I want.
As an example, when you script a table for CREATE in SSMS, the resulting script contains the datatypes in the formats I want. Is there a way to get these automatically?
EDIT:
OK: One more go:
What I want is a bit like this question:
Declaring variable type based on a column type
The difference is that I don’t mind having to declare the variable dynamically as I’m working with dynamic SQL already.
I’m afraid I don’t know of a proper way, but I’ve had a go at doing the
INFORMATION_SCHEMA.Columnsway. This is not pretty, but does handle database defaults, nullables, etc.