I am working with AS400 version 7.1.
Having the following:
ServerA (SA) - DatabaseA (DBA) - TableA (TA)
ServerB (SB) - DataBaseB (DBB) - TableB (TB)
SELECT A.*, B.*
FROM SA.DBA.TA A INNER JOIN SB.DBB.TB
ON A.PN=B.PN
WHERE A.PN='BFDKS';
What's the correct syntax to join 2 tables from two different servers in AS400?
I am getting the following error
Relational database SA not in relational database directory
I'm pretty sure this is not currently possible with Db2 for i...
3-part names are new to the i, and as far as I know are limited to
insert into mylib.mytable
(select * from remotedb.somelib.sometable);
see CREATE TABLE with remote subselect
Or in a trigger program..
see 3-part names in triggers
Db2 for LUW has such federation capabilities...
One work around I've seen is the use of a user defined table function (UDTF) to return rows from a remote Db2 for i database..
Not possible on DB2 for i. But like says Charles, you can do it :
-- run this instructions on server B
create table qtemp.SADBATA as (
SELECT A.* FROM SA.DBA.TA A
where A.PN='BFDKS'
) with data;
SELECT * FROM qtemp.SADBATA A INNER JOIN SB.DBB.TB ON A.PN=B.PN;
Related
So at work we've been using Oracle SQL Developer 4.1.3.20 lately to query our data so I wrote this snippet of code (its actually much longer by 8 or so more table joins):
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from xyz.DRUG_KIT k
left join xyz.DR_SHIP s on s.ID = k.SHIPMENT_ID
left join xyz.USR_PAT u on u.PAT_ID = k.PAT_ID
When I have to switch to another table in a different database, I have been copy pasting everywhere above where you see 'xyz' but surely there has to ba better way? I use to use SQL Server Management Studio 2014 but we had to switch to this once we began using oracle databases. How would I declare a varchar(?) variable in Oracle SQL Developer so I can just use that one variable in place of XYZ and at the top of the query I just can set that variable equal whatever necessary in one place if that makes sense. It becomes cumbersome when I copy paste something wrong on one line when I"m trying to join about 10+ tables together to retrieve data.
XYZ is the owner of the table, not a different database. If you are connected as user XYZ, then you don't need to prefix XYZ to the table name, it's only required if you are attempting to access objects owned by a different "SCHEMA".
You can change the current "parsing" schema of your session by issuing an alter session command:
ALTER SESSION SET CURRENT_SCHEMA=PDQ;
and then select from objects the user PDQ has granted you select privs on:
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
You can then alter your schema back to XYZ and perform the same select statement and get different results based on the contents of the tables as owned by XYZ:
ALTER SESSION SET CURRENT_SCHEMA=XYZ;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from DRUG_KIT k
left join DR_SHIP s on s.ID = k.SHIPMENT_ID
left join USR_PAT u on u.PAT_ID = k.PAT_ID;
To make the DR_SHIP table owned by PDQ accessable from the XYZ schema, a private synonym to PDQ.DR_SIP can be created in place of the table onwed by XYZ:
CREATE OR REPLACE SYNONYM DR_SHIP FOR PDQ.DR_SHIP;
On the other hand if you want the PDQ version of the DRUG_KIT table accessible from all SCHEMAs in the DB, you can create a public synonym:
CREATE OR REPLACE PUBLIC SYNONYM DRUG_KIT FOR PDQ.DRUG_KIT;
If none of the above meet your needs, you can use SQL*Plus style substitution variables:
ACCEPT user;
select
s.NAME as "Shipment ID"
,k.STATUS_ID as "Status"
,u.SCR_NO as "SPID"
from &user..DRUG_KIT k
left join &user..DR_SHIP s on s.ID = k.SHIPMENT_ID
left join &user..USR_PAT u on u.PAT_ID = k.PAT_ID;
But you need to understand that these won't necessarily work outside of SQL*Plus, SQL Developer, SQLcl, and possibly a couple of other SQL development environments.
At the database level, you probably want public synonyms. https://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_7001.htm
I have 2 SQL servers. I need a SQL query that can join 2 tables that are in two different server.
Like
SELECT *
FROM Server1.Db1.dbo.table1 A
INNER JOIN Server2.Db1.dbo.table2 B ON A.Id = B.Id
and I do not have the server names, instead I am using IP address of the servers. Do I need to enable these SQL servers as linked server to allow such cross server queries?
You can proceed with Linked Servers using sp_addlinkedserver.
Once done, you can query your data as you mentioned;
SELECT *
FROM [Db1].[dbo].table1 A
INNER JOIN [Server2].[Db1].[dbo].table2 B
ON A.Id = B.Id
Yes, add as linker server is one option. You also can join the remote table by use [ip address].dbname.dbo.table name s well.
Please consider the following:
use db1;
select * into #db1_tmp from mytable;
use db2;
select * into #db2_tmp from myothertable;
-- join temp table 1 and 2
select * from #db1_tmp a
left join db2_tmp b on where a.uid = b.uid;
This works, but SQL Server Management Studio is red-underlining #db1_tmp in the last query and therefore in all other statements that depend on this table.
Question: what is the proper way to access a temp table created in another database to prevent this underlining from happening? I tried db1.#db1_tmp but this does not work. I'm on SQL Server 2008.
Temp tables actually appear in their own database, TempDB. I think the root of your issue is the use statements. Try this instead:
select * into #db1_tmp from db1.dbo.mytable;
select * into #db2_tmp from db2.dbo.myothertable;
-- join temp table 1 and 2
select * from #db1_tmp a
left join db2_tmp b on where a.uid = b.uid;
But if this is the extent of what you're doing (creating the temp tables just to do a join across the databases), you can skip the temp tables altogether:
select * from db1.dbo.mytable a join db2.dbo.myothertable b on a.uid = b.uid.
Temp tables has no use of Database reference for creation so gets created in tempdb.
Only the source can be changed with "use" or "dbname.dbo.mytable".
The red-underlining is due to intellisense. The temp table is just identified as a normal table before execution and redlined due to database change.
Note: The select query at the last has syntax errors.
it should be,
select * from #db1_tmp a left join #db2_tmp b on a.uid = b.uid;
I am working with the code below but it is returning an error -
Incorrect syntax near the keyword 'AS'
I expect the query to create a table using the data from existing tables.
create table Allactivity as
(
select dbo.dim_client.*,dbo.dim_transaction.*
from dbo.dim_client,dbo.dim_transaction,dbo.fact_capital
where dbo.dim_client.dim_client_key=dbo.fact_capital.dim_client_key
and dbo.dim_transaction.dim_transaction_key=dbo.fact_capital.dim_transaction_key
)
It looks like you're using Microsoft SQL Server. T-SQL has a different syntax: SELECT INTO:
http://msdn.microsoft.com/en-us/library/ms190750.aspx
select dbo.dim_client.*,dbo.dim_transaction.*
into Allactivity
from dbo.dim_client,dbo.dim_transaction,dbo.fact_capital
where dbo.dim_client.dim_client_key=dbo.fact_capital.dim_client_key
and dbo.dim_transaction.dim_transaction_key=dbo.fact_capital.dim_transaction_key
If you use the select into syntax, you will need to have rights to dynamically create a table.
Another solution is to work with a DBA to create the staging table ahead of time. Then clear and load the staging table each time. Have the DBA give you or the application rights to the table. This can be db_datareader & db_datawriter which is a-lot less access than db_owner!
I did notice that you are using older syntax for joins, you should use the newer syntax for forward compatibility.
http://www.straightpathsql.com/archives/2012/11/sql-server-join-syntax-its/
--
-- DBA - Create empty new table (assumes stage schema exists)
--
select
dc.*,
dt.*
into
stage.all_activity
from
dbo.fact_capital as fc
left join
dbo.dim_client as dc
on fc.dim_client_key = dc.dim_client_key
left join
dbo.dim_transaction as dt
on fc.dim_transaction_key = dt.dim_transaction_key
where (1 = 0);
-- User/Application - clear the table
delete from stage.all_activity;
-- User/Application - add some data (+ where clause for range)
insert into
stage.all_activity
select
dc.*,
dt.*
from
dbo.fact_capital as fc
left join
dbo.dim_client as dc
on fc.dim_client_key = dc.dim_client_key
left join
dbo.dim_transaction as dt
on fc.dim_transaction_key = dt.dim_transaction_key;
I'm enhancing an existing java application. There is data in 2 different DB2 databases. The app already gets data from 2 different databases, but it always does a lookup from one and then the other. Is there a way to join data from 2 different DB2 databases using one SQL SELECT?
This is what I tried:
CREATE ALIAS remote_orders FOR remote_db.schema.orders;
select *
from myid.remote_orders a
inner join local_schema.parts b on (a.key = b.key)
with ur FETCH FIRST 200 ROWS ONLY
I get this error:
STATEMENT REFERENCE TO REMOTE OBJECT IS INVALID. SQLCODE=-512, SQLSTATE=56023, DRIVER=4.14.113
Can I do something with a temp table? I can run this select with no errors, but it does not help me... (yet)
select *
from myid.remote_orders
with ur FETCH FIRST 200 ROWS ONLY
EDIT:
A DB2 Temp Table might help. I was able to create one. Now I need to (go to bed) and try selecting into it and THEN doing my join.
Use fully qualified name <database>.<user/schema>.<tablename>
something like:
select *
from DB1.myid.remote_orders a
inner join DB2.local_schema.parts b on (a.key = b.key)
with ur FETCH FIRST 200 ROWS ONLY