In software development, calling stored procedures is one of the common database operations. Oracle is a commonly used relational database management system, so when calling a stored procedure, you need to use the interface it provides to complete the process. This article will introduce how to use C language to call Oracle stored procedures.
1. Introduction to Oracle stored procedures
Oracle stored procedures are predefined blocks of code used to perform specific tasks or operations. After you create a stored procedure in the database, you can call it in your program and pass parameters and return values. The advantage of this is that stored procedures can be reused, while also improving the efficiency and security of the program.
2. Use C language to call Oracle stored procedures
Before using C language to call Oracle stored procedures, we need to ensure that the Oracle ODBC driver is installed to communicate with the Oracle database. Next, we will demonstrate how to call Oracle stored procedures using C language through the following steps:
1. Connect to the Oracle database
Before using the Oracle ODBC driver, we need to create a ODBC data source. ODBC data sources can be created through "Data Sources (ODBC)" in the "Control Panel" of the Windows operating system. You can also specify the data source name in the program, as follows:
SQLRETURN rc; SQLHDBC hdbc; SQLAllocHandle(SQL_HANDLE_DBC, SQL_NULL_HANDLE, &hdbc); rc = SQLConnect(hdbc, (SQLCHAR*)datasource_name, SQL_NTS, (SQLCHAR*)uid, SQL_NTS, (SQLCHAR*)pwd, SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { printf("connect to oracle failed "); return; }
2. Prepare SQL statements
Before calling the stored procedure, you need to prepare the SQL statement so that it can be passed to Database execution. The following example demonstrates how to prepare a simple SQL statement that executes a stored procedure named "get_employee":
SQLHSTMT hstmt; SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt); SQLCHAR* sql = (SQLCHAR*)"{call get_employee(?, ?, ?, ?)}"; SQLINTEGER param1 = 10, param2 = 3; SQLDOUBLE param3 = 0.0; SQLCHAR param4[20]; rc = SQLPrepare(hstmt, sql, SQL_NTS); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { printf("prepare sql failed "); return; } rc = SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, ¶m1, 0, NULL); rc = SQLBindParameter(hstmt, 2, SQL_PARAM_INPUT, SQL_C_LONG, SQL_INTEGER, 0, 0, ¶m2, 0, NULL); rc = SQLBindParameter(hstmt, 3, SQL_PARAM_OUTPUT, SQL_C_DOUBLE, SQL_DOUBLE, 0, 0, ¶m3, 0, NULL); rc = SQLBindParameter(hstmt, 4, SQL_PARAM_OUTPUT, SQL_C_CHAR, SQL_VARCHAR, 20, 0, param4, sizeof(param4), NULL);
In the above example, a SQL statement is prepared using the "SQLPrepare" function, And use the "SQLBindParameter" function to bind the parameter to the "?" placeholder of the statement. The first and second parameters of the stored procedure are input parameters respectively, and the third and fourth parameters are output parameters respectively.
3. Execute the SQL statement
After the SQL statement is prepared and the parameters are bound, we can use the "SQLExecute" function to execute the statement. When the stored procedure is executed, it automatically creates a cursor in which the value of the output parameter can be retrieved. The following is an example of executing a SQL statement:
rc = SQLExecute(hstmt); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO) { printf("execute sql failed "); return; } rc = SQLFetch(hstmt); if (rc != SQL_SUCCESS && rc != SQL_SUCCESS_WITH_INFO && rc != SQL_NO_DATA) { printf("fetch data failed "); return; }
In the above example, the SQL statement is executed through the "SQLExecute" function and the value of the output parameter is obtained in the "SQLFetch" function.
4. Disconnect from the Oracle database
After completing the call to the stored procedure, you need to use the "SQLDisconnect" function to disconnect from the Oracle database to release the allocated resources. The following example demonstrates how to disconnect from the Oracle database:
SQLDisconnect(hdbc); SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
Through the above steps, we can use C language to call Oracle stored procedures.
3. Summary
This article introduces how to use C language to call Oracle stored procedures, which mainly includes connecting to Oracle database, preparing SQL statements, executing SQL statements and disconnecting from Oracle database, etc. step. In actual development, modifications and adjustments can be made according to specific needs. Through the introduction of this article, I believe that you have a certain understanding and understanding of using C language to call Oracle stored procedures.
The above is the detailed content of c calls oracle stored procedure. For more information, please follow other related articles on the PHP Chinese website!