MySql Stored Procedure Problems

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.