Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get column metadata from a table synonym

How can I determine column metadata from a table synonym in a SQL Server 2005 database? I have a synonym called 'ProjectSyn' for a table called 'Project', but I can find no column metadata for the synonym.

My guess is to somewhere determine the 'base table' for the synonym, then query for column metadata for that table. Is this a correct approach, and if not, what would be?

like image 553
ProfK Avatar asked Dec 07 '10 12:12

ProfK


People also ask

How do I find the synonym of a table in SQL?

Connect to your SQL instance, expand the database, and navigate to the Synonyms folder. Right-click on it and choose New Synonym. Enter the required details for the Synonym name, Synonym schema, Database Name, Object schema, Object Type, and name.


1 Answers

This is my solution which works with synonyms of different databases:

SELECT TOP 0 * INTO #TEMP1 FROM YourTable 
SELECT
    [column_name] = c.name,
    [data_type] = t.name,
    [character_maximum_length] = c.max_length
FROM tempdb.sys.columns c
inner join tempdb.sys.types t on t.system_type_id = c.system_type_id
WHERE [object_id] = object_id('tempdb..#TEMP1');
DROP TABLE #TEMP1
like image 59
apc Avatar answered Sep 27 '22 16:09

apc