liwei_ying 2015-06-26 03:28 采纳率: 0%
浏览 675

ODBC 存储过程declare @returnFlag varchar(5)返回获取

create proc sp_ValidateAdminCard
@param1 varchar(50) ---管理卡号

as
begin
declare @returnFlag varchar(5)

if exists(select top 1 C_CardNO from TB_AdminCard (nolock) where C_CardNO=@param1 and State=1)
begin
    set @returnFlag='1'
end
else 
begin
    set @returnFlag='0'
end

select @returnFlag

end

用的SQLExecDirect

  • 写回答

1条回答

  • ChatGPTᴼᴾᴱᴺᴬᴵ 2023-05-27 15:14
    关注

    该回答引用ChatGPT-3.5,仅供参考,不保证完全正确

    使用ODBC的SQLExecDirect函数执行存储过程可以通过以下步骤完成:


    首先,建立ODBC连接并分配所需的环境和连接句柄。

    SQLHENV henv;  // Environment handle
    SQLHDBC hdbc;  // Connection handle
    SQLHSTMT hstmt;  // Statement handle
    
    // Allocate environment handle
    SQLAllocHandle(SQL_HANDLE_ENV, SQL_NULL_HANDLE, &henv);
    
    // Set the ODBC version
    SQLSetEnvAttr(henv, SQL_ATTR_ODBC_VERSION, (SQLPOINTER)SQL_OV_ODBC3, 0);
    
    // Allocate connection handle
    SQLAllocHandle(SQL_HANDLE_DBC, henv, &hdbc);
    
    // Connect to the database
    SQLDriverConnect(hdbc, NULL, (SQLWCHAR*)L"your_connection_string", SQL_NTS, NULL, 0, NULL, SQL_DRIVER_COMPLETE);
    
    // Allocate statement handle
    SQLAllocHandle(SQL_HANDLE_STMT, hdbc, &hstmt);
    

    接下来,构造SQL语句,并使用SQLExecDirect函数执行存储过程。

    // Declare variables for input/output parameters
    SQLCHAR param1[51];  // Assuming maximum length of 50 characters
    SQLLEN param1Length;
    SQLCHAR returnFlag[6];  // Assuming maximum length of 5 characters
    SQLLEN returnFlagLength;
    
    // Set the input parameter value
    strcpy_s((char*)param1, sizeof(param1), "your_admin_card_number");
    param1Length = SQL_NTS;
    
    // Prepare the SQL statement with the stored procedure call
    const SQLCHAR* sqlStatement = (SQLCHAR*)"EXEC sp_ValidateAdminCard ?";
    SQLPrepare(hstmt, sqlStatement, SQL_NTS);
    
    // Bind the input parameter
    SQLBindParameter(hstmt, 1, SQL_PARAM_INPUT, SQL_C_CHAR, SQL_VARCHAR, sizeof(param1), 0, param1, sizeof(param1), &param1Length);
    
    // Execute the statement
    SQLExecDirect(hstmt, sqlStatement, SQL_NTS);
    

    最后,获取存储过程的返回值。

    // Bind the output parameter
    SQLBindCol(hstmt, 1, SQL_C_CHAR, returnFlag, sizeof(returnFlag), &returnFlagLength);
    
    // Fetch the result set (in this case, a single row)
    SQLFetch(hstmt);
    
    // Get the value of the returnFlag
    std::string returnFlagValue((char*)returnFlag, returnFlagLength);
    

    完成后,记得释放分配的句柄和关闭连接。

    // Free statement handle
    SQLFreeHandle(SQL_HANDLE_STMT, hstmt);
    
    // Disconnect from the database
    SQLDisconnect(hdbc);
    
    // Free connection handle
    SQLFreeHandle(SQL_HANDLE_DBC, hdbc);
    
    // Free environment handle
    SQLFreeHandle(SQL_HANDLE_ENV, henv);
    

    这样,你就可以使用ODBC的SQLExecDirect函数执行存储过程,并获取返回的结果。请确保替换示例代码中的连接字符串和输入参数的值以适应你的实际情况。

    评论

报告相同问题?

悬赏问题

  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上
  • ¥15 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常