views:

194

answers:

2

Hi, I'm coming from a MS SQL Server background. Working on a new project using MySQL with NaviCat 8 Admin tools. Ok, here's the question. Normally when working in MS land if I want to update some data I use a stored procedure to do this:

Drop Procedure spNew
Create Procedure spNew (@P_Param)

UPDATE Table
SET Field = 'some value'
WHERE ID = @P_Param

I am trying to do this same logic from within NaviCat. I defined the Parameter, (IN '@P_Param' int)

In the Definition i placed:

BEGIN
     UPDATE Table
     SET Field = 'some value'
     WHERE ID = @P_Param
END;

When I try and save the stored procedure, i'm getting this error: "1064 - You have an error in your SQL syntax, blah, blah, blah"

Can anyone at least point me in the right direction?

Thanks.

+2  A: 
CREATE PROCEDURE spNew(P_Param INT)
BEGIN
     UPDATE Table
     SET Field = 'some value'
     WHERE ID = P_Param;
END;

Note that MySQL syntax and overall ideology are very different from those of SQL Server.

You may also need to set delimiter:

DELIMITER $$

CREATE PROCEDURE spNew(P_Param INT)
BEGIN
     UPDATE Table
     SET Field = 'some value'
     WHERE ID = P_Param;
END;
$$

DELIMITER ;

BTW, I'm assuming you don't actually call your table "Table", since it's a reserved word.

If you do, you need to enclose it into backticks like this:

DELIMITER $$

CREATE PROCEDURE spNew(P_Param INT)
BEGIN
     UPDATE `Table`
     SET    `Field` = 'some value'
     WHERE  `ID` = P_Param;
END;
$$

DELIMITER ;
Quassnoi
I realize the ideologies are very different.Logic-wise though I was able to jump right in and understand what I'm doing for the most part.After removing the '@' I am still getting a syntax error.
EWizard
Still no dice with the Delimiter.Is it safe to say that Stored Procedures are not used by the masses often in MySQL?
EWizard
It is safer to say that `NaviCat 8` is not used :) I just reproduced it on `Query Browser` (shipped with `MySQL`), everything's OK.
Quassnoi
Very good. Thanks!
EWizard
Ha, this is funny. The backticks looked like single quotes to me.Fixed.Thanks a ton Quassnoi!:-D
EWizard
A: 

Parameters to MySQL stored procedures aren't prefixed with @ or quoted in either the declaration or when used. Local variables are prefixed with @, however.

Try:

DROP PROCEDURE IF EXISTS spNew;
CREATE PROCEDURE spNew(IN P_Param INT)
BEGIN
     UPDATE Table
     SET Field = 'some value'
     WHERE ID = P_Param
END;
ataylor