Access A table synonym created in a DB through a DB Link - database

Problem :-
Table is present in Database 1.
Synonym of the table is created in Database 2 using Db link created in Database2 which is pointing to Database 1.
Another DB Link created in Database 3 pointing to Database 2.
While accessing the Synonym of the table present in Database 2 its giving the error :-
ORA-00980 synonym translation is no longer valid
So i guess we cannot access a synonym present in a remote database and its object is present in another remote database.
Any suggestions on the above scenario. What would be the best solution for the mentioned problem.

It seems db-link from DB3 to DB1 may help.
See Ask Tom (notice, below DB3 <- DB2 <- DB1, procedure compiled on DB1):
CAUSE
This issue was reported in Bug 2829591 QUERYING FROM A PL/SQL
PROCEDURE IN 9I -> 8I-> 7.3.4, GETTING ORA-980. This bug was closed
as 'NOT A BUG' for the following reasons
PL/SQL cannot instruct middle database (DB2) to follow the database
link during the compilation phase. Therefore in order for this PL/SQL
block to compile and run, both database links dblink1 and dblink2
should be defined on the front end database - DB1. During runtime
database link dblink2 will be looked up in DB2 as expected.
SOLUTION
To implement the solution, please execute the following steps:
Create a database link dblink2 on DB1 pointing to DB3
...
Create and compile the PL/SQL block on DB1.

Related

How to solve the sql execution error in snowsql client?

I am getting error while creating a table in snowsql client which says
003540 (42501): SQL execution error: Creating table on shared database 'SNOWFLAKE_SAMPLE_DATA' is not allowed.
can anyone help me out how to resolve this error?
Snowflake sessions have "context", such as current role, warehouse, database, and schema.
It appears your current session "context" has your database set to one of sample databases,
which was created from a "Share".
Databases created from a share are readonly, so you cannot create a table in them.
The good news is the fix is easy, you can either:
use a fully qualified table name (databaseName.schemaName.tableName) to create your table in a database/schema that you have proper access to.
USE DATABASE snowflake_sample_data;
USE SCHEMA tpch_sf1;
CREATE TABLE myDatabase.mySchema.myNewTableName AS SELECT * FROM lineitem;
change your session context to a database/schema you have access to create tables, and use the fully qualified table name of the tables you are reading from.
USE DATABASE myDatabase;
USE SCHEMA mySchema;
CREATE TABLE myNewTableName AS SELECT * FROM snowflake_sample_data.tpch_sf1.lineitem;
Relevant docs links:
https://docs.snowflake.com/en/sql-reference/sql/use.html
https://docs.snowflake.com/en/user-guide/data-sharing-intro.html#how-does-secure-data-sharing-work
https://docs.snowflake.com/en/sql-reference/name-resolution.html
https://docs.snowflake.com/en/user-guide/sample-data-using.html#querying-tables-and-views-in-the-sample-database

Getting data from another Database within procedure in DB2

I have two databases DB1 and DB2 and I want to call a stored procedure in DB1 and get data from DB2.
Create procedure diffdbtest()
LANGUAGE SQL
DYNAMIC RESULT SETS 1
BEGIN
DECLARE C1 CURSOR WITH RETURN FOR
SELECT * FROM Db2.myschema.tabletest;
OPEN C1
END#
I get Db2.myschema.tabletest is not defined.
Both DBs have the same user and password (if possible how can I use different users?)
Any idea what is wrong?
Running DB2 Express v10.5 Windows
Thanks.
The only way as far as I know is you have to use federation. You need to:
create server wrapper to DB #2
create nickname on table that is referring the table tabletest
create user mapping
etc.
Then, you can access that table via nickname.
Details on how to do this can be found in knowledge center: https://www.ibm.com/support/knowledgecenter/en/SSEPGG_11.1.0/com.ibm.data.fluidquery.doc/topics/tlsdb201.html
Hope this helps.
Kevin See
Db2 Hybrid Cloud Security Dev Team

Query database running on another physical SQL Server

Historically we have a product which installed two databases on the same server. There is an custom application which assumes that both databases are on the same server.
In a new version they have split the databases onto two separate servers and obviously now the custom application is giving the error:
Database 'DB_2' does not exist. Make sure that the
name is entered correctly.
Is there anything I can do in the SQL Server setup so that the application is still able to query the DB_2 database without modifying the custom application?
The query being used is structured as follows:
Use DB_2
SELECT * FROM MyUser.MyTable
You can create a linked Server, then Create a Database DB_2 add a Synonym for different objects. something like below.
use master
GO;
EXEC master.dbo.sp_addlinkedserver #server = N'RemoteServer', #srvproduct=N'SQL Server'
GO
CREATE DATABASE [DB_2];
GO
USE [DB_2]
GO
CREATE SYNONYM [MyUser].[MyTable] FOR [RemoteServer].[db].[MyUser].[MyTable]
GO
You can use Linked Servers feature. In SSMS go to Server Object/Linked Servers folder in Object Explorer. And link second server. So you can query another DB using this SELECT * FROM [Linked_Server_Name].[Database_Name].[Schema_Name].[Table_Name]

Unable to create objects with db link; ORA - 0251: another session or branch in same transaction failed or finalized

I have a Heterogeneous Link between Oracle 11.2.0.3 and SQL Server 2008. When I attempt to create views, or procedures that reference the Heterogeneous Services views for system tables, I get 4 errors listed below:
ORA-00604: error occured at recursive SQL level 1
ORA-02051: another session or branch in same transaction failed or finalized
ORA-06512: at "SYS.HS$_DDTF_SQLTABLES", line 58
ORA-06512: at line 1
As an example, these errors are created when I try to execute the following code:
CREATE VIEW ALLMYTABLES AS
SELECT * FROM ALL_TABLES#DBLINKNAME;
I am however able to create views on any normal table:
CREATE VIEW RANDOMTABLE AS
SELECT * FROM RANDOMTABLE#DBLINKNAME;
I have done some research to issues similar to this one, but have not found any references to this specific issue. I believe it may have something to do with the conflict between the concurrent operations of creating the view and the select * statement call to the SQL server database. Or perhaps that the records are not entirely fetched when the view is created.
I do not know the specifics of the HS link configuration and properties.

SQL Server cross database alias

I'm trying to understand how I can use an alias to reference another database in the same instance, without having to use a hardcoded name.
The scenario is as below:
I have a data db with stores data, an audit db which keeps all changes made. for various reason, i want to keep the audit data in a separate database, not least because it can get quite large and for reporting purposes.
In the data db, I don't want to reference this by a hardcoded name but an alias so that in different environments, I don't have to change the name and various sp's to reference the new name.
for example:
mydevdata
mydevaudit
If a sp exists in mydevdata such as which calls the mydevaudit, I don't want to change the sp when I go to test where the db's may be called mytestdata and mytestaudit. Again, for various reasons, the database names can change, more to do with spaces an instances etc.
So if I had procedure in mydevdata:
proc A
begin
insert into mydevaudit.table.abc(somecol)
select 1
end
when I go to test, I don't want to be change the procedure to reference another name, (assume for sake of argument that happened)
Instead I am looking to do something like:
proc A
begin
insert into AUDITEBALIAS.table.abc(somecol)
select 1
end
I am interested in finding out how I could do something like that, and the pro's and cons.
Also, dymnamic SQL is not an option.
thanks in advance for you help.
You may be able to use synonyms
CREATE SYNONYM WholeTableAliasWithDBetc FOR TheDB.dbo.TheTable
This means all object references in the local DB are local to that DB, except for synonyms that hide the other database from you.
You can also use stored procedures in the audit DB. There is a 3rd form of EXEC that is little used where you can parametrise the stored proc name
DECLARE #module_name_var varchar(100)
SET #module_name_var = 'mydevaudit.dbo.AuditProc'
-- SET #module_name_var = 'whatever.dbo.AuditProc'
EXEC #module_name_var #p1, #p2, ...
Obviously you can change module_name_var to use whatever DB you like
I've just posted this to How to create Sql Synonym or "Alias" for Database Name? which is a workaround for the same situation:
There is a way to simulate this using a linked server. This assumes you have two SQL servers with the same set of databases one for development/test and one live.
Open SQL Server Management Studio on your development/test server
Right click Server Objects > Linked Servers
Select New Linked Server...
Select the General page
Specify alias name in Linked server field - this would normally be the name of your live server
Select SQL Native Client as the provider
Enter sql_server for Product Name
In Data Source specify the name of the development server
Add Security and Server Options to taste
Click OK
The above is for SQL Server 2005 but should be similar for 2008
Once you've done that you can write SQL like this:
SELECT * FROM liveservername.databasename.dbo.tablename
Now when your scripts are run on the development server with the linked server back to itself they will work correctly pulling data from the development server and when the exact same scripts are run on the live server they will work normally.

Resources