Results 1 to 2 of 2
  1. #1
    Join Date
    Feb 2004
    Posts
    134

    Unanswered: User Defined Function Help?

    I create the following UDF just for learning purposes and I cannot seem to get it to return a value.

    Stored Procedure
    Code:
    REATE PROCEDURE usp_GetXSection
    
    @fWidth float,
    @fHeight float,
    @fResult float OUTPUT
    
    AS
    BEGIN
    set @fResult = [dbo].[XSECTION](@fWidth, @fHeight)
    END
    GO
    Ane the following UDF

    Code:
    CREATE FUNCTION XSECTION
    (@Width float, @Height float)
    RETURNS float
    AS
    BEGIN
    RETURN (@Width * @Height)
    END
    I call it using ADO in a c++ application, but all returns empty

    Any ideas? C++ code to follow.

    Mike B

    Code:
    void CFormTemplateSetup::OnCalculate() 
    {
    	_CommandPtr pCmd(__uuidof(Command));
    	_ParameterPtr pWidth(__uuidof(Parameter));
    	_ParameterPtr pHeight(__uuidof(Parameter));
    	_ParameterPtr pResult(__uuidof(Parameter));
    
    	try
    	{
    		pCmd->ActiveConnection = GetDocument()->GetConnection();
    		pCmd->CommandText = _T("usp_GetXSection");
    		pCmd->CommandType = adCmdStoredProc;
    
    		_variant_t vWidth, vHeight;
    		vWidth.vt = VT_R4;
    		vHeight.vt = VT_R4;
    
    		vWidth.fltVal = 10;
    		vHeight.fltVal = 10;
    
    		pWidth = pCmd->CreateParameter(_T("@fWidth"), adInteger, adParamInput, sizeof(int), vWidth);
    		pHeight = pCmd->CreateParameter(_T("@fHeight"), adInteger, adParamInput, sizeof(int), vHeight);
    		pResult = pCmd->CreateParameter(_T("@fResult"), adDouble, adParamOutput, sizeof(float), vtMissing);
    
    		pCmd->Parameters->Append(pWidth);
    		pCmd->Parameters->Append(pHeight);
    		pCmd->Parameters->Append(pResult);
    
    		_variant_t vResult = pResult->Value;
    
    		float fArea = pResult->Value.fltVal;
    		CString csArea;
    		csArea.Format("Area = %d", fArea);
    
    		::AfxMessageBox(csArea);
    	}
    	catch(_com_error& e)
    	{
    		::AfxMessageBox(e.Description());
    	}
    }

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    You need to SELECT the value in your stored procedure instead of setting a T-SQL variable equat to it to retrieve it using the C++ code in your example.

    -PatP

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •