• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4973
  • Last Modified:

Calling stored procedure dynamically from PB Script

Problem: Calling stored procedure dynamically from PB Script
 
Situation:
I have a Customer table. For each customer one stored procedure is assigned which is saved in a column in the Customer table.
When I am viewing a customer, then respective stored procedure has to be called. There are 1000 customers and each customer has his own stored procedure, so there are 1000 stored procedures.
Each of these stored procedures accept 3 string arguments and return one integer value.
 
 
Ex.
String ls_Cust_sp_name
 
ls_Cust_sp_name = 'sp_macdonalds_budget'     // This value is retrieved from the table for the selected customer
 
// Standard way of calling a stored procedure, where the stored proc actual name is called,
// DECLARE lsp_validate_sp PROCEDURE FOR sp_macdonalds_budget(:ls_Budget_ID, :ls_Status_ID, :ls_Resp_Person );  // Here stored proc name is hardcode, hence does not server my purpose and it is not optimized.
// EXECUTE lsp_validate_sp;
 
 
DECLARE lsp_validate_sp PROCEDURE FOR ls_Cust_sp_name(:ls_Budget_ID, :ls_Status_ID, :ls_Resp_Person );    // I get compilation error, stating 'ls_Cust_sp_name' procedure not found.
EXECUTE lsp_validate_sp;

FETCH lsp_validate_sp INTO :li_Retn_Val;
 
How can I call the stored procedure in an optimized way without using CHOOSE... Case or any other loops. There can be more than 1000 customers and stored procedures.

0
xsysys
Asked:
xsysys
2 Solutions
 
sandeep_patelCommented:
Hi,

Try this...

string ls_sql,ls_spname

ls_spname = 'sp_macdonalds_budget'

ls_sql = "execute " + ls_spname + " ( " + ls_Budget_ID+ "," + ls_Status_ID + "," + ls_Resp_Person + ")"

execute immediate :ls_sql using sqlca;

Hope this will work but I am not sure how you can get return value. Instead pass one value as reference i mean as out parameter and i think then you will be able to get that value once this statement is executed.

Regards,
Sandeep
0
 
michaelstoffelCommented:
You should look up Dynamic SQL Format 3 in the PowerBuilder help.  It has exactly what you want with examples.  One example is as follows:

DECLARE my_proc DYNAMIC PROCEDURE FOR SQLSA ;
integer Emp_id_var
string Emp_state_var

PREPARE SQLSA FROM "emp_select @stateparm=?" ;
// emp_select is a stored procedure

// Set variable to pass to SP
Emp_state_var = "MA"

EXECUTE DYNAMIC my_proc USING :Emp_state_var ;
FETCH my_proc INTO :Emp_id_var ;
CLOSE my_proc ;

Mike
0

Featured Post

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now