Friday, February 7, 2020

Parameterize Schema/Owner Name in SAP BODS SQL scripting function

Scenario: Requirement was to parameterize a schema name in SQL function in BODS scripting language. Schema name should be fetched from the datastore connection and used dynamically in job.

 You can follow the below steps to get this done,

Step 1: First fetch the schema name from the datastore name. Declare a variable in workflow, say $LV_TableOwnerDB to store the schema name. WE can use db_owner pre-built function for the same as below,


# Get the schema/owner name from from datastore using alias name.
$LV_TableOwnerDB = db_owner( 'DATASTORE_NAME', 'ALIAS_NAME');
 print('TABLE OWNER  = '||$LV_TableOwnerDB);

Now $LV_TableOwnerDB variable would store the schema name of your target database.

Step 2: Now declare another variable where you store the SQL Script to be executed.

$LV_Temp = 'UPDATE '||CAST($LV_TableOwnerDB,'VARCHAR(15)')||'.TABLE_NAME SET ACTIVE_FL =\'Y\' WHERE ACTIVE_FL=\'N\'';
Print($LV_Temp);

Step 3: Now pass the variable with SQL stored as argument to the sql function as the second parameter below.

sql('DATASTORE_NAME',$LV_Temp);

Suppose the Datatstore name is 'EDW_HR' alias name defined in datastore is 'ALIAS_1', the script would be as below,

# Get the schema/owner name from from datastore using alias name.
$LV_TableOwnerDB = db_owner( 'EDW_HR', 'ALIAS_1'); print('TABLE OWNER  = '||$LV_TableOwnerDB);
$LV_Temp = 'UPDATE '||CAST($LV_TableOwnerDB,'VARCHAR(15)')||'.TABLE_NAME SET ACTIVE_FL =\'Y\' WHERE ACTIVE_FL=\'N\'';Print($LV_Temp);
sql('EDW_HR',$LV_Temp);

Let us know if this helped. :)

Sunday, March 17, 2019

Datastage - ExecuteCommandActivty - ParamValue/LimitValue not Appropriate [Solved]

Being new to Datastage, I had myself pulling my hair out with this issue. 

Scenario: We have a file with a date in linux server which would act as the Extract Date for one of our ETL sources. 

So say we have this file, extractdate.txt in a specific folder in Linux Server and would contain a datetimestamp value like '01-JAN-2019 03:47:30 AM'. 

We would then read from this flat file using the 'Execute Command' activity and pass it on to  a parameter (ExtractDate) in the next sequential job activity load in a Sequencer Job. Something like below, 

Datastage Sequencer Job
Issue: The problem with the job now was that I was not able to successfully set the parameter in JobActivity with the output from the Execute Command Activity Step. 

Code in ExecuteCommand Activity was a simple cat command as below, 

cat /location/extractdate.txt

The output of the same, I am trying to write into a parameter in the Job_Activity object property as below,


While I try to execute with the above setup, I am facing the error below,

Error calling DSSetParam(prmCNIRTP), code=-4 [ParamValue/Limitvalue is not appropriate]

Solution: The solution is to rap the value expression in Job Activity as below instead of how I have given in the screenshot above,

EReplace(Execute_Command_0.$CommandOutput,@FM,"") 

This solved it for me. The above statement removes any special characters in the flat file. Special characters are highly likely when you read from flat files. 

References: 


Friday, March 1, 2019

Filter column with '&' value in Oracle SQL Developer

We had a data dump into oracle, where in a column value had an '&' in it. Say for example a company name like 'Johnson & Johnson'.


What happens now is SQL Developer expects a substitution variable when it encounters an ampersand.

If you have a table and need to filter on the same, you can go with the below script

SELECT * FROM ORGANIZATION WHERE ORG_NAME IN ('Johnson '||chr(38)||' Johnson'

This should prevent SQL Developer from asking you for a substitution variable.

Wednesday, January 16, 2019

Script to terminate BODS Job on condition

Scenario: We have a metadata table for extract runs, before running an extract job, we need to check whether the previous run was successful. This can be done in Data Services using scripting.

Solution:

Use the following script

if ( sql('DS_Datastore_Name','SELECT EXTRACT_STATUS FROM EXTRACT_RUN WHERE EXTRACT_NAME ='Extract 1') != 'Completed'  )
begin
raise_exception_ext( 'Job terminated due to error in previous run. Correct the previous load and re-run again',1);
end

In the above code, we write a script where in we use an if condition and inside the if condition with the help of a SQL function, we write code to check if the previous run was completed or not.  If not completed, it goes inside the if condition where we have written the below code,

raise_exception_ext( 'Job terminated due to error in previous run. Correct the previous load and re-run again',1);

This code will make sure that a exception is raised and there by failing the job.

Wednesday, May 30, 2018

SAP Data Services SQL Transform with variable - Invalid Pseudocolumn error [Solved]

Scenario: I had a scenario where in I needed to achieve a lead, lag column logic in BODS and had to do with a SQL Transform. Here I have a filter on date, i.e filter CreateDateTime column less than extract date. So I have a variable $PV_ExtractDate where in I pass my extract date value.

which would make my query look something as below,

SELECT COL_A, COL_B FROM SALES WHERE CreateDateTime < $PV_ExtractDate

The above syntax in SQL Transform was creating an error for me as the value was not being passed to the variable.

Error: SQL submitted to ODBC data source <SAMPLE> resulted in error <[Microsoft][SQL Server Native Client 11.0][SQL Server]Invalid pseudocolumn "$PV_ExtractDate".>

Cause: BODS doesn't recognize $PV_ExtractDate as a variable value in SQL Transform, this can be corrected by using the following syntax,

SELECT COL_A, COL_B FROM SALES WHERE CreateDateTime < {$PV_ExtractDate}
Note: It is advised not to go with a SQL Transform, unless no workaround is feasible in SAP BODS. This is because you are writing code that is specific to a particular database, hence if the source system is changed to a new type of database an issue might occur.