MYSQL syntax error, unexpected IDENT_QUOTED, expecting $end - mysql

I am trying to chain multiple operations on my database for a change repo for ease of database consistancy between development/production environments. I have created a file with multiple commands and am getting this weird error that I can't seem to find a reference on.
A snippet is below:
deallocate prepare stmt;
END$$
drop procedure if exists SearchByWantListCount;
delimiter $$
CREATE DEFINER=`webaccess`#`%` PROCEDURE `SearchByWantListCount`(
IN loggedInUser INT,
IN filter varchar(255))
BEGIN
The delimiter is being underlined in red and I'm getting the error:
syntax error, unexpected IDENT_QUOTED, expecting $end

I added in a
delimiter ;
drop procedure if exists SearchByWantListCount;
which seemed to make everything happy....

That's because you don't need to put delimiter before $$ when you want to use the delimiter. Simply put $$.
The syntax you used defines the delimiter, thing that has been done earlier (since we can see you use the delimiter on the END).
Why "delimiter ;" worked is because the semi-colon at the end of "drop procedure if exists SearchByWantListCount;" is then counted as a delimiter. Would you put "delimiter $$" here or not, you need a $$ between the drop line and the start of your next procedure (SearchByWantListCount), or before "delimiter $$" if you want to redefine it.
By the way, you don't need your "delimiter ;" since you put $$ after the previous END symbol. Simply remove the "delimiter" before the $$.

Related

MySQL Trigger Creation Executes Without Syntax Error but no trigger created, other executions fail

I'm not receiving an error currently when running the query to create a trigger but after running it I can't execute another query. It is as if I haven't closed some encapsulation:
DELIMITER // CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable for each row begin INSERT INTO new_delete_table (column) values(old.column) end; END DELIMITER;
I am using command line and start with:
mysql>
I execute and receive a new:
mysql>
which is the normal behavior when a query is successful. If I then try to see my triggers I end up in an infinite loop where it is waiting for me to enter some character to close something.
mysql> show triggers;
->
I can use ctrl + c to exit the function but that boots me out of MySQL as well. When I log back in my trigger is not present and I can't find any errors.
The DELIMITER command is special. All characters following the command until the end of line are interpreted as a string that you want to use as the new delimiter.
You must not put code on the same line, because all of that code will become part of the new delimiter. This is why you got no error, but no CREATE TRIGGER statement was executed. It only became part of a very long delimiter string.
The reason that DELIMITER must interpret the end-of-line as the end of the command is that it doesn't accept ; as the end of the DELIMITER command. If it did, there would be no way to reset the delimiter back to ;.
You asked in a comment if you need newlines. Aside from the newlines after DELIMITER commands, you do not need newlines. You can do this:
DELIMITER //
CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable FOR EACH ROW BEGIN INSERT INTO new_delete_table (column) VALUES(old.column) END //
DELIMITER ;
(Remember to use // as the delimiter at the end of the CREATE TRIGGER statement.)
Youhave some errors in your code
every code line must end in a semicolon and you have an END to much
DELIMITER //
CREATE TRIGGER trigger_name BEFORE DELETE ON existingtable
for each row
begin
INSERT INTO new_delete_table (`column`) values
(old.column) ;
end//
DELIMITER;

MySQL unable to understand the syntax error [duplicate]

Very new to the environment, I have a question about a line that's added to the end of my code. The guide I'm following is:
http://net.tutsplus.com/tutorials/an-introduction-to-stored-procedures/
If anyone has a better one regarding MySQL stored procedures, I'm all ears.
Before I ask, this is the environment I'm using:
OS: Windows 7 / WAMP (MySQL 5.5.24) / MySQL Workbench
I'm instructed to define a delimiter; in my case I'm sticking with the default '$$.'
The stored procedure I created is:
DELIMITER $$
CREATE PROCEDURE test.`p2` ()
LANGUAGE SQL
DETERMINISTIC
COMMENT 'Adds "nson" to first and last names in the record.'
BEGIN
SELECT 'Hello World';
END $$
When I apply this stored procedure and I get the review screen, I see a new line of code added;
At the bottom:
DELIMITER ;
This lats line; is it added because the DELIMITER statement announces a block within which the defined delimiters ($$) can be used and thus closes the block in the end?
When using the builtin procedure editor, MySQL Workbench adds a few extra commands:
USE `test`; // <----------
DROP procedure IF EXISTS `p2`; // <----------
DELIMITER $$
USE `test`$$ // <----------
CREATE PROCEDURE test.`p2` ()
LANGUAGE SQL
DETERMINISTIC
COMMENT 'Adds "nson" to first and last names in the record.'
BEGIN
SELECT 'Hello World';
END $$
DELIMITER ; // <----------
Those commands are not strictly related to the stored procedures syntax, they're merely a commodity—other MySQL clients (such as HeidiSQL or the official command line utility) will not add them. The last delimiter change is probably a reset to avoid problems in future statements on the same connection.
You need to change the delimiter in order to instruct the client about where the procedure code starts and end. The problem is that the procedure body is normally a collection of SQL statements so omitting the delimiter change would make MySQL think that you are attempting to run a series of statements, the first of which would be this:
CREATE PROCEDURE test.`p2` ()
LANGUAGE SQL
DETERMINISTIC
COMMENT 'Adds "nson" to first and last names in the record.'
BEGIN
SELECT 'Hello World';
With DELIMITER $$ you are telling MySQL that your full statement goes from CREATE to END. It's just syntactic sugar: DELIMITER is not even a SQL keyword. HeidiSQL, for instance, provides a GUI with a text box where you write the procedure body, thus you don't need the DELIMITER workaround.

MySQL Is there such a thing as too much comment

I'm trying to do a DROP CREATE stored procedure in MySQL.
The first block of code runs just fine.
DELIMITER //
-- IMPORTANT: Change to ecom_prod
USE ecom_dev;
DROP PROCEDURE IF EXISTS `usp_getDetails`;
The second block gives an error in statement #2 near --DROP CREATE Procedure
DROP PROCEDURE IF EXISTS usp_getDetails; at line 1
DELIMITER //
-- IMPORTANT: Change to ecom_prod
USE ecom_dev;
-- DROP CREATE Procedure
DROP PROCEDURE IF EXISTS `usp_getDetails`;
The only difference is the addition of extra comments. I can't understand how the extra comment is causing this error.
Any and all help is welcome
Once the DELIMITER is changed to something other than semicolon, the new delimiter should be used in place of the semicolon.
For example:
DELIMITER $$
-- IMPORTANT: Change to ecom_prod
USE ecom_dev$$
DROP PROCEDURE IF EXISTS `usp_getDetails`$$
DELIMITER ;
I'm not sure how your code is working with the semicolons. I don't think the issue has anything to do with the comments. But I haven't tested it. I always have a blank line preceding and following DELIMITER. And the only time I ever use DELIMITER is when I'm issuing a CREATE <stored_program_type>.

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.

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;