Calling a function in MySQL database with table alias - mysql

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$$

Related

Mysql Procedure Syntax change to Firebird Procedure Syntax

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;

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

Forward Engineering Database in MySQL Workbench - Error 1304: FUNCTION GET_NEXT_SEQ already exists

I am new to Mysql workbench and I am trying to forward engineer a EER model to a test database, however when I do so I get this error and I am not sure what it means.
I'm tempted to remove the function definition as it's already defined elsewhere which I imagine would solve this error, however because I don't know if the original definition is the same as the new one I'm reluctant to just trust this method.
Executing SQL script in server
ERROR: Error 1304: FUNCTION GET_NEXT_SEQ already exists
SQL Code:
CREATE DEFINER=`masteruser`#`%` FUNCTION `GET_NEXT_SEQ`(`SEQNAME` VARCHAR(50)) RETURNS varchar(50) CHARSET utf8
BEGIN
DECLARE CURVALUE INT;
UPDATE sequence_log SET CURRENT_VALUE = (CURRENT_VALUE + 1) WHERE SEQUENCE_NAME = SEQNAME;
SELECT CURRENT_VALUE INTO CURVALUE FROM sequence_log WHERE SEQUENCE_NAME = SEQNAME;
RETURN LPAD(CURVALUE,9,0);
END
I got the EER from reverse engineering the same database.
delimiter $$
CREATE FUNCTION `GET_NEXT_SEQ_VAL`(`SEQNAME` VARCHAR(50)) RETURNS varchar(50) CHARSET utf8
BEGIN
DECLARE CURVALUE INT;
UPDATE sequence_log SET CURRENT_VALUE = (CURRENT_VALUE + 1) WHERE SEQUENCE_NAME = SEQNAME;
SELECT CURRENT_VALUE INTO CURVALUE FROM sequence_log WHERE SEQUENCE_NAME = SEQNAME;
RETURN LPAD(CURVALUE,9,0);
END
You can simply create new function with new name.

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