Advertisement
Guest User

Untitled

a guest
Nov 27th, 2014
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.51 KB | None | 0 0
  1. (RESULT) NUMBER
  2. P1 VARCHAR2 IN
  3. P2 VARCHAR2 IN
  4. P3 VARCHAR2 IN
  5. P4 VARCHAR2 OUT
  6.  
  7. q := TADOQuery.Create(nil);
  8. q.Connection := conn;
  9. q.SQL.Add('BEGIN');
  10. q.SQL.Add(' SELECT GetData(:IN_1,:IN_2,:IN_3,:OUT_1) into :OUT_2 from dual;');
  11. q.SQL.Add('END;');
  12.  
  13. q.Parameters.ParamByName('IN_1').DataType:=ftString;
  14. q.Parameters.ParamByName('IN_1').Direction:=pdInput;
  15. q.Parameters.ParamByName('IN_1').Size:=3;
  16. q.Parameters.ParamByName('IN_1').Value:='001';
  17.  
  18. q.Parameters.ParamByName('IN_2').DataType:=ftString;
  19. q.Parameters.ParamByName('IN_2').Direction:=pdInput;
  20. q.Parameters.ParamByName('IN_2').Size:=15;
  21. q.Parameters.ParamByName('IN_2').Value:='88000000000';
  22.  
  23. q.Parameters.ParamByName('IN_3').DataType:=ftString;
  24. q.Parameters.ParamByName('IN_3').Direction:=pdInput;
  25. q.Parameters.ParamByName('IN_3').Size:=64;
  26. q.Parameters.ParamByName('IN_3').Value:='';
  27.  
  28. q.Parameters.ParamByName('OUT_1').DataType:=ftString;
  29. q.Parameters.ParamByName('OUT_1').Direction:=pdOutput;
  30. q.Parameters.ParamByName('OUT_1').Size:=255;
  31. q.Parameters.ParamByName('OUT_1').Value:='';
  32.  
  33. q.Parameters.ParamByName('OUT_2').DataType:=ftInteger;
  34. q.Parameters.ParamByName('OUT_2').Direction:=pdOutput;
  35. q.Parameters.ParamByName('OUT_2').Value:='0';
  36.  
  37. q.Open;
  38.  
  39. responseEdit.Text:=q.Parameters.ParamByName('OUT_1').Value;
  40.  
  41. CallableStatement cs = conn.prepareCall("{ call ? := GetData(?,?,?,?)}");
  42.  
  43. q.SQL.Add('BEGIN');
  44. q.SQL.Add(' :OUT_2 := GetData(:IN_1,:IN_2,:IN_3,:OUT_1);');
  45. q.SQL.Add('END;');
  46.  
  47. stp := TADOStoredProc.Create(nil);
  48. stp.Connection := conn;
  49. //stp.ProcedureName:='GetData'; //also tried this
  50. stp.ProcedureName:=':OUT_2 := GetData(:IN_1,:IN_2,:IN_3,:OUT_1)';
  51. stp.Parameters.CreateParameter('OUT_2',ftInteger,pdOutput,4,0);
  52. stp.Parameters.CreateParameter('IN_1',ftString,pdInput,3,'101');
  53. stp.Parameters.CreateParameter('IN_2',ftString,pdInput,15,phoneEdit.Text);
  54. stp.Parameters.CreateParameter('IN_3',ftString,pdInput,64,' ');
  55. stp.Parameters.CreateParameter('OUT_1',ftString,pdOutput,255,' ');
  56. stp.ExecProc;
  57.  
  58. cmd := TADOCommand.Create(nil);
  59. cmd.Connection := conn;
  60. cmd.CommandType := cmdStoredProc;
  61. cmd.CommandText := ' :OUT_2 := GetData(:IN_1,:IN_2,:IN_3,:OUT_1); ';
  62. ...
  63.  
  64. CREATE OR REPLACE FUNCTION ADMIN.GETDATA(IN_1 IN VARCHAR2,
  65. IN_2 IN VARCHAR2,
  66. IN_3 IN VARCHAR2,
  67. OUT_1 IN OUT VARCHAR2) RETURN INTEGER
  68. IS
  69. BEGIN
  70. OUT_1 := IN_1 || IN_2 || IN_3;
  71. RETURN 5;
  72. END;
  73.  
  74. BEGIN
  75. DECLARE OUTVAR VARCHAR(255);
  76. RETVAR INTEGER;
  77. BEGIN
  78. RETVAR:= ADMIN.GETDATA('A','B','C', OUTVAR);
  79. DBMS_OUTPUT.PUT_LINE(OUTVAR);
  80. DBMS_OUTPUT.PUT_LINE(RETVAR);
  81. END;
  82. END;
  83.  
  84. ABC
  85. 5
  86.  
  87. var Proc: TADOStoredProc;
  88. P: TParameter;
  89. Results: String;
  90. begin
  91. Proc := TADOStoredProc.Create(nil);
  92. try
  93. (* Set up the connection to an Oracle database *)
  94. Proc.Connection := MyADOConnection;
  95.  
  96. (* Define the function Name *)
  97. Proc.ProcedureName := 'GETDATA';
  98.  
  99. (* Let the FrameWork retrieve all the parameters from DataBase *)
  100. //Proc.Parameters.Refresh;
  101. //Set parameters values
  102. //Proc.Parameters.ParamByName('IN_1').Value := 'A';
  103. //Proc.Parameters.ParamByName('IN_2').Value := 'B';
  104. //Proc.Parameters.ParamByName('IN_3').Value := 'C';
  105. //Proc.Parameters.ParamByName('OUT1').Value := ''; //This will be overrided by the database.
  106.  
  107. // **********************
  108. // OR
  109. // **********************
  110.  
  111. //Define it manually!
  112. begin
  113. //Defining the Return Value
  114. Proc.Parameters.CreateParameter('RETVAL', ftBCD (* or ftInteger *), pdReturnValue, 0, Unassigned);
  115.  
  116. //Defining Input parameters 1, 2 and 3;
  117.  
  118. Proc.Parameters.CreateParameter('IN1', ftString , pdInput, 4000 (* This is the Max *), 'A');
  119.  
  120. //Collect the reference to update later
  121. P := Proc.Parameters.CreateParameter('IN2', ftString , pdInput, 4000 (* Length for a *), Unassigned);
  122.  
  123. Proc.Parameters.CreateParameter('IN3', ftString , pdInput, 4000 (* ftString param *), 'C');
  124.  
  125. Proc.Parameters.CreateParameter('OUT1', ftString , pdOutput, 4000, Unassigned);
  126.  
  127. P.Value := 'B';
  128.  
  129. Proc.ExecProc;
  130.  
  131. // Expected : [Return Value : 5]~[Out Var: ABC]
  132. Results := Format('[Return Value : %s]~[Out Var: %s]',
  133. [VarToStr(Proc.Parameters.ParamByName('RETVAL').Value),
  134. VarToStr(Proc.Parameters.ParamByName('OUT1').Value)]);
  135. Proc.Close;
  136.  
  137. // ACTUAL : [Return Value : 5]~[Out Var: ABC]
  138. ShowMessage(Results);
  139.  
  140.  
  141. //IT WORKS!!
  142. end;
  143. finally
  144. FreeAndNil(Proc);
  145. end;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement