Join two tables from separate database - sql

How can i open db connection from stored procedure. is it possible ? is there any command to do this ?
I want use two db in one query but ef return error for using two dbcontext. How can i join two different db's table anyway?
from a in dbContextArch.Archive
left join dbContextMain.Envelope
on

The best way of doing it is by using a Synonym, which will be mapped to the table on the other server. It can be created as below;
CREATE SYNONYM [dbo].[SYN_dbContextMain] FOR [dbContextMain].[dbo].[Envelope]
Then in your queries, all you need to do, is to use the synonym created instead of the server name;
from a in dbContextArch.Archive
left join SYN_dbContextMain
on ..

Related

Postgres database.table notation

Im trying to alter a table ALTER TABLE database.table...
What is the correct notation to access tables in a DB from the base postgres db without having to explicitly connect to that database?
There is no way. You have to connect to the right database.
A DATABASE within MySQL is comparable with a SCHEMA in PostgreSQL and many other brands.
MySQL: ALTER TABLE database.table...
Others: ALTER TABLE schema.table...
From the MySQL manual:
CREATE SCHEMA is a synonym for CREATE DATABASE.
This actually means that a single MySQL server has just a single database. This database can have many schema's and every schema can have many tables.
Within a single database you can jump from one schema to the other schema, no problem. This works for MySQL, PostgreSQL and many others. You can not jump from one database to another database without a new database connection because it's a different instance.
It is that MySQL uses a different name for a schema, it calls this a database. A little confusing.
If you want the same thing in other databases, like PostgreSQL, just use schema's within a single database.

Can't delete table in different schema from Apache Ignite

I try to drop my tables in Apache ignite.
For example;
drop table if exists city
The code is working for PUBLIC schema but I couldn't delete tables from other schemas.
The error says 'Failed to parse query. Table "PRODUCT" not found;'
Here is the screenshot of my PowerShell?
How can I delete the tables belong the different schemas?
You need to fully qualify it:
drop table "Product".PRODUCT;
You need to do the same thing whenever you reference tables in different schemas, for example with a JOIN.

Use schema name in a JOIN in Redshift

Our database is set up so that each of our clients is hosted in a separate schema (the organizational level above a table in Postgres/Redshift, not the database structure definition). We have a table in the public schema that has metadata about our clients. I want to use some of this metadata in a view I am creating.
Say I have 2 tables:
public.clients
name_of_schema_for_client
metadata_of_client
client_name.usage_info
whatever columns this isn't that important
I basically want to get the metadata for the client I'm running my query on and use it later:
SELECT *
FROM client_name.usage_info
INNER JOIN public.clients
ON CURRENT_SCHEMA() = public.clients.name_of_schema_for_client
This is not possible because CURRENT_SCHEMA() is a leader-node function. This function returns an error if it references a user-created table, an STL or STV system table, or an SVV or SVL system view. (see https://docs.aws.amazon.com/redshift/latest/dg/r_CURRENT_SCHEMA.html)
Is there another way to do this? Or am I just barking up the wrong tree?
Your best bet is probably to just manually set the search path within the transaction from whatever source you call this from. See this:
https://docs.aws.amazon.com/redshift/latest/dg/r_search_path.html
let's say you only want to use the table matching your best client:
set search_path to your_best_clients_schema, whatever_other_schemas_you_need_for_this;
Then you can just do:
select * from clients;
Which will try to match to the first clients table available, which by coincidence you just set to your client's schema!
You can manually revert afterwards if need be or just reset the connection to return to default, up to you

nhibernate two different databases in the same session

When writing sql, I can do
BEGIN Trans t;
SELECT a.name, b.name from db1.dbo.A as a
JOIN db2.dbo.B as b
ON b.aId = a.Id
COMMIT Trans t;
When two databases are on the same database instance.
I am wondering how I can achieve this with database mapping. So that I don't need to create multiple sessions for queries involves different databases.
No, so far I have not seen any other solution then creating a view within one database which does the cross DB query and then use nh mappings on that view, or maybe fully-qualified names. (see duplicate-link below your post).
There is a possibility to do it with DB synonyms. It is not an optimal solution performance-wise, but it might solve your current bind.
SQL Server Synonyms

Using name of a database inside a sql script for a full table name

I struggled for a while with a bug, and then found out the reason for it in a database stored procedure code, which contained the old name of a database in a table name, whereas the current database name was already different. So, I'd like to ask:
Is there a situation in which using a database name as a part of a full table name (database name + schema name + table name) can be justified (provided we don't touch tables in other databases) or is it always a bad practice? How to correctly use a database name in sql scripts to keep code neutral to a specific database?
Code just for an illustration:
CREATE PROCEDURE [dbo].[MyProc]
AS
BEGIN
DELETE FROM [MyDatabase].[dbo].[MyTable]
END
No, you shouldn't use database names in a stored procedure unless you need to address two databases.
It causes exactly the kinds of bugs you're seeing. When the database name changes, all your SP code breaks, or continues working, but on the old database.
It does make sense if you are sending a SQL query to the database, but only if the application dynamically picks the database name to insert into the query.
My suggestion is that you do a full export of your database schema, and search for database names that are hardcoded and remove them.
It really depends on how your scripts are implemented.
Even if you don't refer to a table as
[MyDatabase].[dbo].[MyTable]
you will still need to refer to the database by:
USE [MyDatabase]
earlier in the script.
It is possible to mix trusted database tables in a single query. When someone do this,it is justified and mandatory to include database on table 'path'.
I don't found a reason out of this scenario if stored procedure and table is on the same database.
You can search all database name occurencies through database catalog in order to fix your development. For SQL Server 2005:
SELECT Name
FROM sys.procedures
WHERE OBJECT_DEFINITION(OBJECT_ID) LIKE '%databasename%'
GO
For SQL Server 2000:
SELECT DISTINCT so.name
FROM syscomments sc
INNER JOIN sysobjects so ON sc.id=so.id
WHERE sc.TEXT LIKE '%databasename%'
GO