Thursday, March 29, 2012

use prefixes in MSSQL to denote databases, servers, and linked servers

Suppose you have a stored procedure on a server named Foo and you wished to get the count of a field out a table called Bar somewhere in the stored procedure. You might approach it like so:

SELECT @bar_variable = COUNT (bar_column) FROM dbo.Bar

 
 

What if you wanted to reach a different table called Bar that was in a database called Baz even though the stored procedure resided in a database called Foo? Well, you might approach it like this:

SELECT @bar_variable = COUNT (bar_column) FROM Baz.dbo.Bar

 
 

Moreover, you could do something like this:

SELECT @bar_variable = COUNT (bar_column) FROM Qux.Baz.dbo.Bar

 
 

In this example, Qux could be the name of another server, or Qux could be a "linked server" which is a variable defined at the server at hand (Foo) denoting a server named something else to correspond to the moniker of Qux.

No comments:

Post a Comment