Mysql procedure, using parameter in where clause - mysql

I made this procedure from phpmyadmin, but it don't works, I replaced the last word from record_id to a specified string id and worked, but when I use the parameter not working.
DROP PROCEDURE `prcd_update_record`;
CREATE DEFINER=`root`#`localhost`
PROCEDURE `prcd_update_record`(
IN `talep_id` VARCHAR(24),
IN `vall` INT(10)
)
NOT DETERMINISTIC
MODIFIES SQL DATA SQL
SECURITY INVOKER
UPDATE `talep_malzeme`
SET `kalan_miktar` = vall
WHERE `talep_malzeme`.`id` = talep_id;
The I execute it like this:
SET #p0='33'; SET #p1='57fb7911ea91e9efa'; CALL `prcd_update_record`(#p0, #p1);

DROP PROCEDURE IF EXISTS `prcd_sevk_toplam`;
create procedure prcd_sevk_toplam(talep_id int, vall VARCHAR(255))
BEGIN
UPDATE `talep_malzeme` SET `kalan_miktar` = vall WHERE `talep_malzeme`.`id` = talep_id;
END;
Hope this will help you.

Looks like you has wrong parameter order, try
CALL `prcd_sevk_toplam`(#p1, #p0);

You should provide proper value to your parameter per your parameter definition. Your procedure accepts parameter as below
PROCEDURE `prcd_sevk_toplam`(
IN `talep_id` VARCHAR(24),
IN `vall` INT(10)
And you are setting both of them to varchar. That could be the issue here. You should set them as
SET #p0=33;
SET #p1='57fb7911ea91e9efa';
CALL `prcd_sevk_toplam`(#p1, #p0);

Related

Trying to convert SQL Server to MySql for class

I am trying to convert code given by teacher that is in SQL Server to MySQL. It ultimately should allow me to input information to the table that was made. This is a for a guest book assignment. Unfortunately I cannot find the correct syntax as I have never worked with SQL programming in general before.
I have tried using brackets,semi colons and commas for the information as well as adding a delimeter which eliminated the "Unrecognized statement type (near PROCEDURE)" error messages that had initially been in the programming as well.
Teachers code:
CREATE PROCEDURE spInsertGuestbookEntry
#GuestBookName varchar(200),
#GuestBookEntry ntext,
#GuestbookEmail varchar(200),
#GuestBookIP varchar(20)
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(#GuestbookName, #GuestBookEntry, #GuestbookEmail, #GuestBookIP, GetDate())
END
GO
My modifications:
DELIMITER $$
CREATE PROCEDURE spInsertGuestbookEntry
#GuestBookName varchar(200)
#GuestBookEntry text
#GuestbookEmail varchar(200)
#GuestBookIP varchar(20)
#GuestBookDate date
AS
BEGIN
SET NOCOUNT ON;
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(#GuestbookName, #GuestBookEntry, #GuestbookEmail, #GuestBookIP, #GuestBookDate)
END
GO
The error messages that I got was "Unrecognized data type. (near))" That error message is by the #GuestBookName varchar(200)
DELIMITER $$
CREATE PROCEDURE `spInsertGuestbookEntry`(
IN `str_GuestBookName` VARCHAR(200),
IN `str_GuestBookEntry` TEXT,
IN `str_GuestbookEmail` VARCHAR(200),
IN `str_GuestBookIP` VARCHAR(20)
)
LANGUAGE SQL
NOT DETERMINISTIC
CONTAINS SQL
SQL SECURITY DEFINER
COMMENT ''
BEGIN
INSERT INTO Guestbook
(GuestbookName, GuestBookEntry, GuestbookEmail, GuestBookIP, GuestBookDate)
VALUES
(str_GuestBookName, str_GuestBookEntry, str_GuestbookEmail, str_GuestBookIP, CURRENT_DATE());
END
$$
Key points to consider:
Distinguish between table column_name and procedure input parameter.
# is used to create user-defined variables in MySQL and you should be using this if your parameter is of type OUT/INOUT while calling procedure.
In procedure/function creation, simple variable names are used, not with # symbol otherwise while using these parameters, MySQL will look for some variable with this name, not the input parameter.

Use IN param in WHERE clause inside MySQL stored procedure

I am using MySQL with HeidiSQL and I want to create a stored procedure that takes one String param and returns a table. This is what I have tried:
CREATE PROCEDURE sp(IN in_param VARCHAR(50))
BEGIN
SELECT * FROM fact_table ft
WHERE ft.param = #in_param
END
And I called it like this:
CALL sp('string_param');
The procedure returns an empty table, as #in_param is somehow NULL inside the SP.
I also like this: WHERE ft.param = in_param, but the I got an error when I ran it, saying SQL Error (1054): Unknown column 'in_param' in 'where clause'
Can someone tell me what I'm doing wrong?
PS:
I tried creating it by hand, and also using Heidi's Create new -> Stored routine wizard
I finally found out a solution that works:
CREATE PROCEDURE sp(IN in_param VARCHAR(50))
BEGIN
DECLARE declared_in_param CHAR(50);
SET declared_in_param = in_param;
SELECT * FROM fact_table ft
WHERE ft.param = declared_in_param;
END
So, the idea was to declare and set a new variable to that IN param, and use that declared variable inside the WHERE clause of the SELECT statement.
I haven't had time to research WHY this works, but I will

MySql syntax error on procedure parameter

I am trying to write a simple procedure but am encountering a syntax error at the first parameter. As best I can tell I'm following the syntax of CREATE PROCEDURE correctly.
I am limited to accessing my database with phpMyAdmin. Here is the create script I'm trying to run:
DROP PROCEDURE IF EXISTS product_index_swap/
CREATE PROCEDURE product_index_swap (#id INT, #oldIndex INT, #newIndex INT)
BEGIN
DECLARE #swapID;
SET #swapID = (SELECT `id` FROM `product` WHERE `order_index` = #newIndex LIMIT 1);
UPDATE `products` SET `order_index` = (CASE WHEN `id` = #id THEN #newIndex
WHEN `id` = #swapID THEN #oldIndex END)
WHERE `id` IN (#id, #swapID);
END
I am using the option on phpMyAdmin to change the delimiter to /.
I receive a syntax error "near '#id INT, #oldIndex INT....". I thought I may encounter more delimiter errors since I'm not entirely clear on the scope of them. I believe if that was the problem the error would be on a new line in the procedure when it failed to understand a semicolon, not at the parameters declaration.
You're using the Microsoft SQL Server convention of putting # before all the parameters and local variables. MySQL doesn't do this.
In MySQL syntax, procedure parameters have no sigil.
Also parameters are typically declared IN or OUT or INOUT.
CREATE PROCEDURE product_index_swap (IN id INT, IN oldIndex INT, IN newIndex INT)
BEGIN
DECLARE swapID;
...
MySQL variables that have the # sigil are session variables.
See also:
https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
https://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html
https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
In MySQL, the #var variables are session level variables.
Use normal variables without the # and make sure you do not have conflict with column names:
CREATE PROCEDURE product_index_swap (in_id INT, in_oldIndex INT, in_newIndex INT)
BEGIN
DECLARE v_swapID int;
SELECT id into v_swapID
FROM product
WHERE order_index = in_newIndex
LIMIT 1;
UPDATE products
SET order_index = CASE WHEN id = in_id THEN in_newIndex
WHEN id = v_swapID THEN in_oldIndex
END
WHERE id IN (in_id, v_swapID);
END

MySQL - use result of nested stored procedure

Is there any way to use the return value of a stored procedure in another stored procedure?
Just a very basic theoretical example:
CREATE PROCEDURE `user_read_name_and_email` (
IN `param_user_id` INT
)
BEGIN
DECLARE `current_user` SET;
SET `current_user` = CALL `user_read`(`param_user_id`);
SELECT `user_name`, `user_email` FROM `current_user`;
END
CREATE PROCEDURE `user_read` (
IN `param_user_id` INT
)
BEGIN
SELECT * FROM `user` WHERE `user_id` = `param_user_id`;
END
I think the one and only workaround is to use temporary tables.
Your example would be possible in MS SQL Server 2008 - there you can assing output of stored procedure to some variable.

Calling a stored procedure within an IF statement MySQL

Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'