Solved

Calling stored procedure dynamically from PB Script

Posted on 2006-11-28
4
4,873 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
How to install Selenium IDE and loops for quick automated testing. Get Selenium IDE from http://seleniumhq.org Go to that link and select download selenium in the right hand column That will then direct you to their download page. From that p…
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 NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.

627 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