I have never used a MySQL stored procedure before. I am not used to writing a lot of data for the first time. The record is as follows: The following is the simplest MySQL stored procedure to add two numbers. Note that
SQL code delimiter $ create procedure proc_add (in a int, in B int) begin declare c int; if a is null then set a = 0; end if; if B is null then set B = 0; end if; set c = a + B; select c; end $ delimiter; www.2cto.com 1. the declare statement can only be placed at the beginning of the stored procedure. If it is placed at the end, an error 2 is reported. if statements must be followed by then, but do not require in. end if 3 is required when the if statement ends. it IS the same as MSSQL to determine whether it is null. 4. delimiter refers to the delimiter, which means to add the delimiter 5 after the end. end if must be followed by a semicolon. Otherwise, syntax errors are common in the following scenarios: Determine whether a value exists in a column in the table. if an operation exists
SQL code delimiter $ create procedure proc_add_book (in $ bookName varchar (200), in $ price float) begin declare $ existsFlag int default 0; select bookId into $ existsFlag from book where bookName = $ bookName limit 1; if bookId> 0 then # if not exists (select * from book where bookNumber = $ bookName) then insert into book (bookNumber, price) values ($ bookName, $ price); end if; end $ delimiter; note that if ex cannot be used Ists; exists can be used after where or in create object, but it cannot be used in if statements. It can only be modified. The while statement also needs to be noted that the following is a simple application of while:
SQL code www.2cto.com delimiter $ create procedure proc_add_books_looply (in $ bookName varchar (200), in $ price float, in $ insertTimes INT) begin while $ insertTimes> 0 do insert into book (bookName, price) values ($ bookName, $ price); end while; end $ delimiter; you can see the conditions behind while, the condition is followed by a do statement. After the while LOOP body ends, end while and end with a semicolon. The above are some simple summaries and hope to be useful. Author: yukaizhao