Trying to convert SQL Server to MySql for class - mysql

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.

Related

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 procedure, using parameter in where clause

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);

Do temporary tables make functions non deterministic in MySQL

I want to create a stored function in MySQL. I've been granted ALL PRIVILEGES, what I think contains also the required SUPER privilege. And binary logging is enabled.
While creating a function I get the 1419 error:
Error Code: 1419. You do not have the SUPER privilege and binary logging is enabled (you *might* want to use the less safe log_bin_trust_function_creators variable)
I read through the MySQL manuals and it looks like this binary logging issue should only apply to NOT DETERMINISTIC functions, which change data.
I've created a simple example function which points out my question more clearly:
DROP FUNCTION IF EXISTS getIdTest;
DELIMITER $$
CREATE FUNCTION getIdTest( pv_order_nr VARCHAR( 45 ) )
RETURNS INT UNSIGNED
COMMENT 'Gets an order number and returns an ID'
DETERMINISTIC READS SQL DATA
BEGIN
DECLARE lv_id INT UNSIGNED;
-- DOES THIS COMMAND MAKE THE FUNCTION NOT-DETERMINISTIC?
CREATE TEMPORARY TABLE tmp_log(
order_nr VARCHAR(45)
, message VARCHAR(255)
, created_at DATETIME
);
-- AND/OR DOES THIS COMMAND MAKE THE FUNCTION NON-DETERMINISTIC?
INSERT INTO tmp_log
SET order_nr = pv_order_nr
, message = CONCAT( 'Id read for order ', pv_order_nr, '.')
, created_at = NOW();
SELECT so.id_sales_order
INTO lv_id
FROM sales_order AS so
WHERE so.order_nr = pv_order_nr
LIMIT 1;
RETURN lv_id;
END
$$
DELIMITER ;
As you see my function is declared as DETERMINISTIC.
My question is, does the second statement in the function routine body (CREATE TEMPORARY TABLE) make the function NOT DETERMINISTIC?
If I omit this statement, does the third statement (INSERT INTO --a temporary table--) make the function NOT DETERMINISTIC as well?
Thanks for reading this :)
Felix

Weird issue with a stored procedure in MySQL

I need to add a new stored procedure on our company's MySQL server. Since it's just slightly different, I used an already existing one, added the additional field and changed the name of the procedure. The weird thing now is that when I want to execute the statement, it returns:
Error Code: 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 '' at line 3
reffering to the 0 in this line: SET #update_id := 0; What makes it weird is, that I queried that stored procedure by using SHOW CREATE PROCEDURE . It's saved in our database and is working fine. I just can't use it as a new stored procedure (no matter if I try to apply it to the new test database or if I use it on the existing database by giving it a new name).
I searched the internet for a solution. Unfortunately to no avail. I even set up a new database with a new table and some demo values where I tried to execute the original, unaltered stored procedure. It returns the exact same error.
Here's the currently used and working stored procedure I'm talking about:
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
SET #update_id := 0;
UPDATE customer_shop SET state = 1, id = (SELECT #update_id := id), instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment WHERE tariff=Itariff AND state = 0 LIMIT 1;
SELECT * FROM customer_shop WHERE id = #update_id;
END
I hope you guys can help me as I am completely out of ideas what's wrong. :/
Regards, Mark
You need to define an alternative command delimiter, as MySQL currently thinks your CREATE PROCEDURE command ends at the first ; it encounters (on line 3, after the 0), which would be a syntax error as it's after a BEGIN but before the corresponding END:
DELIMITER ;; -- or anything else you like
CREATE PROCEDURE
...
END;; -- use the new delimiter you chose above here
DELIMITER ; -- reset to normal
MySQL stored procedures do not use ":=" for value assignment, just use "=".
Also don't think "id = (SELECT #update_id := id)" is acceptable. Here's an alternative solution (untested):
CREATE DEFINER=`root`#`localhost` PROCEDURE `customer_getcard`(IN Iinstance INT, IN Itimebuy DOUBLE, IN Iprice DECIMAL(10,2), IN Itariff INT, IN Icomment VARCHAR(128))
BEGIN
select id into #update_id from customer_shop WHERE tariff=Itariff AND state = 0 LIMIT 1;
UPDATE customer_shop SET state = 1, instance=Iinstance, timebuy=Itimebuy, price=Iprice, comment=Icomment where id = #update_id;
SELECT * FROM customer_shop WHERE id = #update_id;
END
You may also want to put error handlers in case there's no matching row to be edited.

MySQL Stored Procedure Declare Issue

This problem has cost me almost an hour now and I know it is something simple.
I am getting the following 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 'IN VARCHAR(256), hl7PatientId IN VARCHAR(256))
BEGIN
DECLARE mainQueue INT' at line 1
Here is my query which looks right to me:
DROP PROCEDURE IF EXISTS insert_data;
CREATE PROCEDURE `insert_data`(hl7PatientName IN VARCHAR(256), hl7PatientId IN VARCHAR(256))
BEGIN
DECLARE mainQueue INT DEFAULT 1;
SELECT `queueid` INTO mainQueue FROM `queues` WHERE `description` LIKE 'Main' AND `enabled` = 1 LIMIT 1;
INSERT INTO `queue_data`
(`queueid`, `patientname`, `patientid`, `location`, `creationtime`, `priority`)
VALUES
(mainQueue, hl7PatientName, hl7PatientId, 'QUEUE_NUMBER', TIMESTAMP(), '');
END;
I am using MySQL 5.0.77 for this.
Can anybody see anything in this that is wrong?
i've tidied up your example a little - note the use of delimiter and in params !
drop procedure if exists insert_queue_data;
delimiter #
create procedure insert_queue_data
(
in p_patientname varchar(255), -- size ? i always prefix my params p_ and keep the same name as the db field
in p_patientid varchar(255) -- size ? are you sure this isnt an integer ?
)
begin
-- i always prefix my variables v_ and keep same name as the db field
declare v_queueid int unsigned default 1;
select queueid into v_queueid from queues where
description like 'Main' and enabled = 1 limit 1;
insert into queue_data(queueid, patientname, patientid, location, creationtime, priority) values
(v_queueid, p_patientname, p_patientid, 'QUEUE_NUMBER', now(), '');
end#
delimiter ;
Reverse the order of IN and parameter name.
...(IN hl7PatientName VARCHAR(256), IN hl7PatientId VARCHAR(256))...