The task is fairly simple, create a function that takes two arguments and queries the OBJECTS system view and returns 1 for table exists and 0 for table doesn't exist.
CREATE FUNCTION TABLE_EXISTS(IN schema_name VARCHAR(256), IN table_name VARCHAR(256)) RETURNS exists int AS BEGIN DECLARE _exists int := 0; SELECT CASE WHEN COUNT(*) > 0 THEN 1 ELSE 0 END INTO _exists FROM OBJECTS WHERE OBJECT_TYPE='TABLE' AND SCHEMA_NAME=:schema_name AND OBJECT_NAME=:table_name; exists = :_exists; END;
As you can see in listing 1, the function takes two varchar arguments. The first is the schema name and the second is the table name. The select statement queries the system OBJECTS view using the schema and table name. I've added an additional condition to ensure the object type is a table.
The function above can be used to test if a table exists in any specified schema. If you want to test if a table exists in the current schema then use the CURRENT_SCHEMA function as shown in listing 2 below.
CREATE FUNCTION TABLE_EXISTS(IN table_name VARCHAR(256)) ... FROM OBJECTS WHERE OBJECT_TYPE='TABLE' AND SCHEMA_NAME=CURRENT_SCHEMA AND OBJECT_NAME=:table_name; ...
Connect To HANA DB Using NodeJs
This article explains how to connect to a SAP HANA system using the Node HANA client library.28 October 2019 - 2552 views
Connect To HANA DB Using Python
This article explains how to connect to a SAP HANA system using the Python hdbcli package.11 October 2018 - 3906 views
Adding Auto Numbers To Table Columns
This article explains how to create an auto generated column using IDENTITY.14 March 2018 - 1594 views
Connecting To SAP HANA Using PHP ODBC
This article describes how to install and configure PHP odbc to connect to a SAP HANA system on Ubuntu 16.01 December 2017 - 3694 views
Connect To HANA DB Using Java
This article describes how to connect to a HANA DB system using the ngdbc.jar Java driver.03 November 2017 - 2501 views
Loading And Unloading Column Table Into And From Memory
This article explains how to load and unload a column table into and from memory.18 October 2017 - 1636 views
M_SERVICE_MEMORY System View
This article explains how to view detailed information on memory for HANA services.01 August 2017 - 2252 views