Link to home
Create AccountLog in
Avatar of fgo36
fgo36Flag for Belgium

asked on

Example of return value for Visual Basic stored functions

I have a Mysql 5.1.46 stored function which is working well in Mysql and C# applications.
I want to use it in Visual Basic and it cann't work (use of ODBC 5.1.8)
The function,
Mystr = Get_sw("123456", "prune")
takes 2 varchar parameters and must return a Varchar
I configure the parameters like this

With My_Obj_DB
    .Queries ("Get_sw")
    .DefParameter adParamReturnValue, strMsg, adVarChar
    .DefParameter adParamInput, "123456", adVarChar
    .DefParameter adParamInput, "prune", adVarChar
    .ExecuteQueries
End With

and the following error is returned
"Error in SQL syntax"
near
'_latin1"= call Get_sw(_latin1'123456',_latin1" at line 1

How can I do to solve this error
Thank's
Avatar of Robberbaron (robr)
Robberbaron (robr)
Flag of Australia image

have you tried using ADODB . this wraps the conenction in a newer interface  http://dev.mysql.com/doc/refman/5.0/fr/ado--rs-addnew.html

then the calls to the db are a little different.  for my use, the result is returned as a recordset,
Dim bRevOK As Boolean
    
    Dim cn_Doc As ADODB.Connection, cmd_Doc As ADODB.Command
    Dim rs_Rev As ADODB.Recordset
    Set cn_Doc = New ADODB.Connection
    Set cmd_Doc = New ADODB.Command
    Set rs_Rev = New ADODB.Recordset
    
    cn_Doc.ConnectionTimeout = IDoc_conn_ConnectionTimeout
    cn_Doc.CommandTimeout = IDoc_conn_CommandTimeout
    cn_Doc.CursorLocation = IDoc_conn_CursorLocation
    cn_Doc.Open IDoc_Conn_ConnectionString, IDoc_conn_RuntimeUserName, IDoc_conn_RuntimePassword
    
    cmd_Doc.ActiveConnection = cn_Doc
    cmd_Doc.CommandType = adCmdText
         
    ClearCommandParameters cmd_Doc
    cmd_Doc.CommandType = adCmdStoredProc
    
    rs_Rev.CursorType = adOpenForwardOnly  'adOpenStatic
    rs_Rev.CursorLocation = adUseClient
    rs_Rev.LockType = adLockReadOnly
    cmd_Doc.Parameters.Append cmd_Doc.CreateParameter("ProjectId", adVarChar, adParamInput, 10, sProjId)
    cmd_Doc.Parameters.Append cmd_Doc.CreateParameter("DocNo", adInteger, adParamInput, 4, Null)
    cmd_Doc.Parameters.Append cmd_Doc.CreateParameter("DocId", adVarChar, adParamInput, 50, sDocId)
    cmd_Doc.Parameters.Append cmd_Doc.CreateParameter("TestRev", adVarChar, adParamInput, 5, sRevNew)
        
    'now get the actual documents
    cmd_Doc.CommandText = "GetProjDocsLatest"  'name of stored proceedure
    Set rs_Rev = cmd_Doc.Execute
    ClearCommandParameters cmd_Doc
    
    bRevOK = False

Open in new window

Avatar of fgo36

ASKER

I have no problem for using a Stored procedure, I just need to have a response with a stored function.
I have try many possiblities but no correct answer, always error.

Hereafter my last try

' Server Win 2003 32Bits, Client Win XP SP 3
' Mysql 5.1.46,
' ODBC Connector 5.1.8
' ConnectionString
' DRIVER={MySql ODBC 5.1 Driver};SERVER=sv;DATABASE=db;UID=us;PWD=pw;CHARSET=UTF8;OPTION=272646155

Set Cn = New ADODB.Connection
Set lCmd = New ADODB.Command
Cn.CursorLocation = adUseClient
Cn.ConnectionString = ConnectionString
Cn.Open
   
lCmd.ActiveConnection = Cn
With lCmd
    .CommandType = adCmdStoredProc
    .CommandText = "get_embedded_software_version"
    .Parameters.append .CreateParameter("@result", adVarChar, adParamReturnValue, 40)
    .Parameters.append .CreateParameter("@par_event_name", adVarChar, adParamInput, 100)
    .Parameters.append .CreateParameter("@par_event_software", adVarChar, adParamInput, 100)
    .Parameters.Refresh
End With
   
lCmd(1) = "3"
lCmd(2) = "eagle"

lCmd.Execute Options:=adExecuteNoRecords

' ERROR: [MySQL][ODBC 5.1 Driver][mysqld-5.1.46-community]You have an error in your SQL syntax;
'   check the manual that corresponds to your MySQL server version for the right syntax to use
'   near ''' = call get_embedded_software_version('3', 'eagle')' at line 1

it looks ok generally.

http://www.codeguru.com/forum/archive/index.php/t-182960.html is similar but doesnt use @result (ie just "result")

this appears to be the problem as MySQL seems to be trying to assign the result of the call to nothing.
'' = call get_embedded_software_version('3', 'eagle')

the SP does return a varchar ?   should it be char(8) or numeric ?  (unlikely)

Possible bug but this was v3.5?? see http://bugs.mysql.com/bug.php?id=15635
http://forums.mysql.com/read.php?98,123762,123824#msg-123824
http://forums.mysql.com/read.php?98,373032,373057#msg-373057
Avatar of fgo36

ASKER

My problem is the same, the .commandText is = to { ? = call Get_emxxx(?, ?) } just before execute.

I tried with different parameters without succcess.
I use stored proc in the same program with no error but I want to use stored func.

Here a simple SQL function that I try, with no success
SHOW WARNINGS;
USE `car`;
-- --------------------------------------------------------------------------------
-- Routine DDL
-- --------------------------------------------------------------------------------
DELIMITER $$

CREATE DEFINER=`root`@`localhost` FUNCTION `Get_param`(
  par_event_name varchar(20)
) RETURNS varchar(100) CHARSET latin1
    DETERMINISTIC
BEGIN

  DECLARE result varchar(100) DEFAULT '';

  SET result = '*NOT_EXIST';

  SELECT description INTO result FROM customers
        WHERE name = par_event_name;
 
  RETURN result;

END

ASKER CERTIFIED SOLUTION
Avatar of fgo36
fgo36
Flag of Belgium image

Link to home
membership
Create a free account to see this answer
Signing up is free and takes 30 seconds. No credit card required.
See answer
Avatar of fgo36

ASKER

I found solution myself