1. Get rid of all advertisements and get unlimited access to documents by upgrading to Premium Membership. Upgrade to Premium Now and also get a Premium Badge!

package and procedures

Discussion in 'SQL PL/SQL' started by sravan.kaurn, Dec 17, 2014.

  1. sravan.kaurn

    sravan.kaurn Active Member

    Messages:
    13
    Likes Received:
    0
    Trophy Points:
    100
    hi frnds,

    i know the meaning of package and procedure and coding structure for procedure and package but in which scenario we will use procedure and which scenario we will go for package?
    can anyone explain with real time example

    thanks in advance...
     
  2. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    Imagine, if you will, going to the grocery store because you need some item in order to make dinner. Then, at checkout, you put your item in a bag (or not). That is indicative of a procedure.

    Now imagine you go to store on a weekend to buy the needed items for the week. At checkout, most people put all their vegetables in one bag, dairy in another, meats in a separate bag, household cleaners in their own bag, and so forth...each of the bags are packages of like products. You could also package the package...i.e. all the bags of groceries are transported in the trunk of the car.

    Essentially, it's entirely up to you. Packages are just a convenient way to group 'stuff' as you see fit.

    HTH

    CJ
     
  3. jagadekara

    jagadekara Forum Guru

    Messages:
    1,126
    Likes Received:
    145
    Trophy Points:
    1,805
    Location:
    Pulivendula
    Wove! Nice Example...
     
  4. yowancristo

    yowancristo Forum Advisor

    Messages:
    103
    Likes Received:
    10
    Trophy Points:
    305
    Location:
    Bangalore
    Nicely compared:)

    The packages are providing options to implement concepts of Object Oriented Programming style in pl/sql. A simple example for Data Hiding and Encapsulation is you can define a procedure in the package body which is not declared in spec (private procedures), Now the procedure is visible and can be accessed only inside this package and cannot be accessed from any other procedure/package -
     
  5. ocprep

    ocprep Forum Advisor

    Messages:
    277
    Likes Received:
    76
    Trophy Points:
    410
    Location:
    Orlando, Florida
    The groceries concept is interesting... but misses a fair portion of the reasoning behind grouping code together in a package. Two procedures may have very little in common and may not even reference each other, but should be stored in a package because there are parts of the package instantiation that both of them need. Packages allow for global variables and constants that can be easily shared among all the subprograms that reside in them.

    Packages also help to organize your code. Over time a database is quite likely to accumulate hundreds, possibly even thousands of stored procedures and functions. If you don't have them well organized, keeping track of them can become a nightmare. Packages work well for this... so long as the packages themselves are well organized. If you make a package that is a grab-bag of unrelated subprograms... this is not improving organization. It's just creating the equivalent of that junk drawer in your kitchen where you throw everything that doesn't have a place to be.
     
  6. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    Valid points all...it was only intended as an abstract example.
     
  7. ocprep

    ocprep Forum Advisor

    Messages:
    277
    Likes Received:
    76
    Trophy Points:
    410
    Location:
    Orlando, Florida
    DTSIGuy:

    I was not slamming the example. The example plus two 'Attaboys' could have left the OP thinking that the only reason you group subprograms into a package is when they are similar and that you never group dissimilar subprograms into a package. Just wanted to make sure the OP was aware that there are other reasons that packages make sense.

    I'll even borrow (steal) your analogy... you might be purchasing items from the store to take to the park for a picnic. Therefore you create a single bag that has fried chicken, dinner rolls, paper towels, potato salad, some fruit, and bottled water. Those items have little in common except that they are all intended to work together for a single purpose.
     
  8. DTSIGuy

    DTSIGuy Forum Advisor

    Messages:
    402
    Likes Received:
    58
    Trophy Points:
    410
    Location:
    Texas
    Never figured it as a slam...and you brought up good points. ;-)

    CJ