Want To Search Something Else? Just Google It !

Wednesday, September 28, 2011

PL/SQL Function and Procedure

Stored Procedure:-

A Stored procedure is named PL/SQL block which perform one or more specific task.A procedure has a header and a body. The header consists of the name of the procedure and the parameters or variables passed to the procedure. The body consists or declaration section, execution section and exception section similar to a general PL/SQL Block. 

We can pass parameters to procedures in three ways.
1) IN-parameters
2) OUT-parameters
3) IN OUT-parameters
 
Function:-

A function is a named PL/SQL Block which is similar to a procedure.The major difference between a procedure and a function is, a function must always return a value, but a procedure may or may not return a value.  

You can call your function in multiple ways like following:-

1) Since a function returns a value we can assign it to a variable.
   employee_name :=  employer_details_func;
2) As a part of a SELECT statement
  SELECT employer_details_func FROM dual; 
3) In a PL/SQL Statements like,
  dbms_output.put_line(employer_details_func);
    This line displays the value returned by the function.

Difference between function and procedure

1. Function is mainly used in the case where it must return a value. Where as a procedure may or may not return a value or may return more than one value using the OUT parameter.

2. Function can be called from SQL statements where as procedure can not be called from the sql statements

3. Functions are normally used for computations where as procedures are normally used for executing business logic.

4. You can have DML (insert,update, delete) statements in a function. But, you cannot call such a function in a SQL query.

5. Function returns 1 value only. Procedure can return multiple values (max 1024).

6.Stored Procedure: supports deferred name resolution. Example while writing a stored procedure that uses table named table1 and table2 etc..but actually not exists in database is allowed only in during creation but runtime throws error Function wont support deferred name resolution.

7.Stored procedure returns always integer value by default zero. where as function return type could be scalar or table or table values

8. Stored procedure is precompiled execution plan where as functions are not.

9.A procedure may modify an object where a function can only return a value The RETURN statement immediately completes the execution of a subprogram and returns control to the caller

Advantages and Comparison of Stored Procedure/Function with normal SQL:-

Overhead: Stored procedure statements are stored directly in the database, they may remove all or part of the compilation overhead that is typically required in situations where software applications send inline (dynamic) SQL queries to a database.
 In addition, while they avoid some overhead, pre-compiled SQL statements add to the complexity of creating an optimal execution plan because not all arguments of the SQL statement are supplied at compile time. Depending on the specific database implementation and configuration, mixed performance results will be seen from stored procedures versus generic queries or user defined functions.

Avoidance of network traffic: A major advantage with stored procedures is that they can run directly within the database engine. The benefit here is that network communication costs can be avoided completely. This becomes particularly important for complex series of SQL statements.

Encapsulation of business logic: Stored procedures allow programmers to embed business logic an API in the database, which can simplify data management and reduce the need to encode the logic elsewhere in client programs. The database system can ensure data integrity and consistency with the help of stored procedures.

Delegation of access-rights: In many systems, stored procedures can be granted access rights to the database that users who execute those procedures do not directly have.

Some protection from SQL injection attacks: Stored procedures can be used to protect against injection attacks. Stored procedure parameters will be treated as data even if an attacker inserts SQL commands. Also, some DBMSs will check the parameter's type.

Disadvantage of procedure/functions:-

Vender Specific:-Stored procedure languages are quite often vendor-specific. Switching to use another vendor's database most likely requires rewriting any existing stored procedures.

Restricted Overloading Functionality:- Procedure and function overloading is available only within database packages or as PL/SQL sub-blocks. Database stored procedures cannot be overloaded. If you try to add a second overloaded stored procedure with the same procedure identifier, an error will result.

Disabled Procedure:-Database procedures can be accidentally dropped by a person with sufficient privilege. This is nothing new, except that there is no practical way of writing application code to detect inoperable procedure(s). Since SQL and PL/SQL are interpreted languages, an invalid or missing stored procedure is not detectable until module run-time. When the error is encountered, a confusing, cryptic database error results that is of very little help to the average application user.

Automatic dependency tracking:-When a simple DDL operation, such as adding a new column to an existing table, is performed, many stored procedures are marked as needing recompilation. What is not provided is the ability to recompile all these dependencies in a single action or to ascertain which procedures are, in fact, invalid. Other than compiling each object one at a time and checking its results, we have no way of determining the effect of a DDL modification.

48 comments:

  1. For Oracle Apps Technical,Functional,HRMS and SCM training with 100% placement assurance.Contact-9885489062

    ReplyDelete
  2. Hi,
    These are good questions to refresh your knowledge before an interview. A technical interview would also consist of practical SQL Interview Questions.

    ReplyDelete
  3. Nice article . Thank you for this beautiful content, Keep it up. Techavera is the best
    PLC SCADA Training in Noida.
    Visit us For Quality Learning.Thank you

    ReplyDelete
  4. Hi There,



    Love it absolutely! So crystalline. No mumbo jumbo. No non-sense. Straight and simple. You guys need a standing ovation for your good work.

    We received an email from the operations team that they are seeing frequent high CPU alerts on a particular server. These alerts occur around 5:20 AM EST . And, they are saying that high CPU is due to sqlserver process that runs at these times. My question is , how to find which sql server process was running at 5:20 AM EST? Any script or something like that.


    Excellent tutorials - very easy to understand with all the details. I hope you will continue to provide more such tutorials.


    Kind Regards,
    Irene Hynes

    ReplyDelete
  5. Hello Mate,


    The sense of praise that I have found for you after reading is overwhelming! Such a tremendous read!

    i need help

    I have 3 tables, the first 2 tables already have values, this is the output

    SQL> select * from tblhorario
    2 ;

    IDHORARIO DESCRIPCIONHORARIO
    ---------- -----------------------
    1 L-V Oficina
    2 L-S Oficina
    3 L-S Extendido
    4 L-S Completo
    5 247

    SQL> select * from tblciudad;

    IDCIUDAD NOMBRECIUDAD
    ---------- -----------------------
    1 Bogota
    2 Cali
    3 Medellin
    4 Bucaramanga
    5 Cartagena
    6 Barranquilla
    7 Cucuta

    7 rows selected.

    For the thir table i need to insert data using a mix of sources:

    First from the normal keyboard input, and second a select query that brings me the fileds DESCRIPCIONHORARIO and NOMBRECIUDAD using the respective ID´S from the other 2 tables. I'm an student and i need help with this.

    This is the original query

    INSERT INTO tblLocal(nombreLocal, direccionLocal, telefono1Local, telefono2Local, celularLocal, emailLocal, renta, idHorario, idCiudad)
    VALUES('UTadeoLozano', 'Cra 5 calle 22', '2756456', 'N/A', '3107770900', 'lutadeo@printing.com', 675000, 1, 1);

    The last numerical values at the and of the clause are meant to come from the two original tables.

    If the solution is a trigger or a stored procedure, please help me.







    By the way do you have any YouTube videos, would love to watch it. I would like to connect you on LinkedIn, great to have experts like you in my connection (In case, if you don’t have any issues).


    Best Regards,

    ReplyDelete
  6. Salve,

    Hot! That was HOT! Glued to the SQL and PL/SQL Interview Questions and Answers your proficiency and style!

    This is the table I want to transpose into rows:

    target_name tgl round(avg(round ((AVERAGE/10),3)),3)
    health 30-AUG-17 0.816
    health 31-AUG-17 0.814
    health 01-SEP-17 0.633
    health 02-SEP-17 0.662
    health 03-SEP-17 1.119
    health 05-SEP-17 13.939

    and select query is,

    select * from
    (
    select target_name, trunc(rollup_timestamp) tgl, round(avg(round ((AVERAGE/10),3)),3) from
    MGMT$METRIC_HOURLY
    where
    rollup_timestamp >= (trunc(rollup_timestamp)+8/24)
    and rollup_timestamp < (trunc(rollup_timestamp)+18/24)
    and rollup_timestamp between sysdate - 7 and sysdate
    and METRIC_COLUMN = 'cpuUtil' and target_type='host'
    and METRIC_NAME = 'Load'
    group by target_name,trunc(rollup_timestamp)
    union
    select target_name,trunc(collection_timestamp) tgl, (round(value,3))
    from MGMT$METRIC_current where
    METRIC_COLUMN = 'cpuUtil' and target_type='host' and METRIC_NAME = 'Load' ) where target_name = 'ehgexnp2db01.ehealth.gov.au'
    order by 1,2;

    Result needed as:

    target_name 30-AUG-17 31-AUG-17 01-SEP-17 02-SEP-17 03-SEP-17 05-SEP-17
    health 0.816 0.814 0.633 0.662 1.119 13.939

    Great effort, I wish I saw it earlier. Would have saved my day :)

    Gracias
    Radhey

    ReplyDelete
  7. Greetings Mate,


    So bloody thorough! Ah! So happy and blissed out! I feel redeemed by reading out SQL and PL/SQL Interview Questions and Answers . Keep up the good work!

    I have this requirement where I have to execute a sql query based on output of another query, something like given in example below :


    Code (Text):

    a=select to_char(sysdate,'DAY') from dual
    if a='SUNDAY' then
    select syadate from dual;
    else
    select sysdate-1 from dual;

    Explanation - If output of my first query is SUNDAY then execute Query1 else execute query2
    Any help is appreciated.


    I read multiple articles and watched many videos about how to use this tool - and was still confused! Your instructions were easy to understand and made the process simple.


    Kind Regards,
    Ajeeth

    ReplyDelete
  8. Wow it is really wonderful and awesome thus it is very much useful for me to understand many concepts and helped me a lot. it is really explainable very well and i got more information from your blog.Thanks for this great share. This post is much helpful for us.Excellent Blog! This is very helpful for who wants to learn professional Education.Thank you. Please keep Going. I am waiting for your more posts like this or related to any other informative topic.
    tibco training institute

    vmware online training

    ReplyDelete
  9. I have read your blog its very attractive and impressive. I like it your blog.

    appvn
    hotmail sign up login

    ReplyDelete
  10. Quickbooks Payroll Support Number welcome you 24*7 to access the various support services of Intuit products asking for help.

    ReplyDelete
  11. QuickBooks Customer Support Number research team is often prepared beforehand because of the the best option solutions that are of good help and less time intensive. Their pre-preparedness helps them extend their hundred percent support to all the entrepreneurs in addition to individual users of QuickBooks.

    ReplyDelete
  12. Intuit Enterprise Support Phone Number possible That When You Are Using QuickBooks And Encounter Some Errors Then Do Not Hyper Because QuickBooks Enterprise Support Team Is Available Few Steps Away From You.

    ReplyDelete
  13. QuickBooks Tech Support Phone Number has a great deal to offer to its customers in order to manage every trouble that obstructs your projects. There are tons many errors in QuickBooks such as difficulty in installing this software, problem in upgrading the software in to the newer version so that you can avail the most recent QuickBooks features, trouble in generating advanced reports, difficulty with opening company file in multi-user mode and thus on and so forth. Whatever the issue is, if it bothers you and deters the performance of your respective business, you may need to not get back seat and offer up, just dial us at our toll-free number and luxuriate in incredible customer care.

    ReplyDelete
  14. Have you been scratching your head and stuck together with your QuickBooks related issues, you may be only one click away from our expert technical support for your QuickBooks related issues. We QuickBooks Tech Support Phone Number, are leading technical support provider for all your QuickBooks related issues. Either it is day or night, we offer hassle-free tech support team for QuickBooks and its associated software in minimum possible time. Our dedicated technical team is available to help you 24X7, 365 days per year to make certain comprehensive support and services round the clock. We assure you the quickest solution of all your QuickBooks software related issues.

    ReplyDelete
  15. With regards to features and functionality, QuickBooks always tops the listing of perfect accounting software. From accuracy to speed, this software supplies the perfection to your accounting process. QuickBooks uses web soul to exhibit all internet based pages. While taking care of this software, you may possibly face an error while attempting to glance at webpages from QuickBooks program. An error message pops through to your screen specifying the error. Don’t panic, because our company is only at QuickBooks Support Phone Number to help you realize the error as well as its resolution. We have a team of dedicate and technically skilled professionals to produce you probably the most reliable technical assistance in QuickBooks.

    ReplyDelete
  16. In May 2002 Intuit thrown QuickBooks Enterprise Solutions for medium-sized businesses. QuickBooks Enterprise Support Phone Number here to make tech support team to users. In September 2005, QuickBooks acquired 74% share associated with market in the usa. A June 19, 2008 Intuit Press Announcement said that during the time of March 2008, QuickBooks’ share of retail units inside the industry accounting group touched 94.2 percent, predicated on NPD Group.

    ReplyDelete
  17. QucikBooks Enterprise Technical Support has almost eliminated the typical accounting process. Along with a wide range of tools and automations, it provides a wide range of industry verticals with specialized reporting formats and tools.

    ReplyDelete
  18. QuickBooks Payroll Support Phone Number today’s scenario individuals have got really busy inside their lives and work. They want to grow and learn as many new things as they possibly can. This drive has initiated a feeling of awareness amongst individuals and thus they find approaches to invent alternatives for daily tasks.

    ReplyDelete
  19. QuickBooks 2019 may be the better account management product till now. The recent improvement that is made in the program regarding current user requirements as well as the approaches to overcome the limitation of previous QuickBooks versions. We've been here to boost your understanding in regards to the payroll updates happens in QuickBooks Enterprise, desktop, pro, premier 2019 versions. Solve your queries related to QuickBooks Online Payroll whether Enhanced or Full Service. Fix all the issues for QuickBooks Desktop Payroll Basic, Standard, & Payroll Assisted. Look to the above number to make contact with our ProAdvisor to have support forQuickBooks Payroll Support Phone Number USA.

    ReplyDelete
  20. They surely will lead you to the stage or they can tell you the processes by following which you can remove error 12007 from QuickBooks. Another thing that you can do is to do contact some of the experts of QuickBooks Tech Support Number for solving this error,

    ReplyDelete
  21. So, you can choose to use this QuickBooks Errors Support Number software if you are interested in using the various new features which can be useful for the betterment of your accounting. There is a number of advanced features which have been added in this enterprise version of the software.

    ReplyDelete
  22. Enterprise customer support cell phone number. We understand that your growing business needs your precious time which explains why we offer the most effective to the customers. Our technically skilled professionals are well regarded for smart technical QuickBooks Enterprise Support Contact Number the world.

    ReplyDelete
  23. QuickBooks Payroll is an application which includes made payroll a simple snap-of-fingers task. You'll be able to quite easily and automatically calculate the tax for your employees. It is an absolute software that fits your organization completely. We provide QuickBooks Payroll Tech Support team in terms of customers who find QuickBooks Payroll hard to use. As Quickbooks Payroll customer care we make use of the responsibility of resolving all of the issues that hinder the performance regarding the exuberant software. There is certainly sometimes a number of errors which may bother your projects flow, nothing should be taken as burden that being said because the support team of Quickbooks Payroll Customer care resolves every issue in minimal time and commendable expertise.

    ReplyDelete
  24. By using QuickBooks Payroll Tech Support, you're able to create employee payment on time. However in any case, you might be facing some problem when making use of QuickBooks payroll such as for instance issue during installation, data integration error, direct deposit issue, file taxes, and paychecks errors, installation or up-gradation or simply just about some other than you don’t panic, we provide quality QuickBooks Payroll help service. Here are some features handle by our QB online payroll service.

    ReplyDelete
  25. Although Intuit has pops up many accounting software like Quicken , Intuit Tax Online Accountant, QuickBooks GoPayment, Mint,TaxCaster by TurboTax,MyTaxRefund by TurboTax,TurboTax SnapTax,Online Payroll, QuickBooks Support Phone Number however the users of Quickbooks are far more than other products .

    ReplyDelete
  26. This is the reason why QuickBooks has opened toll free QuickBooks Support Phone Number. For telephone assistance just call or email to guide team. You can fetch quick resolutions for all the issues you face together with your QuickBooks.

    ReplyDelete
  27. And also, some issues pertaining to QuickBooks company file, QuickBooks email service and heavy and unexpected QuickBooks Support Phone Number and many more. So if so, you just need the most advanced & highly certified experts, and we have given you our excellent professional or experts team and so they provide you with an immediate and very easy solution of your all issues or errors.

    ReplyDelete
  28. While you are facing HP tablet and driver problems, ensure that you have taken the aid of the HP Printer Tech Support Number experts. You can repair the HP tablet if you take their assistance. Also, you will be expected to realize that when you have the manual, you are able to proceed with the HP Printer driver installation and setup.

    ReplyDelete
  29. Nowadays it is a considerable challenge for business organizations to obtain an excellent strategy to manage their business accounts in QuickBooks Support To utilize a successful organization, you have to have a strong account management process to cultivate and lead.

    ReplyDelete
  30. The most common errors faced by the QuickBooks users is unknown errors thrown by QuickBooks software at the time of software update. To be able to fix the problem, you ought to look at your internet and firewall setting, web browser setting and system time and date setting you can simply give us a call at QuickBooks Phone Number for instant assistance in QB issues.

    ReplyDelete
  31. Everbody knows you will find always two sides to a coin and QuickBooks Tech Support isn't any different. This software also throws some errors in the long run. Sometimes it becomes quite difficult to know that is using this error code or message.

    ReplyDelete
  32. The QuickBooks Payroll Tech Support Phone Number team at site name is held responsible for removing the errors that pop up in this desirable software. We care for not letting any issue can be purchased in in the middle of your work and trouble you in undergoing your tasks. Many of us resolves most of the QuickBooks Payroll issue this type of a fashion that you'll yourself believe that your issue is resolved without you wasting the time into it. We take toll on every issue through the use of our highly trained customer care/.

    ReplyDelete
  33. We plan to give you the immediate support by our well- masterly technicians. A group of QuickBooks Support Phone Number dedicated professionals is invariably accessible to suit your needs so as to arranged all of your problems in an attempt that you’ll be able to do your projects while not hampering the productivity.

    ReplyDelete
  34. QuickBooks Tech Support Phone Number offers the Outmost Solution of your Software Issues. Although, QuickBooks is a robust accounting platform that throws less errors in comparison with others. It is always been the absolute most challenging task to efficiently manage the business accounts in a genuine and proper way by simply obtaining the best and proper solutions.

    ReplyDelete
  35. Our QuickBooks customer support team will guide you for each product of QuickBooks whether QuickBooks Enterprise Support, Accountant, Pro, and Premier.QuickBooks Online Are you thinking that how can you place your business finance in an organized way? Your search is completed now. QuickBooks Support Phone Number is the right choice for you.

    ReplyDelete
  36. Amended income tracker, pinned notes, better registration process and understandings on homepage are the large choice of general alterations for most versions of QuickBooks 2015. It can benefit for QuickBooks Enterprise Tech Support Number to acquire technical help & support for QuickBooks.

    ReplyDelete
  37. QuickBooks error 9999 appears during program installation. Also, an error occurs while QuickBooks is running, during windows start up or shut down or even during the installation of the Windows operating system. If you would like to learn How To Fix Quickbooks Error 9999, you can continue reading this blog.

    ReplyDelete


  38. I am reading your post from the beginning, it was so interesting to read & I feel thanks to you for posting such a good blog, keep updates regularly.I want to share about tibco training .

    ReplyDelete