Question: I have about 100 sites built in a cms, each with its own database. Each database has the same tables.

My stored procedure needs to select all of the pages in a database given a sitename.

Below I am trying to pass the database name as a parameter, but it doesn't seem to work.

```
...
@site nvarchar(250)

AS

SELECT *
FROM @site..cmsDocument
WHERE published = 1

```

Is there some other way to do this?

Answer: 
```
SELECT @dbname = quotename(dbname)
SELECT @sql = ' SELECT ... FROM ' + @dbname + '.dbo.tablename WHERE ...'
EXEC sp_executesql @sql, @params, ...

```

Refs:

[sp\_executesql (Transact-SQL)](http://msdn.microsoft.com/en-us/library/ms188001.aspx)

[The Curse and Blessings of Dynamic SQL](http://www.sommarskog.se/dynamic_sql.html)
You can specify the database in the connection string:

Data Source=myServerAddress;**Initial Catalog=myDataBase**;User Id=myUsername;Password=myPassword;