Click here to Skip to main content
15,900,431 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i have some problem when i am creating a view in SQL Server 2005.
problem is that i have Two Database. in one database two table and anther have on table. both database is in server and i am creating view in my local server..

is it possible to create view on local server database.?
Posted

1 solution

Yes, you can create a view that looks at data in another database. e.g.

You have database1 and database2. database1 contains table1 and database2 contains tables2

By using 3 part naming syntax to qualify your database objects, we can create a view in database1 that looks like the following

The syntax here is [database].[owner].[object]

SELECT 
    Field1, Field2, Field3
FROM
    database1.dbo.table1 T1
INNER JOIN
    database2.dbo.table2 T2
WHERE
    T1.SomeField = T2.SomeField


You might have security issues with anyone consuming this view. They would need permissions to both the databases and objects.

EDIT: sorry, you updated you answer while I wrote mine!

You can use a 4 part naming query[^] for this.

SQL
SELECT 
    Field1, Field2, Field3
FROM
    localhost.database1.dbo.table1 T1
INNER JOIN
    REMOTESERVER.database2.dbo.table2 T2
WHERE
    T1.SomeField = T2.SomeField


However, you'll definitely run into security issues with this and more than likely some performance issues

Maybe investigate using OPENQUERY[^] to grab the data you need from your REMOTESERVER and perform aggregation locally.

Totally depends on how much data you're working with
 
Share this answer
 
v3
Comments
ChiragBMakwana 29-Mar-11 8:18am    
but all Databases are in server and i am working in Local Server...
problem is that i need some field from the live database..

i am writing one query for create view...

Create view Viewname
WITH ENCRYPTION
Select field1,field2 from databasename1.bdo.Tablename1
Union All
Select field1,field2 from databasename1.bdo.Tablename2
Union All
Select field1,field2 from databasename2.dbo.Tablename1(it's another database table)

is this write query or not?????
Dylan Morley 29-Mar-11 8:23am    
No, you need to use '4 part query' - look where I updated my answer

You changed your question while I was writing my original answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900