简体   繁体   中英

Syntax Problem with MySQL Stored Procedure

create procedure testproc
as
begin
select * from customers
end

In this code as is showing error. If I hover over as it shows as is not valid at this position. I have tried all other codes means different alternatives but still the same issue.

If you're not passing in any parameters or processing the results, consider creating a view. Like this:

USE `database_name`;
CREATE OR REPLACE VIEW `v_my_view_name`
AS
    SELECT * FROM customers;

Then, you can call the view like this:

SELECT * FROM v_my_view_name;

try this:

CREATE FUNCTION testproc() RETURNS INT    
BEGIN
    DECLARE test INT;
    SELECT 42 INTO test; 
    RETURN test;
END

this creates a new FUNCTION named testproc returning the data type INT . Between BEGIN and END , you find the body of the function which is executed each time you call the function.

  • First we DECLARE a variable test of type INT .
  • Then we start a query ( SELECT 42 ) and store the result INTO test .
  • Finally, we RETURN test .

To execute this function, you can use

SELECT testproc();

which will return 42.

This the way I always do it:

DROP PROCEDURE IF EXISTS `testproc`;
DELIMITER //
CREATE PROCEDURE testproc()
BEGIN
   SELECT * FROM customers;
END  //
DELIMITER ;

And to use this Stored Procedure

CALL testproc();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM