Is it possible to have not only a LinkedServer, but linked database and server? My situation is that of having one environment with a setup of:
ServerX: DatabaseOne, DatabaseTwo
and another environment of:
ServerY: MyDatabaseOne, MyDatabaseTwo
Now, DatabaseOne and MyDatabaseOne are exactly the same, as are DatabaseTwo and MyDatabaseTwo. I have a stored procedure that does some basic table updates from one db to the other on a given server, and I want it to be able to work in both evironments. I have set up a linked server so my reference can be:
MyLinkedServer.DatabaseOne.dbo.MyTable or MyLinkedServer.MyDatabaseOne.dbo.MyTable
Even with this though, I will need to change the SP when I switch environments to change the db names, is there any way of setting up a Database Alias, so that I can write:
SELECT * FROM MyLinkedServer.MyLinkedDatabase.dbo.MyTable
So that as long as LinkedServer and (if possible) LinkedDatabase are set up in each environment, the SP will work without changing when switching environments?
In SQL Server Management Studio open Linked Servers and then 'New Linked Server'. Inside of appeared wizard – Select the General tab. Specify alias name in "Linked server" field.
SQL Server alias gives you the flexibility to choose a familiar name for the SQL Server instance. You do not require remembering the instance details such as instance name, port number, and protocol. You can register alias in DNS to use it from every client machine.
SQL aliases are used to give a table, or a column in a table, a temporary name. Aliases are often used to make column names more readable. An alias only exists for the duration of that query. An alias is created with the AS keyword.
A DB alias is a user-defined object that is associated with a database. When you define a DB alias, you provide parameters that are used to communicate with the associated database. These parameters include the type and version of the database management system (DBMS) and any required connection specifications.
You can avoid dynamic sql by Aliasing the server by following these steps (credits to this post)
1) Step 1:
2) Step 2:
3) Step 3:
4) Step 4:
You can take it one step further and skip specifying the database name when querying a table you can create synonyms for each object like this:
CREATE SYNONYM [dbo].[DimProduct]
FOR
[AdventureWorksDW].[dbo].[DimProduct]
this will create DimProduct Synonyms in your database. As far as I know, you can't create a synonym to a database.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With