Stored procedure in php w3schools

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Stored Procedures are created to perform one or more DML operations on Database. It is nothing but the group of SQL statements that accepts some input in the form of parameters and performs some task and may or may not returns a value. 

    Syntax : Creating a Procedure 
     

    CREATE or REPLACE PROCEDURE name(parameters)
    IS
    variables;
    BEGIN
    //statements;
    END;

    The most important part is parameters. Parameters are used to pass values to the Procedure. There are 3 different types of parameters, they are as follows: 
     

    1. IN: 
      This is the Default Parameter for the procedure. It always receives the values from calling program.
    2. OUT: 
      This parameter always sends the values to the calling program.
    3. IN OUT: 
      This parameter performs both the operations. It Receives value from as well as sends the values to the calling program.


    Example: 

    Imagine a table named with emp_table stored in Database. We are Writing a Procedure to update a Salary of Employee with 1000. 
     

    CREATE or REPLACE PROCEDURE INC_SAL(eno IN NUMBER, up_sal OUT NUMBER)
    IS
    BEGIN
    UPDATE emp_table SET salary = salary+1000 WHERE emp_no = eno;
    COMMIT;
    SELECT sal INTO up_sal FROM emp_table WHERE emp_no = eno;
    END; 
    • Declare a Variable to Store the value coming out from Procedure : 
    VARIABLE v NUMBER;
    • Execution of the Procedure: 
       
    EXECUTE INC_SAL(1002, :v);
    • To check the updated salary use SELECT statement: 
       
     SELECT * FROM emp_table WHERE emp_no = 1002;
    • or Use print statement : 
       
    PRINT :v

    Pl sql stored procedure:

    The pl sql stored procedure is a named PL/SQL block which performs one or more specific tasks. A pl sql stored procedure can be divided into two parts: Header and Body part.
    Header: The header part contains the name of the procedure and the parameters passed to the procedure.
    Body: The body part contains declaration section, execution section and exception section.
    Note: A pl sql stored procedure do not return a value directly.

    How to pass parameter in a procedure?

    We can use the below modes to pass the parameters in a procedure:
    IN-parameters: These parameters are the read-only parameters. Procedure cannot change the value of IN parameters.
    OUT-parameters: These parameters are the write-only parameters and used to return values back to the calling program. Procedure can change the value of OUT parameters.
    IN OUT-parameters: These parameters are read and write parameters i.e. a procedure can reads and change the IN OUT parameter value and return it back to the calling program.

    Syntax of pl sql stored procedure:

    CREATE [OR REPLACE] PROCEDURE proc_name [list of parameters] 
    IS | AS    
       //Declaration block 
    BEGIN    
       //Execution block 
    EXCEPTION    
      //Exception block 
    END;

    How to create a procedure?

    Procedure example without parameters:

    CREATE OR REPLACE PROCEDURE hello_world
    AS
    BEGIN
       dbms_output.put_line('Hello World!');
    END;
    /

    Procedure example with parameters:

    CREATE OR REPLACE PROCEDURE add_student(rollNo IN NUMBER, name IN VARCHAR2)
    IS 
       BEGIN 
        insert into students values(rollNo,name);
      END;
    /

    How to execute stored procedure?

    A procedure can be executed by using EXEC or EXECUTE statement.

    EXEC procedure_name();
    EXEC procedure_name;

    Note: Execute procedure with parameters:

    EXEC procedure_name(param1,param2…paramN);

    A procedure can also be invoked from other PL SQL block.

    BEGIN
       procedure_name;
    END;
    /

    How to drop stored procedure?

    DROP PROCEDURE procedure_name;

    Stored procedure in php w3schools

    What are stored procedure in PHP?

    A stored procedure is a subroutine stored in the database catalog. Applications can call and execute the stored procedure. The CALL SQL statement is used to execute a stored procedure. Stored procedures can have IN , INOUT and OUT parameters, depending on the MySQL version.

    How do I execute a stored procedure in PHP?

    How to Call a Stored Procedure From PHP in MySQL.
    Right-click the PHP page you want to use to call the stored procedure and select "Open With." Click the PHP editor to open the code..
    Add the PHP connection to the MySQL database. ... .
    Select a database name. ... .
    Call the stored procedure to retrieve MySQL records..

    What is stored procedure explain with example?

    What is a Stored Procedure? A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again, save it as a stored procedure, and then just call it to execute it.

    What are stored procedures?

    A stored procedure is a set of Structured Query Language (SQL) statements with an assigned name, which are stored in a relational database management system (RDBMS) as a group, so it can be reused and shared by multiple programs.