In order to select tables in a stored procedure, view or trigger in an external database you must use the fully qualified name in the select statement.
For example, to select from a table in a separate database on the same SQL Server instance you might write:
You can also select cross-databases by adding the server name:
Until this morning the SQL Statement above had always worked for me, but this morning using a new server (running SQL2008) I received the following error message:
I’d never seen this error before and found this article (http://msdn.microsoft.com/en-us/library/aa259589(SQL.80).aspx) explaining how to use sp_addlinkedserver.
So I executed the following on the SQL Server 2008 box:
And afterward the former SQL expression “SELECT AuthorID, Author FROM Server2.BooksDb.dbo…” executed successfully.
For example, to select from a table in a separate database on the same SQL Server instance you might write:
SELECT AuthorID, Author
FROM BooksDb.dbo.PIC_Author
You can also select cross-databases by adding the server name:
SELECT AuthorID, Author
FROM Server2.BooksDb.dbo.PIC_Author
Until this morning the SQL Statement above had always worked for me, but this morning using a new server (running SQL2008) I received the following error message:
---------------------------
Microsoft SQL Server Management Studio
---------------------------
SQL Execution Error.
Executed SQL statement: SELECT AuthorID, Author FROM Server2.BooksDb.dbo.PIC_Author
Error Source: .Net SqlClient Data Provider
Error Message: Could not find server 'Server2' in sys.servers. Verify that the correct server name was specified. If necessary, execute the stored procedure sp_addlinkedserver to add the server to sys.servers.
---------------------------
OK Help
I’d never seen this error before and found this article (http://msdn.microsoft.com/en-us/library/aa259589(SQL.80).aspx) explaining how to use sp_addlinkedserver.
So I executed the following on the SQL Server 2008 box:
USE master
GO
EXEC sp_addlinkedserver
'Server2'
GO
And afterward the former SQL expression “SELECT AuthorID, Author FROM Server2.BooksDb.dbo…” executed successfully.
Comments