Click here to monitor SSC
SQLServerCentral is supported by Redgate
Log in  ::  Register  ::  Not logged in
Home       Members    Calendar    Who's On

Add to briefcase

SQL definition statement for synonym ? Expand / Collapse
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: Tuesday, May 20, 2014 6:09 AM
Points: 5, Visits: 27
I have created a synonym as follows
CREATE SYNONYM [dbo].[ms4] FOR [local].[Northwind].[dbo].[Products]

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



Group: General Forum Members
Last Login: Today @ 10:05 AM
Points: 14,543, Visits: 38,391
you want to use the system view sys.synonyms, instead, which has everything you need:
select * from sys.synonyms
--rebuilding the command:
+ quotename(SCHEMA_NAME(schema_id))
+ quotename(name) --ie MyView
+ ' FOR '
+ base_object_name --ie [SandBox].[dbo].[VW_FIXEDWIDTH]
FROM sys.synonyms

CREATE SYNONYM [dbo].[MyView] FOR [SandBox].[dbo].[VW_FIXEDWIDTH]


help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Post #1411081
Posted Thursday, January 24, 2013 5:34 AM



Group: General Forum Members
Last Login: Thursday, September 1, 2016 2:56 AM
Points: 5,969, Visits: 6,067
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
When a question, really isn't a question - Jeff Smith
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