Thursday, November 04, 2004

SP to get DataType Length in SQL

How do I get the Data Type length for a column in SQL table, might be a simple question, the answer is also pretty simple.

The sp_gettypestring stored procedure returns the type string for the given table id and column id.

Syntax
sp_gettypestring tabid, colid, typestring
where
tabid - is the table id. tabid is int.
colid - is the column id. colid is int.
typestring - is the type string.
It's output parameter. typestring is nvarchar(255).

This is the example to get the type string for the column number 2 in the authors table, from the pubs database.

USE pubs
GO
declare @tabid int, @typestring nvarchar(255)
select @tabid = object_id('authors')
EXEC sp_gettypestring @tabid, 2, @typestring output
select @typestring
GO

Here is the result set from my machine:
-------------------------------
varchar(40)

Link for further reading - http://www.mssqlcity.com/Articles/Undoc/SQL2000UndocSP.htm

No comments: