Mysql Procedure Syntax change to Firebird Procedure Syntax - mysql

I don't know what's different about the stored procedure of firebird syntax.
MySql Procedure:
ALTER PROCEDURE [dbo].[SP_CAL_SHIFTDTL]
(
#PSHIFTDTEFROM DATETIME,
#PSHIFTDTETO DATETIME
)
AS
BEGIN
SET NOCOUNT ON
DECLARE #GEN_EXCEPTION_FROM DATETIME,
#GEN_EXCEPTION_TO DATETIME
SELECT #GEN_EXCEPTION_FROM = #PSHIFTDTEFROM,
#GEN_EXCEPTION_TO = #PSHIFTDTETO
How can I change this one syntax for Firebird stored procedure?
SELECT #GEN_EXCEPTION_FROM = #PSHIFTDTEFROM,
#GEN_EXCEPTION_TO = #PSHIFTDTETO

The equivalent in Firebird PSQL syntax would be simple assignment, so GEN_EXCEPTION_FROM = PSHIFTDTEFROM; See also Assignment Statements in the Firebird Language Reference. The full equivalent of the fragment shown in your question would be:
RECREATE PROCEDURE SP_CAL_SHIFTDTL
(
PSHIFTDTEFROM TIMESTAMP,
PSHIFTDTETO TIMESTAMP
)
AS
DECLARE GEN_EXCEPTION_FROM TIMESTAMP;
DECLARE GEN_EXCEPTION_TO TIMESTAMP;
BEGIN
GEN_EXCEPTION_FROM = PSHIFTDTEFROM;
GEN_EXCEPTION_TO = PSHIFTDTETO;

Related

MySQL procedure with query string

I am a newbie to using query strings in mysql and I have tried to write this procedure to drop tables under certain conditions. I don't reall know what I'm doing wrong and need help with getting the procedure to work or for someone to point me in the right direction. Thanks.
BEGIN
DECLARE String scheduler = 'select status from mysql.scheduler where id=0' ;
DECLARE String auftragpos = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftragpos';
DECLARE String auftrag = 'SELECT count("SchemaName") FROM "SYS.Tables" where "SchemaName" = dwh and "Name" = lexware_fk_auftrag';
IF(auftragpos > 1)
BEGIN
drop table "dwh.lexware_fk_auftragpos";
END
IF(auftrag > 1)
BEGIN
drop table "dwh.lexware_fk_auftrag";
END
END
The syntax for declaring a variable is
DECLARE variablename datatype;
So it should be
DECLARE auftragpos INT;
Then you need to assign the result of the query to the variable. You do that by putting the SELECT query in parentheses.
You also should not quote table names, and you need to quote the literal strings you're using for the schema and table names you're searching for.
SET auftragpos = (
SELECT count(*)
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_SCHEMA = 'dwh' AND TABLE_NAME = 'lexware_fk_auftragpos');
But there's really no need to do this. If you want to prevent an error from DROP TABLE, just add the IF EXISTS option.
DROP TABLE IF EXISTS dwh.lexware_fk_auftragpos;

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

Calling a function in MySQL database with table alias

Currently I have used imported a MySQL database in my current environment.My MySQL version is 5.6.x. Now I have certain database functions that I have to use in my java application. The problem is stated below:
MySQL database is EMPLOYEE.
MySQL function FN_GET_USER_CODE(userID INT) has used table
alias (EMPDTL) for table EMP_DETAILS_COMPANY and used some joins in function definition.
When I try to call the function via command
select FN_GET_USER_CODE(234599), It says Table EMPLOYEE.EMPDTL does not exist.
Error Code: 1146
Table 'EMPLOYEE.EMPDTL' doesn't exist
Function Definition
CREATE DEFINER=`user`#`192.168.0.1` FUNCTION `FN_GET_USER_DETAILS(userID INT) RETURNS VARCHAR(25) CHARSET utf8
BEGIN
DECLARE user_getempcode VARCHAR(100);
SELECT DISTINCT(reg.empcode) INTO user_getempcode FROM m_registration_seq_num AS reg
INNER JOIN EMP_SECRET_CODE ecd
ON reg.tempcd = ecd.secretcd AND reg.lang_cd = ecd.lang_cd
RETURN user_getempcode;
END$$
DELIMITER ;
Can anyone has idea of what I am missing here.
I think you are missing AS before alias name of table EMP_SECRET_CODE . it should be like this:
CREATE DEFINER=`user`#`192.168.0.1` FUNCTION `FN_GET_USER_DETAILS(userID INT) RETURNS VARCHAR(25) CHARSET utf8
BEGIN
DECLARE user_getempcode VARCHAR(100);
SELECT DISTINCT(reg.empcode) INTO user_getempcode FROM m_registration_seq_num AS reg
INNER JOIN EMP_SECRET_CODE AS ecd
ON reg.tempcd = ecd.secretcd AND reg.lang_cd = ecd.lang_cd
RETURN user_getempcode;
END$$

MySQL - CREATE DEFINER syntax error

I am trying to update a stored function in our MySQL database. The update is to be released to multiple devices so I am doing it through an update.sql file.
Here is the function
DROP FUNCTION `STAFF_MPT`;
CREATE DEFINER=`jelena`#`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id` INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
DECLARE proc_total INT;
DECLARE proc_time INT;
SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);
SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
FROM stocktake_scans
WHERE stocktake_staff_id = par_stocktake_staff_id
);
RETURN (proc_total/proc_time)*3600;
END
It was just reported to me by the test team that the report that uses this function did not generate properly. I tried to run the code in PMA SQL query window and got the following:
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
Can someone tell me what am I missing? According to this, line 3 is empty, so how could it possibly have a syntax error?
As of MySQL docs:
If you use the mysql client program to define a stored program containing semicolon characters, a problem arises. By default, mysql itself recognizes the semicolon as a statement delimiter, so you must redefine the delimiter temporarily to cause mysql to pass the entire stored program definition to the server.
To redefine the mysql delimiter, use the delimiter command.
#juergen_d hinted in the comment: you have to define your procedure with a delimiter:
DROP FUNCTION `STAFF_MPT`;
delimiter ||
CREATE DEFINER=`jelena`#`%` FUNCTION `STAFF_MPT`(`par_stocktake_staff_id` INT) RETURNS DECIMAL(20,0) NOT DETERMINISTIC CONTAINS SQL SQL SECURITY DEFINER BEGIN
DECLARE proc_total INT;
DECLARE proc_time INT;
SET proc_total = (SELECT SUM(quantity) FROM stocktake_scans WHERE stocktake_staff_id = par_stocktake_staff_id);
SET proc_time = (SELECT TIMESTAMPDIFF( SECOND , MIN( scan_date ) , MAX( scan_date ) ) AS area_time
FROM stocktake_scans
WHERE stocktake_staff_id = par_stocktake_staff_id
);
RETURN (proc_total/proc_time)*3600;
END
||
delimiter ;

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.