Accessing a DB on a different server from a stored proc - sql

So I've often needed to do a SELECT for instance on a table in another DB which I've been doing as follows:
SELECT MyColumn FROM MyOtherDB.dbo.MyOtherTable WHERE Conditions
Of course this will only work if the DB is on the same server. Is there a way this can be achieved if the two DBs are on different servers, let's say 192.168.0.1 and 192.168.0.2?

You should create link to that server with sp_addlinkedserver. After that you should create a synonym. Than you can use that synonym with in your query.

Related

access a synonym using linked server

Using SQL server 2016
Is it possible to access a synonym from a linked server, when the actual target table of the synonym cannot be accessed by the source server?
There are two databases in one server
say on
server1
database1
database2
server2
database3
And there is a table "table1" on database1.
And a synonym for that has been created in database2
USE [database2]
GO
CREATE SYNONYM [dbo].[synonym1_for_table1] FOR [server1].[database1].[dbo].[table1]
GO
There is a linked server access for database2 from server2
When the following query is executed from the server2
SELECT *
FROM [server1].[database2].[dbo].[synonym1_for_table1]
Below error is encountered
Cannot process the object [database2].[dbo].[synonym1_for_table1]. The
OLE DB provider "SQLNCLI" for linked server "server1" indicates that
either the object has no columns or the current user does not have
permissions on that object.
In server2 under "linked servers", under the tree
I see tables and views of database2 but not synonyms.
Is there a way to see synonyms. That could be a solution to this issue.
Update:
If the synonym is made a view, the following error is observed
The server principal "xyz" is not able to access the database "database1" under the current security context.
"xyz" cannot be created/cannot be granted access to "database1".
You should create a view to read data from table present on the other server/database.
Also, create a separate user (service account or user account) that will have access only to read data from the source table and can execute the view(present on the different server).

How to create database table when using Wildfly?

I am trying to complete a tutorial on a simple javaEE project using wildfly. The first step is creating two tables in my database. As it says I should create my tables like this: "CREATE TABLE wildfly.name...." but it gives me an error saying thet wildfly is unknown.
Link to the tutorial: click here
My question is why should i put "wildfly." before the table name and how can I solve this error?
Thank you for your help!
Note: I am using oracle database instead of mysql
It's a misleading MySQL tutorial example because in Oracle syntax "wildfly." is a user(schema) in the Oracle database.
Schema/user in Oracle is a namespace for tables and other objects. So, when you issue such a statement - you're telling oracle to create table in namespace WILDFLY. If you don't have such user in your database or you don't have rights to access such user/schema - you can't create tables there.
You should create such user in Oracle database (or alter your statement to another user/schema name that you actually have in your database) and put your tables there.
For example these statements are correct because I created WILDFLY user before putting tables to it:
CONNECT SYS/****#ORCL AS SYSDBA
CREATE USER WILDFLY IDENTIFIED BY WILDFLYPASSWORD;
GRANT UNLIMITED TABLESPACE TO WILDFLY;
CREATE TABLE WILDFLY.MYTABLE...

Synonym support on PostgreSQL

How to create and use Synonyms on PostgreSQL as in Oracle. Do I need to create some DB link or any thing else. I could not find any good official doc on this topic.
Edit 1
Actually as of now i have an application which has two separate modules which connects with two different oracle databases; One modules need to access tables of other so for which we use synonyms over db link in oracle. Now we are migrating application to postgresql, so we need synonyms.
Edit 2
When i say two different oracle databases it means it can be two different oracle instances or two schemas of same db, it is configurable in application and application must support both modes.
PostgreSQL version: 9.6.3
Approach 1:-
Finally i got it working using foreign data wrapper postgres_fdw as below
I have two databases named dba and dbb. dbb has a table users and i need to access it in dba
CREATE SERVER myserver FOREIGN DATA WRAPPER postgres_fdw OPTIONS (host 'localhost', dbname 'dbb', port '5432');
CREATE USER MAPPING FOR postgres
SERVER myserver
OPTIONS (user 'user', password 'password');
CREATE FOREIGN TABLE users (
username char(1))
SERVER myserver
OPTIONS (schema_name 'public', table_name 'users');
CREATE FOREIGN TABLE users (users char(1));
Now i can execute all select/update queries in dba.
Approach 2:-
Can be achieved by creating two schemas in same db, below are the steps:
create two schemas ex app_schema, common_schema.
Grant access:
GRANT CREATE,USAGE ON SCHEMA app_schema TO myuser;
GRANT CREATE,USAGE ON SCHEMA common_schema TO myuser;
Now set search path of user as below
alter user myuser set search_path to app_schema,common_schema;
Now tables in common_schema will be visible to myuser. For example let say we have a table user in common_schema and table app in app_schema then below queries will be running easily:
select * from user;
select * from app;
This is similar to synonyms in oracle.
Note- Above queries will work PostgreSQL 9.5.3+
I think you don't need synonyms in Postgres the way you need them in Oracle because unlike Oracle there is a clear distinction between a user and a schema in Postgres. It's not a 1:1 relationship and multiple users can easily use multiple schemas without the need to fully qualify the objects by exploiting Postgres' "search path" feature -  mydb.public.mytable.
If the tables are supposed to be in a different database in PostgreSQL as well, you'd create a foreign table using a foreign data wrapper.
If you used the Oracle synonym just to avoid having to write atable#dblink, you don't have to do anything in PostgreSQL, because foreign tables look and feel just like local tables in PostgreSQL.
If you use the synonym for some other purposes, you can either set search_path to include the schema where the target table is, or you can create a simple view that just selects everything from the target table.

SELECT data from another schema in oracle

I want to execute a query that selects data from a different schema than the one specified in the DB connection (same Oracle server, same database, different schema)
I have an python app talking to an Oracle server. It opens a connection to database (server/schema) A, and executes select queries to tables inside that database.
I've tried the following :
select ....
from pct.pi_int, pct.pi_ma, pct.pi_es
where ...
But I get:
ORA-00942: table or view does not exist
I've also tried surrounding the schema name with brackets:
from [PCT].pi_int, [PCT].pi_ma, [PCAT].pi_es
I get:
ORA-00903: invalid table name
The queries are executed using the cx_Oracle python module from inside a Django app.
Can this be done or should I make a new db connection?
Does the user that you are using to connect to the database (user A in this example) have SELECT access on the objects in the PCT schema? Assuming that A does not have this access, you would get the "table or view does not exist" error.
Most likely, you need your DBA to grant user A access to whatever tables in the PCT schema that you need. Something like
GRANT SELECT ON pct.pi_int
TO a;
Once that is done, you should be able to refer to the objects in the PCT schema using the syntax pct.pi_int as you demonstrated initially in your question. The bracket syntax approach will not work.
In addition to grants, you can try creating synonyms. It will avoid the need for specifying the table owner schema every time.
From the connecting schema:
CREATE SYNONYM pi_int FOR pct.pi_int;
Then you can query pi_int as:
SELECT * FROM pi_int;
Depending on the schema/account you are using to connect to the database, I would suspect you are missing a grant to the account you are using to connect to the database.
Connect as PCT account in the database, then grant the account you are using select access for the table.
grant select on pi_int to Account_used_to_connect

How to query table with different table owner

I have several tables with defined owner user in a SQL Server 2008 database
a_user.[table1]
a_user.[table2]
When I log in as admin I cannot query either table because it doesn't exist [in that user namespace].
How can I make admin or any other user be able to query table a_user.[table1] without referencing the user?
select * from a_user.table1 --> works
select * from table1 --> doesn't work, i'd like this to work
What you're looking at are schemas, not owners. The two concepts are distinct (from SQL Server 2005 onwards).
To resolve any table name, SQL Server will look in your default schema. By default, this will be dbo. The only way to access a table without specifying a schema name is for it to be in your default schema, or for you to add a synonym for the table within your default schema.