SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


SQL definition statement for synonym ?


SQL definition statement for synonym ?

Author
Message
s_v_joshi
s_v_joshi
SSC Journeyman
SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)SSC Journeyman (91 reputation)

Group: General Forum Members
Points: 91 Visits: 27
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?
Lowell
Lowell
SSC Guru
SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)SSC Guru (183K reputation)

Group: General Forum Members
Points: 183087 Visits: 41569
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
--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!
anthony.green
anthony.green
SSC Guru
SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)SSC Guru (62K reputation)

Group: General Forum Members
Points: 62800 Visits: 8598
Also syscomments is depreciated, use sys.sql_modules instead



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
Managing Transaction Logs - Gail Shaw
Troubleshooting SQL Server: A Guide for the Accidental DBA - Jonathan Kehayias and Ted Krueger


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum








































































































































































SQLServerCentral


Search