 |
|
 |
|
Next: T-SQL how to refine this query?
|
| Author |
Message |
External

Since: Apr 16, 2008 Posts: 22
|
(Msg. 1) Posted: Mon Sep 29, 2008 9:52 am
Post subject: using synonyms and aliases Archived from groups: microsoft>public>sqlserver>programming, others (more info?)
|
|
|
I'm starting to explore for the first time the use of synonyms and aliases
on SQL Server 2005, to avoid having to hard-code server names in stored
procedures that do cross-server queries and joins. Books Online is rather
terse on the subject.
Anyone know of a good online tutorial or discussion? Basically I want to be
able to define a server name in configuration rather than in code, so that
if we move the remote databases to a different server I can adjust a setting
in a single place rather than recoding the sp's.
Thanks in advance,
Tom Dacon
Dacon Software Consulting >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
|
Aaron Bertrand [SQL Serve
|
External

Since: Jan 10, 2008 Posts: 2166
|
(Msg. 2) Posted: Mon Sep 29, 2008 1:30 pm
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
I use synonyms pretty extensively. I don't know of any good tutorial
online, I learned the hard way by rolling up my sleeves and just playing
with them. In a few cases I have 500+ databases that all use functions and
procedures that are centrally located (but the code in each database doesn't
need to know that). This is more about modifying the function code in one
place as opposed to changing the name or location of the target, but both
could be achieved. (I also use it for storing central lookup tables that
need to be referenced by many databases... I would rather have a synonym
that stores a reference, than copy the table to each database, or hard-code
the other database name in all of the stored procedures, etc.) But I don't
think I need to explain all of that to you... I think you understand the
benefits already.
Basically, you can do this:
USE CentralDatabase;
GO
CREATE FUNCTION dbo.PrettyDate
(
@dt SMALLDATETIME
)
RETURNS CHAR(10)
AS
BEGIN
RETURN CONVERT(CHAR(10), @dt, 120);
END
GO
Then in any database you can say:
CREATE SYNONYM dbo.PrettyDate FOR CentralDatabase.dbo.PrettyDate;
GO
Then all of your code can call dbo.PrettyDate() without having to know
anything about CentralDatabase. You can change the name of the function or
put it in another database, and the developers will never know (as long as
you update their synonym(s)).
A couple of caveats:
You cannot ALTER a synonym. You will need to drop and re-create them (they
behave kind of like alias types in this regard).
You cannot use sp_helptext against a synonym to determine its target (you
get Msg 15197, "there is no text"), and sp_help provides nothing useful
(ooh, it's a synonym, really?). Instead you must piece together the
definitions from sys.synonyms.
If you use locking hints against objects (e.g. WITH (NOLOCK)) or set certain
isolation levels, this will break if you try to apply the hints to synonyms
that cross an instance boundary.
If you are using service broker, you need to be very aware of permissions
issues when your activation procedure "jumps" to another database or server
via a synonym. You can go through a mess of server signed certificates, or
do what I did, and just resign to the fact that for service broker you
should have all of your code local to that database. This actually bit me
this morning and I am still investigating the possible solutions (e.g.
granting EXEC to public on the target object). I am not sure when I will
have time to re-visit that one, but just wanted to mention it...
On 9/29/08 12:52 PM, in article OcMOOPlIJHA.1160.DeleteThis@TK2MSFTNGP04.phx.gbl, "Tom
Dacon" wrote:
> I'm starting to explore for the first time the use of synonyms and aliases
> on SQL Server 2005, to avoid having to hard-code server names in stored
> procedures that do cross-server queries and joins. Books Online is rather
> terse on the subject.
>
> Anyone know of a good online tutorial or discussion? Basically I want to be
> able to define a server name in configuration rather than in code, so that
> if we move the remote databases to a different server I can adjust a setting
> in a single place rather than recoding the sp's.
>
> Thanks in advance,
> Tom Dacon
> Dacon Software Consulting
>
> >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
External

Since: Apr 16, 2008 Posts: 22
|
(Msg. 3) Posted: Mon Sep 29, 2008 1:30 pm
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Thank you, Aaron. This is an interesting approach that'll take some thinking
about. We run a bunch of near-identical e-commerce databases, each with an
identical set of reference tables and stored procedures. They vary only in
the contents of certain business-specific tables and of course the product
and customer and order tables. The idea of centralizing the common objects
in a central or control database is very attractive. I'll talk it around a
bit here and see what people think.
Tom D.
"Aaron Bertrand [SQL Server MVP]" wrote in message
>I use synonyms pretty extensively. I don't know of any good tutorial
> online, I learned the hard way by rolling up my sleeves and just playing
> with them. In a few cases I have 500+ databases that all use functions
> and
> procedures that are centrally located (but the code in each database
> doesn't
> need to know that). This is more about modifying the function code in one
> place as opposed to changing the name or location of the target, but both
> could be achieved. (I also use it for storing central lookup tables that
> need to be referenced by many databases... I would rather have a synonym
> that stores a reference, than copy the table to each database, or
> hard-code
> the other database name in all of the stored procedures, etc.) But I
> don't
> think I need to explain all of that to you... I think you understand the
> benefits already.
>
> Basically, you can do this:
>
> USE CentralDatabase;
> GO
> CREATE FUNCTION dbo.PrettyDate
> (
> @dt SMALLDATETIME
> )
> RETURNS CHAR(10)
> AS
> BEGIN
> RETURN CONVERT(CHAR(10), @dt, 120);
> END
> GO
>
> Then in any database you can say:
>
> CREATE SYNONYM dbo.PrettyDate FOR CentralDatabase.dbo.PrettyDate;
> GO
>
> Then all of your code can call dbo.PrettyDate() without having to know
> anything about CentralDatabase. You can change the name of the function
> or
> put it in another database, and the developers will never know (as long as
> you update their synonym(s)).
>
> A couple of caveats:
>
> You cannot ALTER a synonym. You will need to drop and re-create them
> (they
> behave kind of like alias types in this regard).
>
> You cannot use sp_helptext against a synonym to determine its target (you
> get Msg 15197, "there is no text"), and sp_help provides nothing useful
> (ooh, it's a synonym, really?). Instead you must piece together the
> definitions from sys.synonyms.
>
> If you use locking hints against objects (e.g. WITH (NOLOCK)) or set
> certain
> isolation levels, this will break if you try to apply the hints to
> synonyms
> that cross an instance boundary.
>
> If you are using service broker, you need to be very aware of permissions
> issues when your activation procedure "jumps" to another database or
> server
> via a synonym. You can go through a mess of server signed certificates,
> or
> do what I did, and just resign to the fact that for service broker you
> should have all of your code local to that database. This actually bit me
> this morning and I am still investigating the possible solutions (e.g.
> granting EXEC to public on the target object). I am not sure when I will
> have time to re-visit that one, but just wanted to mention it...
>
>
>
>
>
>
>
> On 9/29/08 12:52 PM, in article OcMOOPlIJHA.1160.RemoveThis@TK2MSFTNGP04.phx.gbl,
> "Tom
> Dacon" wrote:
>
>> I'm starting to explore for the first time the use of synonyms and
>> aliases
>> on SQL Server 2005, to avoid having to hard-code server names in stored
>> procedures that do cross-server queries and joins. Books Online is rather
>> terse on the subject.
>>
>> Anyone know of a good online tutorial or discussion? Basically I want to
>> be
>> able to define a server name in configuration rather than in code, so
>> that
>> if we move the remote databases to a different server I can adjust a
>> setting
>> in a single place rather than recoding the sp's.
>>
>> Thanks in advance,
>> Tom Dacon
>> Dacon Software Consulting
>>
>>
> >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
External

Since: Feb 05, 2008 Posts: 62
|
(Msg. 4) Posted: Sun Apr 25, 2010 3:57 am
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
We have an app that uses around 500 tables. Over the years, the customer has
created numerous tables and views, mainly for reporting, in amongst the
existing data. What I want to do is move the official database tables out to
another database and then leave in their place synonyms, so that whatever
customer created content thats left behind will still work.
My question is how seemless would this be. Would I get any problems for
instance with views bound to tables that are now synonyms etc
"Aaron Bertrand [SQL Server MVP]" wrote:
> I use synonyms pretty extensively. I don't know of any good tutorial
> online, I learned the hard way by rolling up my sleeves and just playing
> with them. In a few cases I have 500+ databases that all use functions and
> procedures that are centrally located (but the code in each database doesn't
> need to know that). This is more about modifying the function code in one
> place as opposed to changing the name or location of the target, but both
> could be achieved. (I also use it for storing central lookup tables that
> need to be referenced by many databases... I would rather have a synonym
> that stores a reference, than copy the table to each database, or hard-code
> the other database name in all of the stored procedures, etc.) But I don't
> think I need to explain all of that to you... I think you understand the
> benefits already.
>
> Basically, you can do this:
>
> USE CentralDatabase;
> GO
> CREATE FUNCTION dbo.PrettyDate
> (
> @dt SMALLDATETIME
> )
> RETURNS CHAR(10)
> AS
> BEGIN
> RETURN CONVERT(CHAR(10), @dt, 120);
> END
> GO
>
> Then in any database you can say:
>
> CREATE SYNONYM dbo.PrettyDate FOR CentralDatabase.dbo.PrettyDate;
> GO
>
> Then all of your code can call dbo.PrettyDate() without having to know
> anything about CentralDatabase. You can change the name of the function or
> put it in another database, and the developers will never know (as long as
> you update their synonym(s)).
>
> A couple of caveats:
>
> You cannot ALTER a synonym. You will need to drop and re-create them (they
> behave kind of like alias types in this regard).
>
> You cannot use sp_helptext against a synonym to determine its target (you
> get Msg 15197, "there is no text"), and sp_help provides nothing useful
> (ooh, it's a synonym, really?). Instead you must piece together the
> definitions from sys.synonyms.
>
> If you use locking hints against objects (e.g. WITH (NOLOCK)) or set certain
> isolation levels, this will break if you try to apply the hints to synonyms
> that cross an instance boundary.
>
> If you are using service broker, you need to be very aware of permissions
> issues when your activation procedure "jumps" to another database or server
> via a synonym. You can go through a mess of server signed certificates, or
> do what I did, and just resign to the fact that for service broker you
> should have all of your code local to that database. This actually bit me
> this morning and I am still investigating the possible solutions (e.g.
> granting EXEC to public on the target object). I am not sure when I will
> have time to re-visit that one, but just wanted to mention it...
>
>
>
>
>
>
>
> On 9/29/08 12:52 PM, in article OcMOOPlIJHA.1160 RemoveThis @TK2MSFTNGP04.phx.gbl, "Tom
> Dacon" wrote:
>
> > I'm starting to explore for the first time the use of synonyms and aliases
> > on SQL Server 2005, to avoid having to hard-code server names in stored
> > procedures that do cross-server queries and joins. Books Online is rather
> > terse on the subject.
> >
> > Anyone know of a good online tutorial or discussion? Basically I want to be
> > able to define a server name in configuration rather than in code, so that
> > if we move the remote databases to a different server I can adjust a setting
> > in a single place rather than recoding the sp's.
> >
> > Thanks in advance,
> > Tom Dacon
> > Dacon Software Consulting
> >
> >
>
> >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 2061
|
(Msg. 5) Posted: Sun Apr 25, 2010 2:25 pm
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
Mark (Mark@discussions.microsoft.com) writes:
> We have an app that uses around 500 tables. Over the years, the customer
> has created numerous tables and views, mainly for reporting, in amongst
> the existing data. What I want to do is move the official database
> tables out to another database and then leave in their place synonyms,
> so that whatever customer created content thats left behind will still
> work.
>
> My question is how seemless would this be. Would I get any problems for
> instance with views bound to tables that are now synonyms etc
As long as there are not any GUI that queries for metadata, it should not
be an issue. If there are GUIs that query for metadata to permit the user to
build queries, it depends on how the GUI runs the queries, but there is
certainly a good oppurtunity for things to break.
--
Erland Sommarskog, SQL Server MVP, esquel RemoveThis @sommarskog.se
Links for SQL Server Books Online:
SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
SQL 2000: http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
External

Since: Apr 26, 2010 Posts: 1
|
(Msg. 6) Posted: Mon Apr 26, 2010 6:25 am
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
The SQL Query Builder in Management Studio works, but you can't drag a
synonym from Object Explorer onto a Database Diagram!
Do you think it would be better to use views in place of synonyms, should
help the GUI issue.
"Erland Sommarskog" wrote in message
> Mark (Mark@discussions.microsoft.com) writes:
>> We have an app that uses around 500 tables. Over the years, the customer
>> has created numerous tables and views, mainly for reporting, in amongst
>> the existing data. What I want to do is move the official database
>> tables out to another database and then leave in their place synonyms,
>> so that whatever customer created content thats left behind will still
>> work.
>>
>> My question is how seemless would this be. Would I get any problems for
>> instance with views bound to tables that are now synonyms etc
>
> As long as there are not any GUI that queries for metadata, it should not
> be an issue. If there are GUIs that query for metadata to permit the user
> to
> build queries, it depends on how the GUI runs the queries, but there is
> certainly a good oppurtunity for things to break.
>
> --
> Erland Sommarskog, SQL Server MVP, esquel.TakeThisOut@sommarskog.se
>
> Links for SQL Server Books Online:
> SQL 2008: http://msdn.microsoft.com/en-us/sqlserver/cc514207.aspx
> SQL 2005: http://msdn.microsoft.com/en-us/sqlserver/bb895970.aspx
> SQL 2000:
> http://www.microsoft.com/sql/prodinfo/previousversions/books.mspx
>
>
> __________ Information from ESET NOD32 Antivirus, version of virus
> signature database 5061 (20100426) __________
>
> The message was checked by ESET NOD32 Antivirus.
>
> http://www.eset.com
>
>
>
__________ Information from ESET NOD32 Antivirus, version of virus signature database 5061 (20100426) __________
The message was checked by ESET NOD32 Antivirus.
http://www.eset.com >> Stay informed about: using synonyms and aliases |
|
| Back to top |
|
 |  |
External

Since: May 30, 2004 Posts: 2061
|
(Msg. 7) Posted: Mon Apr 26, 2010 7:25 am
Post subject: Re: using synonyms and aliases [Login to view extended thread Info.] Archived from groups: per prev. post (more info?)
|
|
|
|
|
| Back to top |
|
 |  |
| Related Topics: | Synonyms - We have a number of stored procs/functions in a support databse which looks to a production database for the data. Rather than do this in each procedure: SELECT col1 FROM ProdServer.dbo.MyTable We were hoping to create a synonym, say SourceDB, for..
Table aliases in queries - Hi, Is there any performance impact on using aliases in the select statements? I always use alias in my applications (like a habbit), but someone told me that it may slow down the query execution. Is that right? for e.g., I always queries like..
sys.synonyms: how to determine target database & object na.. - I have synonyms in the database: CREATE SYNONYM MySynonym FOR MyDatabase.dbo.MyTable There is also the view sys.synonyms with a structure like sys.objects. How can I determine the target database and table name? Are there any other views (like..
Getting Server Aliases Information via SQL Query - I wrote this Query to quickly get all the server aliases and where they are point, however I can not seem to get it to work in SQL 2005. Any ideas, Is there another, better way? -TIA- -Matt- [code] /****************************************************...
need help with query - I want to sum the records in a table (u) which have 'P.M.' in a field value and test against a fixed limit something to the effect of : case when SUM(substring(u.sTime,1,4)='P.M.') <= 64 Then .... except it should work ... |
|
You can post new topics in this forum You can reply to topics in this forum You can edit your posts in this forum You can delete your posts in this forum You can vote in polls in this forum
|
|
|
|
 |
|
|