Solved

Calling stored procedure dynamically from PB Script

Posted on 2006-11-28
4
4,762 Views
Last Modified: 2013-12-26
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
Comment
Question by:xsysys
4 Comments
 
LVL 14

Accepted Solution

by:
sandeep_patel earned 25 total points
ID: 18034858
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
 
LVL 5

Assisted Solution

by:michaelstoffel
michaelstoffel earned 25 total points
ID: 18042567
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

Programmer's Notepad is, one of the best free text editing tools available, simply because the developers appear to have second-guessed every weird problem or issue a programmer is likely to run into. One of these problems is selecting and deleti…
Update (December 2011): Since this article was published, the things have changed for good for Android native developers. The Sequoyah Project (http://www.eclipse.org/sequoyah/) automates most of the tasks discussed in this article. You can even fin…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now