Stored Procedure

Stored Procedure

A stored procedure is small program that runs in the database. It can be called from any programming language. As it is in the database, it can be updated easily. The example shown below illustrates how to create a stored procedure to add two numbers. In the same way, the program illustrates how to call this stored procedure. See Publishing a Web Site > Public SQL web site to learn how to setup an user account in the database so that an user can execute store procedures.

Problem 1
Create a program called SpTest to call a stored procedure from a database. Observe that each input or output parameter in the stored procedure is represented by a question mark. In this case, there are three parameters (in_x, in_y, out_z), therefore there are three question marks.


@in_x DECIMAL(8, 3),
@in_y DECIMAL(8, 3),
@out_z DECIMAL(8, 3) OUTPUT
SET @out_z = @in_x + @in_y;

void SpTest:: btOK_Click(Win::Event& e)
     Sql::SqlConnection conn;
          //conn.OpenSession(DSN, USERNAME, PASSWORD); //Control Panel>Administrative Tools>Data Sources (ODBC)>Create dsn_myDatabase
          conn.OpenSession(hWnd, CONNECTION_STRING);
          double x = 10.8;
          double y =15.3;
          double z;
          conn.BindInputParameter(1, x);
          conn.BindInputParameter(2, y);
          conn.BindOutputParameter(3, z);
          int returnValue = conn.ExecDirect(L"{call proc_suma(?, ?, ?)}"); //A question mark for each param
          this->MessageBox(Sys::Convert::ToString(z), L"The summation is ", MB_OK); //returnValue must be 1
     catch (Sql::SqlException e)
          this->MessageBox(e.GetDescription(), L"Error", MB_OK | MB_ICONERROR);

© Copyright 2000-2019 Wintempla selo. All Rights Reserved. Sep 05 2019. Home