Development resources, articles, tutorials, code samples, tools and downloads for SAP HANA and ABAP, HANA Database, SQLScript, SAP UI5, Screen Personas, Web Dynpro, Workflow
Sample HANA Database with Table Data for SQL Developers
SAP HANA database SQLScript developers can execute given SQL scripts in this tutorial to create a sample HANA database populated with sample data to work hands-on on created database tables to improve their SQL knowledge.
I believe to experience SQL programming on HANA database, programmers require more sample databases to play with.
On SQL Server, there is a quite number of sample databases which can be downloaded as a database backup and can be restored on data platform easily.
Or database developers can find SQL scripts that can be executed to create sample database tables and INSERT commands to fill those database tables with sample data.
In this HANA database tutorial, I want to share SQL script files that programmers can download and execute on their sample databases. Please note that the tables and data are extracted from AdventureWorksLT database and converted to SQLScript to use in HANA database.
Some of the tables for our sample HANA database shared in this guide are Product, Customer, Address and Sales Order related tables.
By joining and querying these tables HANA database developers can improve their SQL knowledge.
Before creating tables maybe it is better to check if same table names are used before in your current schema. To be in the safe side, I did not share the DROP TABLE commands in front of each CREATE COLUMN TABLE command.
I left this decision to SQL programmers. If there is not a match with table names used in our sample database, SQL developers can create sample tables without any issue.
Data Model for Sample HANA Database
AdventureWorksLT2017 sample database is used on SQL Server by SQL programmers working on that platform as a sample database.
In this guide, I tried to convert SQL Server table structures and sample data into HANA database equivalents as much as I can.
For overall DDL (Data Definition Language) statement of the data model, please use sample-database-for-hana
Address table.
Please note that the DEFAULT constraints for ModifiedDate and rowguid columns can either be defined in DDS (Data Definition Statement) of Address table CREATE TABLE command or can be explicitely created with ALTER TABLE ALTER column syntax.
ProductCategory table.
Please be aware that when SQL programmer runs following SQLScript batch, column table is created and a foreign key is added to the table referencing itself between ProductCategoryID and ParentProductCategoryID columns.
ProductDescription table
ProductModel table.
Please note that the CatalogDescription field in ProductModel table is in XML data type on SQL Server.
Since there is not an XML data type in SAP HANA database, SQLScript developers can use nvarchar(5000) to map XML data on HANA database table columns.
ProductModelProductDescription table is a mapping table where all valid combinations of product models and descriptions are stored.
And now we are ready to create Product table.
In this table SQLScript developers will face a few data type mapping issues from original SQL Server data types to their corresponding data types on SAP HANA database.
One of the data types that don't exist in SAP HANA but in SQL Server is money data type.
When working on HANA database, SQL programmers can use decimal(19,4) for matching money amount values.
Other data type that does not exist in HANA database but exists in SQL Server is varbinary(max).
SQLScript developers can use blob data type for such values.
Customer table can be created by executing below CREATE TABLE DDS command on a development HANA database.
Although the NameStyle column was created as bit data type on SQL Server, we have to convert it to tinyint on HANA database.
In our sample HANA database, SQLScript developers will create an additional database table named CustomerAddress
SalesOrderHeader sample HANA database table can be created bby executing below SQLScript command.
SalesOrderDetail DDS (Data Definition Statement) CREATE TABLE script to be executed on our sample HANA database is as follows
Data for Sample HANA Database
Sample data for ProductCategory table can be created using below dynamically created INSERT statements.
I did not copy all 41 row data below.
You can get the full list of ProductCategory Insert commands using ProductCategory-table-data-for-hana-database
The sample data for HANA database table ProductCategory is extracted from SQL Server AdventureWorksLT2017 sample database using following SQL query
To export sample table data from SQL Server for use in HANA database table ProductDescription, I used following SQL Select statemen.
Sorry, since some of the data was corrupt I had to clean them by replacing '?' and repeating space characters etc.
Because of that reason, there are some replace and trim commands that will not be used in normal cases.
For ProductModel table, following SQL query can be used on SQL Server to export sample data for HANA database table.
Just to add some note for SQL programmers who are wondering why there are REPLACE() functions.
The reason is that. In Name column there are text including ' for example "Women's Mountain Shorts"
Such values should be escaped.
This is similar for the originally XML data type column values of CatalogDescription field.
The output of the above SQL script which contains INSERT DDS commands to execute on HANA database to populate ProductModel table with sample data can be found at ProductModel-table-data-for-hana-database
Another table in our sample HANA database is ProductModelProductDescription table.
The export SQL query is as follows for this sample table.
One last important sample table is Product table.
I executed below SQL SELECT statement to create INSERT DDS statement for importing this data into HANA database.
One column data is missing in this sample data migration, it is the ThumbNailPhoto column values which are in binary format.
As I will also mention later converting money data type to string or character types on SQL Server requires attention.
Otherwise, you can simply lose some data stored in decimal part of the table column.
There are two solutions one is using "trim(str(MoneyDataTypeColumn,19,4))" or using "convert(nvarchar(50),convert(numeric(20,4), MoneyDataTypeColumn))"
I used the later option for StandardCost and ListPrice columns of Product table.
And the sample database table Product content can be created on HANA database by executing the INSERT scripts given here Product-table-data-for-hana-database
For Address table data following SQL script is used to export data from SQL Server.
HANA database SalesOrderHeader table can be populated with sample data using below INSERT commands.
There are a few issues regarding to migration SQL script here.
First of all, computed columns aka calculated columns in SQL Server or generated column in HANA database, these column values cannot be inserted directly.
So I exclude those columns from INSERT command
Second issue is regarding to exporting SQL Server money data.
We have to first convert money type to numeric data type then as a second step we have to convert varchar() data type to protect 4 decimal points.
One more solution of this conversion problem is using "trim(str(MoneyField,19,4))" String conversion with additional function arguments then trimming the output of STR function
Delta Merge on SAP HANA Database for Data Populated Tables
Right after SQL programmer populates HANA database tables with sample data, it will be a good practise to execute delta merge statements for those sample tables in order to move new data from row store to column store and decrease sizes of tables in memory with more compression options, etc.
Executing below commands will trigger delta merge process for selected HANA database tables.
Database Views for Sample HANA Database
AdventureWorksLT has 3 database views that SQL programmers can create on SAP HANA database under their schema by executing following CREATE VIEW commands also shared SQLScript create statements at sample-database-views-for-hana
vProductModelCatalogDescription view extracts XML tag values stored in CatalogDescription field of database table ProductModel using XMLEXTRACTVALUE function.
Please note that XMLEXTRACTVALUE is new with SAP HANA 2.0
If you run XMLEXTRACTVALUE function on HANA 1.0 database, you will get following error message: SAP DBTech JDBC: [328]: invalid name of function or procedure: XMLEXTRACTVALUE
vGetAllCategories view is used to list parent and child relation of product categories.
Since SAP HANA database SQLScript does not support recursive queries yet, I have modified the original recursive CTE expression in SQL Server to a sub-select statement in HANA database.
vProductAndDescription view is used to display products and product descriptions by language.
Drop Tables and Views Created on HANA Database
If you want to drop all the sample database tables created for this SQLScript tutorial, developers can use following "DROP TABLE" commands in the given order.
Before dropping tables, database programmers should drop views as first step.
Since there are "Foreign Key"s created within sample HANA tables referincing other sample tables, a database developer should start with referencing tables before referred tables just like in above order.