Skip to Content
Author's profile photo Former Member

Step by step tutorial – cross-database queries in SAP HANA SPS09

Hi everyone, in this blog post I’m gonna show you how to query data across databases. As we know, the concept of multitenant database containers (MDC) was introduced in SAP HANA SPS09. With MDC, now we can run multiple tenant databases on one SAP HANA system and that enables strong separation of data and users as well. But do you know the cross-database queries are also supported in SAP HANA SPS09? For example, it’s possible for one user in its tenant database to query data from another tenant database. But how can we achieve this? Is it enabled by default? Maybe you have a lot of questions. Don’t worry. Let’s first have a look at some basics of the cross-database queries and I’ll show you an example step by step.

Related materials

First of all, I’d like to share with you some useful materials about this topic. You can find everything I showed in this post from the following materials. If you’re interested, you can read them and make the same example with mine.

Key concepts

Here I just borrowed page 41 from SAP HANA SPS 09 – What’s New? – Security, since I think this page highlighted the key concepts well.

1.PNG

The key concepts can be summarized as:

  • Cross-database queries are not enabled by default. Thus we need to configure it.
  • The mechanism of cross-database queries is based on “remote identity” which means User_1 plays the role of User_2 when he/she queries data from Table_A in the above example.
  • A user in the target database can only be associated with one user in the source database, which means User_2 cannot be associated with User_3.
  • The association is unidirectional.
  • Only the SELECT privileges are considered, which means User_1 is not allowed to insert data into Table_A.

Scenario

Based on the key concepts, we can create a scenario to test the cross-database queries in SAP HANA SPS09 MDC environment.

2.PNG

As the above image displayed,

  • there are three tenant databases in our scenario, i.e., DB1, DB2 and DB3.
  • Meanwhile, there are three users in different tenant databases, USER_A in DB1, USER_B in DB2 and USER_C in DB3.
  • USER_A has a table named PRODUCT which holds products in China.
  • USER_B has a table named PRODUCT as well which keeps products in the US.
  • Now USER_C wants to create a PRODUCT view which unions all products in China and in the US as well.

Now comes the problem. How can we achieve this? 😕

Step by step example

Notice: At the beginning of each step, I’ll first show <DATABASE>(<USERNAME>) in this step.

1. SYSTEMDB(SYSTEM) Create three tenant databases


CREATE DATABASE DB1 SYSTEM USER PASSWORD Manager1;
CREATE DATABASE DB2 SYSTEM USER PASSWORD Manager2;
CREATE DATABASE DB3 SYSTEM USER PASSWORD Manager3;







2. SYSTEMDB(SYSTEM) Make the configuration in red box at the system layer, Administration -> Configuration -> global.ini -> cross_database_access

/wp-content/uploads/2014/12/3__611671.png

We first enable the cross-database queries and then set DB1 and DB2 as targets for DB3.

3. DB3(SYSTEM) Create USER_C in the tenant database DB3


CREATE USER USER_C PASSWORD aA111111;







4. DB1(SYSTEM) Create USER_A in the tenant database DB1 and associate USER_A with USER_C in the tenant database DB3


CREATE USER USER_A PASSWORD aA111111;
ALTER USER USER_A ADD REMOTE IDENTITY USER_C AT DATABASE DB3;







You can also use CREATE USER … WITH REMOTE IDENTITY … statement directly like in step 5.

5. DB2(SYSTEM) Create USER_B in the tenant database DB2 and associate USER_B with USER_C in the tenant database DB3


CREATE USER USER_B PASSWORD aA111111 WITH REMOTE IDENTITY USER_C AT DATABASE DB3;







You can also use CREATE USER plus ALTER USER statements like in step 4.

6. DB1(USER_A) Create the PRODUCT table and insert some products.


CREATE COLUMN TABLE USER_A.PRODUCT (
  ID INTEGER,
  NAME NVARCHAR(100),
  LOC NVARCHAR(100),
  DESC NVARCHAR(100)
);
INSERT INTO USER_A.PRODUCT VALUES (1001, 'Product A', 'CN', 'Made in China');
INSERT INTO USER_A.PRODUCT VALUES (1002, 'Product B', 'CN', 'Made in China');







7. DB3(USER_C) Can’t wait anymore? OK. Let’s first let USER_C query products made in China


SELECT * FROM DB1.USER_A.PRODUCT;







Here you go.

/wp-content/uploads/2014/12/4__611715.png

8. DB2(USER_B) Create the PRODUCT table and insert some products


CREATE COLUMN TABLE USER_B.PRODUCT (
  ID INTEGER,
  NAME NVARCHAR(100),
  LOC NVARCHAR(100),
  DESC NVARCHAR(100)
);
INSERT INTO USER_B.PRODUCT VALUES (2001, 'Product C', 'US', 'Made in US');
INSERT INTO USER_B.PRODUCT VALUES (2002, 'Product D', 'US', 'Made in US');







9. DB3(USER_C) Now let USER_C query products made in US.


SELECT * FROM DB2.USER_B.PRODUCT;






/wp-content/uploads/2014/12/5__611716.png

10. DB3(USER_C) Create the PRODUCT view and query the data from this view.


CREATE VIEW USER_C.PRODUCT AS
SELECT * FROM DB1.USER_A.PRODUCT
UNION ALL
SELECT * FROM DB2.USER_B.PRODUCT;
SELECT * FROM USER_C.PRODUCT;






/wp-content/uploads/2014/12/6__611731.png

Till now we’ve successfully achieved the scenario we created. 😎

Want more?

DB3(USER_C) Failed to insert data into table DB1.USER_A.PRODUCT since only SELECT is allowed.


INSERT INTO DB1.USER_A.PRODUCT VALUES (1003, 'Product E', 'CN', 'Made in China');





7.PNG

DB1(USER_A) Failed to let USER_A query data from the view DB3.USER_C.PRODUCT since the unidirectional association.


SELECT * FROM DB3.USER_C.PRODUCT;




8.PNG

DB2(SYSTEM) Failed to associate USER_B with USER_A at the tenant database DB1 since only one user is allowed.


ALTER USER USER_B ADD REMOTE IDENTITY USER_A AT DATABASE DB1;



9.PNG

DB1(SYSTEM) Have a look at remote users.


SELECT * FROM REMOTE_USERS;



/wp-content/uploads/2014/12/10__611744.png

You can do the same with DB2(SYSYEM).

That’s it! Hope you enjoyed reading my blog and making cross-database queries successfully. 🙂

Assigned Tags

      15 Comments
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Nice one 🙂 Thank you !!!

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thank you. I'm glad you liked it. 🙂

      Best regards,

      Wenjun

      Author's profile photo Fernando Jorge Bartolo
      Fernando Jorge Bartolo

      Excellent. Thanks!

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thank you. 🙂

      Author's profile photo Sergio Guerrero
      Sergio Guerrero

      great content Wenjun

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks Sergio 🙂

      Author's profile photo Former Member
      Former Member

      Thanks for the information. Do you know if a table/view within another tenant DB can be used directly in a graphical calc view in HANA without SDA?

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      I don't think so... It should be OK in SQLScript view.

      Best regards,

      Wenjun

      Author's profile photo Peng Zhang
      Peng Zhang

      Thanks for sharing.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      🙂

      Author's profile photo safiyudeen Vattaparambil
      safiyudeen Vattaparambil

      Great Work. Thank you.. Helpful content.

      Author's profile photo Former Member
      Former Member
      Blog Post Author

      Thanks. 🙂

      Author's profile photo Denys van Kempen
      Denys van Kempen

      Hi,

      For those interested, we just uploaded a full playlist on multitenant database containers:

      SAP HANA Multitenant (MDC) videos now available on the SAP HANA Academy

      Regards,

      Denys

      Author's profile photo Former Member
      Former Member

      Great work. Thanks You 🙂

      Author's profile photo Former Member
      Former Member

      Hi Wenjun,


      Great work.


      How can we create SAP HANA Information Views using cross database tables?


      Is there are any impact on Performance?


      Shirish.