MSSQL cross-database Queries

Forum related to version 6.5.1 (alpha) and 6.6.x (beta) of ZeosLib's DBOs

Moderators: gto, cipto_kh, EgonHugeist

Post Reply
Petya
Fresh Boarder
Fresh Boarder
Posts: 21
Joined: 16.08.2005, 16:07

MSSQL cross-database Queries

Post by Petya »

Hi again folks,

Today I wrote a query that references another database - other than the one in TZConnection.Database

Code: Select all

  SELECT Rendszam,FszgA,FszgB,CI
  FROM Online FULL OUTER JOIN BUSDATADKV.dbo.Jarmu
  ON (BUSDATADKV.dbo.Jarmu.FSZGA=Online.CI) OR (BUSDATADKV.dbo.Jarmu.FSZGB=Online.CI)
  WHERE CI=999 OR BUSDATADKV.dbo.Jarmu.FSZGA=999 OR BUSDATADKV.dbo.Jarmu.FSZGB=999 ORDER BY Rendszam ;
If I execute it from a dinamically created TZQuery component, I get the following error :

Code: Select all

General SQL Server error: Check messages from the SQL Server.  
The database name component of the object qualifier must be the name of the current database. 
There's a commonly used manager program for MS SQL Server 2000. It's called EMS SQL Manager for SQL Server. If I copy-paste the query into this program, it runs fine, gives expected results. This (commercial) program is probably using Zeos also, because it has the same 'busy' cursor than Zeos displays if you set TZConnection.SQLHourGlass to true.
Therfore it's very likely that it's possible to do this with Zeos.

To resolve the problem, I've tried to empty the database property from ZConnection and put exact database references before each table name in the query, but it gave me the same error.

I was also searching in the MSSQL manual, but found nothing about connections and databases, but this is the expected case from Microsoft Corp.

So can anybody tell me please, how can I access two MSSQL databases from one ZQuery on one ZConnection ?

Thank You.

Cheers,
Peter
Petya
Fresh Boarder
Fresh Boarder
Posts: 21
Joined: 16.08.2005, 16:07

Post by Petya »

With other words :

Can I access two (or more) databases with a single ZConnection component ? How ?
bangfauzan
Senior Boarder
Senior Boarder
Posts: 50
Joined: 31.08.2006, 10:41
Contact:

Post by bangfauzan »

Hi, Petya.

of course you can use cross-query, and also cross-server and cross-host. but you have to remove "dbo" clause in your sql statement.

so, try to change your sql above to be:

SELECT Rendszam,FszgA,FszgB,CI
FROM Online FULL OUTER JOIN BUSDATADKV..Jarmu
ON (BUSDATADKV..Jarmu.FSZGA=Online.CI) OR (BUSDATADKV..Jarmu.FSZGB=Online.CI)
WHERE CI=999 OR BUSDATADKV..Jarmu.FSZGA=999 OR BUSDATADKV..Jarmu.FSZGB=999 ORDER BY Rendszam ;

Note:
if you want to make cross-DBMS query or cross-host query using MSSQL, you can use OpenRowSet() function.

Regards.
bangfauzan
Senior Boarder
Senior Boarder
Posts: 50
Joined: 31.08.2006, 10:41
Contact:

Post by bangfauzan »

attention:
do not remove ".." between database_name and table_name

just remove the "dbo" and leave the ".."

example:

select * from db_name..tbl_name
Petya
Fresh Boarder
Fresh Boarder
Posts: 21
Joined: 16.08.2005, 16:07

Success

Post by Petya »

Problem solved, thank you !

:-))
Post Reply