Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

SQL definition statement for synonym ? Expand / Collapse
Author
Message
Posted Thursday, January 24, 2013 5:23 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: General Forum Members
Last Login: Wednesday, February 13, 2013 7:18 AM
Points: 5, Visits: 25
I have created a synonym as follows
CREATE SYNONYM [dbo].[ms4] FOR [local].[Northwind].[dbo].[Products]
GO

and then i want to retrieve a same SQL definition statement from one of the system/db's view so i fired

select text from syscomments where id = object_id('dbo.[ms4]')

but 'text' from syscomments returns text for all other types other then synomym

syscomments contains entries for each view, rule, default, trigger, CHECK constraint, DEFAULT constraint, and stored procedure within the database. The text column contains the original SQL definition statements.
so from which table/view i can get original SQL definition statements for synonym. Its not there in syscomments . so where is they stored?

Post #1411076
Posted Thursday, January 24, 2013 5:29 AM


SSChampion

SSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampionSSChampion

Group: General Forum Members
Last Login: Today @ 2:46 PM
Points: 12,744, Visits: 31,065
you want to use the system view sys.synonyms, instead, which has everything you need:
select * from sys.synonyms
--rebuilding the command:
select
'CREATE SYNONYM '
+ quotename(SCHEMA_NAME(schema_id))
+'.'
+ quotename(name) --ie MyView
+ ' FOR '
+ base_object_name --ie [SandBox].[dbo].[VW_FIXEDWIDTH]
FROM sys.synonyms

--Results:
/*
CREATE SYNONYM [dbo].[MyView] FOR [SandBox].[dbo].[VW_FIXEDWIDTH]
*/



Lowell

--There is no spoon, and there's no default ORDER BY in sql server either.
Actually, Common Sense is so rare, it should be considered a Superpower. --my son
Post #1411081
Posted Thursday, January 24, 2013 5:34 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Wednesday, August 28, 2013 2:37 AM
Points: 5,075, Visits: 4,834
Also syscomments is depreciated, use sys.sql_modules instead



Want an answer fast? Try here
How to post data/code for the best help - Jeff Moden
Need a string splitter, try this - Jeff Moden
How to post performance problems - Gail Shaw
CrossTabs-Part1 & Part2 - Jeff Moden
SQL Server Backup, Integrity Check, and Index and Statistics Maintenance - Ola Hallengren
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger

Post #1411082
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse