cancel
Showing results for 
Search instead for 
Did you mean: 
Read only

HANA SQL loop over columns

martin_chambers
Participant
0 Likes
2,329

I'm working in SAP BW powered by HANA. You can transform data using SQL in AMDPs (ABAP Managed Database Procedure).

I have tried and failed to write a script that can do the following -

1) Compare each column of 2 tables with an identical structure

2) Determine for each record and for each column if the column value differs between the 2 tables

3) List the difference for each record and column

4) The program should be generic, i.e. if the table structure changes, I will not have to change the code. The code should be able to determine the table structure.

I did think about using "cursor". Didn't work. Not sure whether this is because cursors don't work in an AMDP or because they can't use internal tables.

Example

Table 1

Col1 Col2 Col3

A01 1 2

A02 3 4

Table 2

Col1 Col2 Col3

A01 1 2

A02 3 5

Result

Col1 Col2 Col3 Col4
(ID) (Col Name) (Value 1) (Value 2)

A02 Col3 4 5

Accepted Solutions (0)

Answers (1)

Answers (1)

former_member302041
Contributor
0 Likes

Wow, very challenging requirement (especially "generic")...

In general I would try the next (it isn't a solution, but direction) in AMDP:

1. in each table concatenate all non key values (something like this)

In order to be generic we need to know what columns are in the table so we can try using "SYS"."COLUMNS"

2. join the tables and compare concatenated columns to identify rows that differ (using case/when)

3. finally, if you need to know in which column there is a difference then try to parse values by "|" and compare

It seems to be extremely complex solution (if it is possible at all).

Please share if you succeed (in any way)...

BR,


martin_chambers
Participant
0 Likes

Hi Andrey,

Thank you very much for your solution. In my particular situation, I already know which records are different. I need to identify the fields with different contents. Parsing does seem like a complicated way. Our current solution, is simple, but not generic. I simply write a comparison for each field. Unfortunately, this means that I have to adjust the coding if the underlying table structure changes.

By the way, a generic solution is possible in ABAP. In retrospect, I probably should have chosen that path, as the number of records is very small (1000 records). No performance issues.