Getting function name from a datastore could be useful if you have configured more than one datastores in your project. This helps avoiding code change while migrating objects from one environment to another during the code promotion.
Below is step by step procedure to create custom function to get the database name from a datastore and call it in a Batch Job.
1) Go to your Local Object Library and choose Custom Functions then right click on Custom Function and select New
2) Enter name of the function as ‘CF_DATABASE_NAME’
3) Enter the below line of code inside the editor.
Click on the above image to zoom it.
Then declare an input parameter named $P_Datastore of length varchar(64). Then Validate the function and if no error found then Save it.
4) Create a global variable at Job level in any of the test batch job you have created and name this global variable as $G_Database of length varchar(64)
5) Call this function in one script of your batch job and use this Global variable wherever required in your Job.
You can call this function as under in script. You simply need to pass the name of your datastore in single quotes.
Example of a practical use:
sql(‘CONV_DS’,’ TRUNCATE TABLE ||$G_DATABASE||’.ADMIN.CONV_CUSTOMER_ADDRESS’);
So the above code will read database name at run time from global variable and truncate records before it loads in the Job. This code can be used in any environment when you promote your code else if database name is hard coded then you will end up updating the code in every new environment.