As is not valid identifier when creating procedure - mysql

I was following a tutorial on how to create procedures, I wrote the code
CREATE PROCEDURE test2
AS
select * FROM vgsales_ratings;
and got
"AS" is not valid at this position expecting '('
I tried adding parentheses around some of the stuff but it didn't change anything. Is something wrong with my machine or is the code wrong. I'm using mysql workbench 8. The table exsits. Thanks for any help

I would simply use :
CREATE PROCEDURE test2 ()
select * FROM vgsales_ratings;
But most of time,we use a routine block if we have multiple statements. In this case, delimiter needs to be used to change the default delimiter. After the procedure is created, change the delimiter back to ; .
delimiter //
CREATE PROCEDURE test2 ()
BEGIN
IF (select count(*) FROM vgsales_ratings) > 0 then
select * FROM vgsales_ratings;
end if;
END //
delimiter ;

Related

Why delimiter used with stored procedure in mysql?

I am trying to understand, why delimiter used with stored procedure in mysql?
but i couldn't.
DELIMITER //
CREATE PROCEDURE GetAllProducts()
BEGIN
SELECT * FROM products;
END //
DELIMITER ;`
Mysql's default delimiter is ; which is used for one statement in the command line , something as
select * from users ;
When you write a trigger or stored procedure to execute the entire code mysql needs to understand that its a block of code/query.
If no delimiter is provided then when mysql encounters any ; inside the store procedure or trigger it will think that as one statement and will try to execute it. So we need to provide a delimiter for store procedure or trigger and make mysql understand that anything within that delimiter is one complete set of code.
So in your example
SELECT * FROM products;
it will be a part of the complete statement when there is a delimiter other than ; is provided at the beginning.

Cannot create stored procedure

I have the following piece of statement entered into MySQL5.6 Command Line Client. However, the following error was received. I haven't even been able to add in END// Delimiter; after the select statement.
At the same time, i was wondering after the stored procedure has been created successfully, how do i CALL the stored procedure without the command line but using java codes.
Kindly assist. Greatly appreciated!
give space between delimiter and //. After your select statement write end; on next line and // at last line (after end; in next new line)
delimiter //
create procedure GetUStocks()
Begin
Select * From buystocks;
end;
//
mysql> delimiter //
mysql> CREATE PROCEDURE GetUStocke()
-> BEGIN
-> SELECT * FROM buystocks ;
-> END//
You need a space between DELIMITER and the symbol you are changing the delimiter to.
mysql> DELIMITER //
The clue that it worked should be that you get another mysql> prompt instead of the "unfinished command" prompt ->.
Re your comment, if you need to call a stored procedure from a Java application, refer to the manual on callable statements: http://dev.mysql.com/doc/refman/5.6/en/connector-j-usagenotes-statements-callable.html
Well, seriously, I was Shocked and still am upon this accidental discovery.
It's simply because you are not using the delimiter that you have defined for ending the procedure.
Here let me attach two snippets that will help illustrate what is generating the error.

Delimiters in MySQL

I often see people are using Delimiters. I tried myself to find out what are delimiters and what is their purpose. After 20 minutes of googling, I was not able to find an answer which satisfies me. So, my question is now: What are delimiters and when should I use them?
Delimiters other than the default ; are typically used when defining functions, stored procedures, and triggers wherein you must define multiple statements. You define a different delimiter like $$ which is used to define the end of the entire procedure, but inside it, individual statements are each terminated by ;. That way, when the code is run in the mysql client, the client can tell where the entire procedure ends and execute it as a unit rather than executing the individual statements inside.
Note that the DELIMITER keyword is a function of the command line mysql client (and some other clients) only and not a regular MySQL language feature. It won't work if you tried to pass it through a programming language API to MySQL. Some other clients like PHPMyAdmin have other methods to specify a non-default delimiter.
Example:
DELIMITER $$
/* This is a complete statement, not part of the procedure, so use the custom delimiter $$ */
DROP PROCEDURE my_procedure$$
/* Now start the procedure code */
CREATE PROCEDURE my_procedure ()
BEGIN
/* Inside the procedure, individual statements terminate with ; */
CREATE TABLE tablea (
col1 INT,
col2 INT
);
INSERT INTO tablea
SELECT * FROM table1;
CREATE TABLE tableb (
col1 INT,
col2 INT
);
INSERT INTO tableb
SELECT * FROM table2;
/* whole procedure ends with the custom delimiter */
END$$
/* Finally, reset the delimiter to the default ; */
DELIMITER ;
Attempting to use DELIMITER with a client that doesn't support it will cause it to be sent to the server, which will report a syntax error. For example, using PHP and MySQLi:
$mysqli = new mysqli('localhost', 'user', 'pass', 'test');
$result = $mysqli->query('DELIMITER $$');
echo $mysqli->error;
Errors with:
You have an error in your SQL syntax; check the manual that corresponds
to your MySQL server version for the right syntax to use near 'DELIMITER $$' at line 1
The DELIMITER statement changes the standard delimiter which is semicolon ( ;) to another. The delimiter is changed from the semicolon( ;) to double-slashes //.
Why do we have to change the delimiter?
Because we want to pass the stored procedure, custom functions etc. to the server as a whole rather than letting mysql tool to interpret each statement at a time.
When you create a stored routine that has a BEGIN...END block, statements within the block are terminated by semicolon (;). But the CREATE PROCEDURE statement also needs a terminator. So it becomes ambiguous whether the semicolon within the body of the routine terminates CREATE PROCEDURE, or terminates one of the statements within the body of the procedure.
The way to resolve the ambiguity is to declare a distinct string (which must not occur within the body of the procedure) that the MySQL client recognizes as the true terminator for the CREATE PROCEDURE statement.
You define a DELIMITER to tell the mysql client to treat the statements, functions, stored procedures or triggers as an entire statement. Normally in a .sql file you set a different DELIMITER like $$. The DELIMITER command is used to change the standard delimiter of MySQL commands (i.e. ;). As the statements within the routines (functions, stored procedures or triggers) end with a semi-colon (;), to treat them as a compound statement
we use DELIMITER. If not defined when using different routines in the same file or command line, it will give syntax error.
Note that you can use a variety of non-reserved characters to make your own custom delimiter. You should avoid the use of the backslash (\) character because that is the escape character for MySQL.
DELIMITER isn't really a MySQL language command, it's a client command.
Example
DELIMITER $$
/*This is treated as a single statement as it ends with $$ */
DROP PROCEDURE IF EXISTS `get_count_for_department`$$
/*This routine is a compound statement. It ends with $$ to let the mysql client know to execute it as a single statement.*/
CREATE DEFINER=`student`#`localhost` PROCEDURE `get_count_for_department`(IN the_department VARCHAR(64), OUT the_count INT)
BEGIN
SELECT COUNT(*) INTO the_count FROM employees where department=the_department;
END$$
/*DELIMITER is set to it's default*/
DELIMITER ;

i bulid stored procedure in MySql server but doesn't work !

how to bulid a stored procedure in MySql server .
i wrote the stored procedure outside in note but i don't know how to apply this stored procedure in the server that i am doing the testin .
to be clear i download wampSever which includes Mysql ,then i created my tables. but again i don't know the steps that i have to take in orded to use the stored procedure.
my stored procedure:
CREATE PROCEDURE findHorse
#horseName varchar(15) AS SELECT * FROM horse
WHERE horseName =#horseName
i know that if i wante to execute it i use
exec findHorse
all what i need are :
1. how to create it in mySql. //steps
2.where should i put exec findHorse in order to execute it //steps
In MySQL you execute a stored procedure by 'calling' it:
CALL DoStuff(#Param1);
You create one as follows:
CREATE PROCEDURE DoStuff()
BEGIN
SELECT Column1
FROM MyTable;
END;
This might help
I believe this is what you are looking for:
DELIMITER $$
CREATE PROCEDURE `findHorse` (IN `inFindHorse` CHAR(255))
BEGIN
SELECT * FROM `horse` WHERE `horse`.`horseName` = `inFindHorse`;
END$$
DELIMITER ;
And as Randy noted, the correct way to call this procedure is:
CALL findHorse('Flicka');

create procedure fails?

when trying to create a simple procedure in mysql 5.1.47-community it fails everytime i've tried everything!
even simple things like this!
DELIMITER //
CREATE PROCEDURE two ()
begin
SELECT 1+1;
end;
//
The error is
ERROR: Error 1064: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'mydb' at line 1
The error message you've given doesn't correspond to the code you've pasted. You're referring to "mydb" somewhere in the SQL you're running yet it's not anywhere in the code you've put in the question.
The code you've given should work fine as I see no syntax errors, you may just need to give it a database to work on ("test" in my case here, perhaps it should be "mydb" for you?).
DELIMITER //
CREATE PROCEDURE test.two ()
begin
SELECT 1+1;
end;
//
DELIMITER ;
CALL test.two;
However, I suspect the error you're getting is become of a line in your SQL that you're not showing us.
EDIT
It could perhaps be the delimiter command. You're changing the delimiter to // rather than the default ;. So perhaps you've run that command (and changed the delimiter for your session to //), and then tried to run USE mydb; where the ; is no longer recognised as a valid delimiter for your session, and that could be giving you the error. Try putting delimiter ; before your use line and see if that helps (and then use it again after you've defined your stored procedure so you can call it). This is just a theory though, as I'm not sure of the intricacies of the delimiter command.
Remove the final delimiter "end" instead "end;"
I had the same problem using heidisql as the fronted to enter the SQL. My first attempt was:
CREATE PROCEDURE Add_Two (IN someNumber int, OUT result INT)
BEGIN
SELECT someNumber +2 INTO result;
END
and this resulted in SQL ERROR (1064) because i was not aware that when using a client program a delimiter is needed to define the stored procedures.
After changing the above to this:
DELIMITER //
CREATE PROCEDURE Add_Two(IN someNumber int, OUT result INT)
BEGIN
SELECT someNumber +2 INTO result;
END
//
It worked out.
Example to call it
SET #someNumber :=8;
CALL Add_Two(#someNumber, #result);
SELECT #result;