Results 1 to 6 of 6

Thread: [RESOLVED] Call Oracle SP from VB

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    12

    Resolved [RESOLVED] Call Oracle SP from VB

    Please help...

    I'm new to Oracle and need to call a oracle stored procedure from vb. The stored procedure will eventually have many select statements, but for now I need to execute a simple select statements and display the results in VB. Can anyone assist as I'm not sure what to do next.

    Oracle
    Code:
    CREATE OR REPLACE PROCEDURE TEST
    AS
    BEGIN
      EXECUTE IMMEDIATE 'SELECT * FROM S_USERS';
    END;
    VB - Form
    VB Code:
    1. Private Sub Command1_Click()
    2.   Open_Connection
    3.  
    4.   SQL ("CALL TEST()")
    5.  
    6.   'SQL ("SELECT * FROM S_USERS")
    7.   'SQL ("EXECUTE TEST")
    8.  
    9.   MsgBox RsSQL.RecordCount
    10.  
    11.   Set Cn = Nothing
    12.   Set RsSQL = Nothing
    13. End Sub

    VB - Module
    VB Code:
    1. Option Explicit
    2.  
    3.      'Database connections
    4.      Public Cn As New ADODB.Connection
    5.  
    6.      'Record connections
    7.      Public RsSQL As New ADODB.Recordset
    8.  
    9.  
    10. Public Function Open_Connection() As Boolean
    11.   'Set Connection
    12.   Set Cn = New ADODB.Connection
    13.    
    14.   'Connection to Database
    15.   With Cn
    16.     .ConnectionTimeout = 5
    17.     .CommandTimeout = 5
    18.     '.ConnectionString = "Provider=SQLOLEDB.1;Data Source=***;Database=***;User ID=***;Password=***;Persist Security Info=True"
    19.     .ConnectionString = "Provider=OraOLEDB.Oracle.1;Data Source=***;Database=***;User ID=***;Password=***;Persist Security Info=True"
    20.    
    21.     .Open
    22.   End With
    23.  
    24.   'Connection succeded
    25.   Open_Connection = True
    26. End Function
    27.  
    28. Public Sub SQL(SQL As String)
    29.   'Set Recordset
    30.   Set RsSQL = New ADODB.Recordset
    31.    
    32.   'Record set SQL
    33.   With RsSQL
    34.     .CursorLocation = adUseClient
    35.    
    36.     .Open SQL, Cn, adOpenKeyset, adLockOptimistic
    37.   End With
    38. End Sub


    Using : Oracle 8i, VB6.0 SP5

    Thanks in advance
    Last edited by si_the_geek; Nov 7th, 2005 at 01:45 PM. Reason: added Code/VBCode tags

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Call Oracle SP from VB

    You are going to need parameters for your SP. If you are going to pass in the SQL statement you will need at least 2 paramters one is an In and the other is an Out paramter.

    Code:
    Create or Replace Procedure Test (
          pSQL IN Varchar2,
          pRet  OUT Ret_S_Users
    )
    Begin
    
    Open pRet For 
           Select * From S_USERS
    
    End;
    /
    The you will need to define Ret_S_Users as a Cursor somewhere in a package on the database.
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    12

    Re: Call Oracle SP from VB

    Thanks for the response.

    Here's what I have done so far...

    ------------------------
    CREATE OR REPLACE PACKAGE Ret_S_Users AS
    TYPE cur IS REF CURSOR;
    END Ret_S_Users;
    /

    -------------------
    CREATE OR REPLACE Procedure Test
    (
    pSQL IN Varchar2,
    pRet OUT Ret_S_Users.cur
    )
    IS
    Begin

    Open pRet For
    Select * From S_USERS;

    End;
    /

    What parameter's do I need to parse into Test and will I be able to EXECUTE the statements from TOAD with the following statement CALL TEST('???'); to test the sp.

  4. #4
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: Call Oracle SP from VB

    This is how I have always done this:

    Code:
    CREATE OR REPLACE PACKAGE cv_types AS
      
      
      /*  
        Record type for Platforms     
      */
      TYPE REC_s_users IS Record (  
       fname         VARCHAR2(15),
       lname          VARCHAR2(25)
      );
      TYPE CV_REC_s_users IS REF CURSOR RETURN REC_s_users;
      
    End cv_types;
    /
    
    
    CREATE OR REPLACE PROCEDURE SP_GET_S_USERS (
         psuers	OUT cv_types.CV_REC_PLAT_ITEMS2 )
              
    AS
    BEGIN
    
    	OPEN psuers FOR
        	SELECT  
         		fname,
          		lname,
      	FROM  
    		s_users
      	ORDER BY 
    		lname,fname;
    
      
    EXCEPTION 
      WHEN OTHERS THEN         
          ROLLBACK WORK;
          RAISE;
    END SP_GET_S_USERS;
    /
    if VB
    VB Code:
    1. Set adoCommand = New ADODB.Command
    2. With adoCommand
    3.     .ActiveConnection = mdlGeneral.gDBConn
    4.     .CommandType = adCmdStoredProc
    5.     .CommandText = "SP_GET_S_USERS"
    6. .Parameters.Append .CreateParameter(, adBSTR, adParamOutput, , rs)End With
    7. adoCommand.Execute

    I'm not sure of the RS value, do a search and there are examples of pulling the rs back from the procedure.

    I have always done my test in SQL*Plus. I have attached a word doc to this with an example of doing this.
    Attached Files Attached Files
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  5. #5

    Thread Starter
    New Member
    Join Date
    May 2004
    Posts
    12

    Resolved Re: Call Oracle SP from VB

    Thanks Gary for the help...
    your examples really helped a lot. Tested my stored proc in sqlplus and vb and it's working. !!!! COOL !!!!!

  6. #6
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Call Oracle SP from VB

    If you have found a resolution to this problem, please pull down the Thread Tools menu and click the Mark Thread Resolved button. That will let everyone know that you have your answer

Posting Permissions

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



Click Here to Expand Forum to Full Width