Scema Issue

  • I have a user on which i have acess over 2 databases say AdventreWork and AdventureWorkDW. Now i have to write a query in which i dont have to mention a db name but still it will give me the result.

    example :

    AdventureWork : Employee (Table Name)

    AdventureWorkDW : Department (Table Name)

    QUERY

    Select Empid, Deptname From Employee E INNER JOIN Department D ON (E.Dept_id=D.Dept_id) Will this work??-----

    Insted if i write

    Select Empid, Deptname From AdventureWork .Employee E INNER JOIN AdventureWorkDW .Department D ON (E.Dept_id=D.Dept_id)--- This is working fine.

    But our requiremnt is we dont want to mention the schema name. Is tis possible??

  • Your sample query

    QUERY

    Select Empid, Deptname From Employee E INNER JOIN Department D ON (E.Dept_id=D.Dept_id) Will this work??-----

    Insted if i write

    Select Empid, Deptname From AdventureWork .Employee E INNER JOIN AdventureWorkDW .Department D ON (E.Dept_id=D.Dept_id)--- This is working fine.

    But our requiremnt is we dont want to mention the schema name. Is tis possible??

    This is not describing a schema, AdventureWorkDW, and AdventureWork are database names.

    If you want to eliminate the database name from your query then you must be connected to that database.

    For Example

    Use [AdventureWorks]

    SELECT *

    FROM [Production].[Product]

    Works.

    If your purposly connected to AdventureWorks, and you need data from the AdventureWorkDW database then you need to fully qualify the AdventureWorksDW table in your query by using the [DatabaseName].[Schema].[Table Name] in your Join.

    Like I mentioned I'm confused by what your asking because you seem to be calling database names Schemas, and they are different. and the examples you posted do not work as you mention.

  • In the database you are working in, you can create a synonym for each table you want to access outside of the account's database you are working in.

    I don't know if this will meet your criteria, as you would specify the database, schema, and table name in the synonym, but the query could then be written using the synonyms with the accounts default database and schema.

  • Thanks for the reply. I was a bit confused about this. I will give you my exact query.

    I have created a connection through my application on SQL SERVER (sql server authentication) which is for AdventureWorks Database. This is for user1. User1 have access on AdventureWorksDW database also. The query which i have written is

    SELECT t1.Name , t2.ModelName FROM AdventureWorks .Production.Product t1 LEFT JOIN AdventureWorksDW.dbo.DimProduct t2 ON (t1.ProductID=t2.ProductKey)

    This query is work fine, this is working fine even in this case also.

    SELECT t1.Name , t2.ModelName FROM Production.Product t1 LEFT JOIN AdventureWorksDW.dbo.DimProduct t2 ON (t1.ProductID=t2.ProductKey)

    Now my question is will this query work if i write a query like

    SELECT t1.Name , t2.ModelName FROM Production.Product t1 LEFT JOIN dbo.DimProduct t2 ON (t1.ProductID=t2.ProductKey)

    Help me out with this.

Viewing 4 posts - 1 through 3 (of 3 total)

You must be logged in to reply to this topic. Login to reply