You can try something like this, but I'm not sure if this will help ...
SELECT o.name,c.name,t.name,c.max_length,c.precision,c.is_nullable FROM sys.columns AS c INNER JOIN sys.objects AS o ON c.object_id=o.object_id INNER JOIN sys.types AS t ON c.system_type_id=t.system_type_id WHERE o.name=@YourTableName AND c.name=@YourColumnName
UPDATE Worst case: -D
No one has yet given an answer ... There seems to be no general approach ...
So think about it:
DECLARE @cmd VARCHAR(MAX); WITH AllColumns AS ( SELECT c.TABLE_NAME AS tbl ,c.COLUMN_NAME AS col ,c.DATA_TYPE AS tp FROM INFORMATION_SCHEMA.COLUMNS AS c ) SELECT @cmd=REPLACE ('CASE @tablename ' + (SELECT ' WHEN ''' + c1.tbl + ''' THEN \n' +( SELECT 'CASE @columnname \n' + ( SELECT ' WHEN ''' + c2.col + ''' THEN ''' + c2.tp + '''\n' FROM AllColumns AS c2 WHERE c2.tbl=c1.tbl FOR XML PATH('') ) + ' END' ) FROM AllColumns AS c1 GROUP BY c1.tbl FOR XML PATH('')) + ' END','\n',CHAR(13)+CHAR(10));
This will create an embedded TVF with all the types you need. Whenever you change the database structure, you run this script (first with DROP FUNCTION ), and everything is fine again.
source share