Bonjour,
Je viens vers vous car j'ai besoin d'exécuter des procédures stockées et des fonctions stockées Oracle depuis du code VBA.
J'ai déjà un module de classe de connexion dans lequel j'ai déjà créé une méthode d'exécution de procédures stockée qui fonctionne. Celle-ci prend en paramètre le nom de la procédure et un tableau de paramètres. Ce paramètre est un type que j'ai créé :
J'utilise une connexion que j'ai initialisée plus tôt et qui se nomme "conn" dans le code. Voici la le code qui éxecute la procédure stockée :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6 Public Type type_ParamStoredProc str_nameParam As String 'Nom du paramètre str_value As String 'Valeur du paramètre theDataType As DataTypeEnum 'Type du paramètre typeSize As Integer 'Taille du type (dans le cas varchar par exemple) End Type
Maintenant, ce que je souhaite, c'est faire la même chose mais pour une fonction stockée, et je souhaite récupérer la valeur de retour. Mais je n'y arrive pas
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41 Public Sub ExecuteStoredProcedure(str_ProcName As String, tabParam() As type_ParamStoredProc) Dim cmdToExecute As ADODB.Command Dim i As Integer Dim theParam As ADODB.Parameter On Error GoTo gestErr Set cmdToExecute = New ADODB.Command 'Initialisation de la commande via la connection en cours If Not isConnected Then connect End If Set cmdToExecute.ActiveConnection = conn cmdToExecute.CommandType = adCmdStoredProc cmdToExecute.CommandText = str_ProcName For i = 0 To UBound(tabParam) - 1 If tabParam(i).typeSize <> 0 Then Set theParam = cmdToExecute.CreateParameter(tabParam(i).str_nameParam, tabParam(i).theDataType, adParamInput, tabParam(i).typeSize) Else Set theParam = cmdToExecute.CreateParameter(tabParam(i).str_nameParam, tabParam(i).theDataType, adParamInput) End If cmdToExecute.Parameters.Append theParam theParam.Value = tabParam(i).str_value Next i cmdToExecute.Execute Set cmdToExecute = Nothing Set theParam = Nothing Exit Sub gestErr: LogError Err.Number, Err.Description, "ExecuteStoredProcedure" Err.Raise Err.Number, , Err.Description End Sub
Voilà ce que j'ai fait (en fait, j'ai ajouté un paramètre qui est la valeur de retour) :
Si quelqu'un à déja fait ça, merci de me dire ce qui ne va pas la dedans =)
Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55 Public Sub ExecuteStoredFunction(str_ProcName As String, tabParam() As type_ParamStoredProc, ByRef paramReturn As type_ParamStoredProc) Dim cmdToExecute As ADODB.Command Dim i As Integer Dim theParam As ADODB.Parameter Dim returnvalue As ADODB.Parameter On Error GoTo gestErr Set cmdToExecute = New ADODB.Command 'Initialisation de la commande via la connection en cours If Not isConnected Then connect End If Set cmdToExecute.ActiveConnection = conn cmdToExecute.CommandType = adCmdUnknown 'ICI JE NE SAIS PAS QUOI METTRE (toujours adCmdStoredProcedure ?) cmdToExecute.CommandText = str_ProcName For i = 0 To UBound(tabParam) - 1 If tabParam(i).typeSize <> 0 Then Set theParam = cmdToExecute.CreateParameter(tabParam(i).str_nameParam, tabParam(i).theDataType, adParamInput, tabParam(i).typeSize) Else Set theParam = cmdToExecute.CreateParameter(tabParam(i).str_nameParam, tabParam(i).theDataType, adParamInput) End If cmdToExecute.Parameters.Append theParam theParam.Value = tabParam(i).str_value Next i ' Valeur de retour Set returnvalue = cmdToExecute.CreateParameter(, paramReturn.theDataType, adParamReturnValue) cmdToExecute.Parameters.Append returnvalue cmdToExecute.Execute paramReturn.str_value = returnvalue.Value Set cmdToExecute = Nothing Set theParam = Nothing Set returnvalue = Nothing Exit Sub gestErr: Debug.Print Err.Description LogError Err.Number, Err.Description,"ExecuteStoredFunction" Err.Raise Err.Number, , Err.Description End Sub
Partager