Getting data from another Database within procedure in DB2 - database

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

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

Azure - How to execute UDF from external data source in select statement

I have an external data source created in DB1 and its pointing to DB2 database in Azure.
I want to query a UDF defined in DB2 from DB1.
Following syntax works, from DB1:
EXEC sp_execute_remote
N'Ext_DB2',
N'SELECT Result'
But I want to use following syntax:
select C1, C2, < function_in_DB2 >
from t1
I tried searching web, this format is not mentioned anywhere, is it not supported? Any pointers will help.
Thanks !
Just per my experience, I think Azure SQL database doesn't support it. As we know, we can't do cross database query directly.
Since none document talk about this, I would suggest you ask Azure SQL support to get more support, ref: How to create an Azure support request.
Hope this helps.

DB Link from Oracle to MS Server

I am new to this forum and already searched for 45 minutes to find a solution for my problem. I hope you can help me.
A Gateway to a remote Microsoft SQL Database was installed on a Oracle Server (Oracle 12c). The tsnnames.ora file was appropiately set up.
For the connection, I created a database link (In the Oracle DB) as follows:
CREATE DATABASE LINK TEL CONNECT TO "fb_B2C" IDENTIFIED BY "passwort" USING 'dg1msql';
When I now execute the Select statement:
SELECT "name" FROM "sys"."databases"#TEL
it shows me the according databases. Among others, I can see the AB_Colors database.
Now, I want to select a view in the AB_Colors database.
Due to the fact I can connect to this database via Excel, I know that in the database AB_Colors, there are 10 Views(A,B,C,..). I would like to select the View C from the database AB_Colors via the DB LInk.
Owner of the View is b2b.
How do i need to formulate the select statement to do it?
I already tried different writings:
SELECT * FROM b2b.C#TEL;
SELECT * FROM "AB_colors"."b2b"."C"#TEL;
SELECT * FROM [AB_Colors].[b2b].[C]#TEL;
The common error message is: View/Table does not exist
I highly appreciate your help,
Fedja
This is the correct format
SELECT * FROM "b2b"."C"#TEL;
The issue maybe because the database you want to select from is not the one specified in the gateway for dg1msql. You can't add the database name to the query so you must specify it in the gateway connection.
This is defined in
$ORACLE_HOME/dg4msql/admin/initdg4msql.ora
where you should check HS_FDS_CONNECT_INFO

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]

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