Monday, May 26, 2014

oracle stored procedure select template return cursor with optional Parameters

Description :
i need to create Stored Procedure select from table with optional parameters to filter and return cursor.
then receive cursor using vb.net function and mapping data to data table to check data existence.

Stored Procedure Template
Stored Specification (with Package Declaration)

PACKAGE     USER_COMPANY IS
 TYPE GET_CO_INFO_CUR IS REF CURSOR ;
PROCEDURE   GET_COMPANY_DATE  -------------                            
                       (VGENERAL_ON    IN    NUMBER  DEFAULT 0       ,
                        VSEGIL_NO      IN    VARCHAR2 DEFAULT NULL    ,                                            
                        VGET_DATE      OUT   GET_DATE,
                        VEXCEPTION  OUT NUMBER);
END;  -- end Package Declaration
---------------------------------------------------------
Stored Body(with Package Body)

PACKAGE BODY   USER_COMPANY IS
PROCEDURE   GET_COMPANY_DATE  --------------                                            
                       (VGENERAL_ON    IN    NUMBER  DEFAULT 0       ,
                        VSEGIL_NO      IN    VARCHAR2 DEFAULT NULL    ,                     
                        VGET_DATE      OUT   GET_DATE,
                        VEXCEPTION  OUT NUMBER)  IS
BEGIN
     OPEN VGET_DATE  FOR          
      SELECT  GENERAL_ON, SEGIL_NO, EMAIL, PASS_WORD, MOBIL_NO, PHONE_NO, MNG_MOBIL_NO,USER_TYPE
      FROM    TND.TND_COMPANY_REGISTER 
      WHERE  (VGENERAL_ON = 0 OR GENERAL_ON = VGENERAL_ON)
      AND    (VSEGIL_NO   IS NULL OR  SEGIL_NO   = VSEGIL_NO );
EXCEPTION WHEN OTHERS  THEN
          VEXCEPTION := SQLCODE;      
    
END  GET_COMPANY_DATE;
END; --end Package Body
--------------------------------------------------------------------------------
Notes : if you passed two optional parameters so the stored procedure query will filter using two values
else if the number value is 0 or varchar2 value is null or empty string the query execution will ignore it in filter so you can use this template for search optionally for your input parameters
the other benefit is if you pass one parameter only for the procedure the execution will succeed because of default value binding option.

-----------------------------------------------------------------------
vb.net function to get cursor data

note : the template code using Oracle.DataAccess.Client library.

 Public Function Get_Company_Data(ByVal general_No As String, ByVal sejelno As String) As DataTable
        Dim mydt As New DataTable()
        Try
            Dim OConn As New OracleConnection
            OConn.ConnectionString = connect.Tender
            Dim cmd As New OracleCommand
            cmd.CommandType = Data.CommandType.StoredProcedure
            cmd.CommandText = "SchemaName.PackageName.ProcedureName"
            cmd.Connection = OConn
            cmd.BindByName = True
            Dim O_par_1, O_par_2, O_par_3, O_par_4 As OracleParameter
            O_par_1 = cmd.Parameters.Add("VGENERAL_ON", OracleDbType.Double, 22)
            O_par_2 = cmd.Parameters.Add("VSEGIL_NO", OracleDbType.Varchar2, 20)
            O_par_3 = cmd.Parameters.Add("VGET_DATE", OracleDbType.RefCursor)
            O_par_4 = cmd.Parameters.Add("VEXCEPTION", OracleDbType.Double, 20)
            O_par_1.Direction = Data.ParameterDirection.Input
            O_par_2.Direction = Data.ParameterDirection.Input
            O_par_3.Direction = Data.ParameterDirection.Output
            O_par_4.Direction = Data.ParameterDirection.Output

            O_par_1.Value = general_No
            O_par_2.Value = sejelno


            If OConn.State = ConnectionState.Closed Then
                OConn.Open()
            End If
            Dim adp As New OracleDataAdapter(cmd)

            adp.Fill(mydt)

            cmd.Connection.Close()
            OConn.Close()
            If mydt.Rows.Count > 0 Then
                Return mydt
            Else
                Return Nothing
            End If

        Catch ex As Exception
            Return mydt
        End Try
    End Function



No comments:

Post a Comment