How to create an alias of database in SQL Server
Create a database with the name you want to impersonate. Re-jigg the DDL code generator to create a view for every table in the database that has the tables I need to access via the hardcoded name. Basically, each view will have a statement that looks like this..
CREATE VIEW schemaname.tablename as SELECT * FROM targetdbname.schemaname.tablename
Example:
The target database name that is hardcoded is called ProdDBV1
and the Source DB you have is named ProductDatabaseDatabaseV1
, schema is dbo
and table name is customer
- Create the database called
ProdDBV1
using SSMS or script. CREATE VIEW dbo.customer as SELECT * FROM ProductDatabaseDatabaseV1.dbo.customer
If you can enumerate each table in your "source" database and then create the DDL as above. If you want I can update this posting with a code example. (using the sp_msforeachtable
procedure if possible)
Can I alias a database name or create a synonym for a database?
Looks like this is not currently a possibility with SQL Server
Alias/Synonym for a database based on user
Is it possible to have the same query, but based on different user currently logged in, request the info from different databases?
Sure. Logins have a default database and users have a default schema.
Both of these can affect object name resolution.
if you use a 1-part name, like
SELECT * FROM clients
then SQL Server will look for an object named "clients" in the user's default schema first, then in the dbo schema in the current database.
If you use a 2-part name, like
SELECT * FROM foo.clients
then SQL Server will look for an object named "clients" in in the "foo" schema in the current database.
Related Topics
How to Create Xml Schema from an Existing Database in SQL Server 2008
Oracle Trigger Ora-04098: Trigger Is Invalid and Failed Re-Validation
Wrong Week Number Using Datepart in SQL Server
Order by Maximum Condition Match
Postgresql Gin Index Slower Than Gist for Pg_Trgm
How to Use 'Like' Statement with Unicode Strings
Postgresql Insert If Not Exists
A Constraint That Only Allows One of Two Tables to Reference a Base Table
Complicated SQL Query--Finding Items Matching Multiple Different Foreign Keys
How SQL's Convert Function Work When Converting Datetime to Float
Convert Rows to Columns Using 'Pivot' in Mssql When Columns Are String Data Type
Java.Sql.Sqlexception: Ora-03115: Unsupported Network Datatype or Representation
Default Value of Guid in for a Column in MySQL
How to Select All Hours Between Two Dates
To Get Column Names from Table Having a Particular Value
Sqlite Get Name of Attached Databases
Postgres Syntax Error at or Near "On"
How to Return Only Work Time from Reservations in Postgresql