MySQL; the function value doesn't appear in resulted table - mysql

I created a table with three fields and applied a created function to automatic concat two fields of the same record and the result appears in the third field of the table at the same record.
While the problem is whenever I do insert statement the result of the function appears as error but not on the table!!! the following is the code:
create table Tab (
stuId varchar (1),
stuName varchar (10)
recordCon varchar (20),
primary key (stuId));
Auto concat function for one record by input id:
DELIMITER //
CREATE FUNCTION AutoCon (id varchar (1)) RETURNS int DETERMINISTIC
BEGIN
DECLARE name varchar(10);
DECLARE Con varchar(20);
select stuName from Tab where stuId = id into name;
Select concat(id, name) into con;
RETURN con;
END
//
DELIMITER ;
insert into Tab values ('1a', 'Rami', AutoCon('1a'))
the result
a1 Rami null

FUNCTION AutoCon (id varchar (1)), but you pass 2 chars as the parameter - AutoCon('1a')
Something wrong there ...

Related

Ask how to pass in a parameter that returns the value of 2 columns in a table in the mysql function

DELIMITER $$
CREATE DEFINER=`root`#`localhost` FUNCTION `sumwhere`(`mus_level` INT) RETURNS int(11)
READS SQL DATA
begin
declare tong int;
select sum(us_id) into tong
from members
where us_level = mus_level
group by us_level;
return tong;
end$$
DELIMITER ;
CREATE FUNCTION FUNC_1_MANV
(
#MANV CHAR(10)
)
RETURNS #TABLE TABLE(HOTEN NVARCHAR(30), CHUCVU NVARCHAR(30))
AS
BEGIN
INSERT INTO #TABLE
SELECT TENNV , CHUCVU
FROM NHANVIEN
WHERE MANV=#MANV
RETURN
END
GO
--THỰC THI
SELECT * FROM dbo.FUNC_1_MANV('NV001')
GO
enter image description here
I want to do the same as below code func "FUNC_1_MANV" but using MySQL.
I want to display one more column of mine, like count(us_id) , or the value of a column (us_id) when I have where run on create function MySQL how. I see MySQL only return 1 parameter.

For each record in a table, insert a record in another table and put the key into the first table's record

I am localizing an application and need to convert existing TEXT columns to have multiple translated versions of the text.
The original table looks like this:
person
personId: INT
description: VARCHAR(20)
(other columns)
To store the localized text, I created a new table:
text
textId: INT (auto-incrementing)
languageId: INT
text: TEXT
I want to replace the person.description column with a new person.descTextId column.
For each person that has a value in the description column but descTextId is null:
Insert a new text record with that person's description (English
languageId=1)
Update the person's descTextId to the new textId
value
Then remove the person.description column.
What is the cleanest way to do all of this? Do I need to create a procedure and cursor or is there a clever MySQL syntax that would help with this? How would you do it?
I figured it out with a temporary procedure and loop.
DROP PROCEDURE IF EXISTS moveToTextTable;
DELIMITER ;;
CREATE PROCEDURE moveToTextTable()
BEGIN
DECLARE n INT DEFAULT 0;
DECLARE i INT DEFAULT 0;
DECLARE id INT DEFAULT 0;
SELECT COUNT(*) INTO n FROM person WHERE description IS NOT NULL AND description <> '';
WHILE i<n DO
SELECT personId INTO id FROM person WHERE description IS NOT NULL AND description <> '' AND descTextId IS NULL LIMIT 1;
INSERT INTO text (languageId, text) SELECT 1, description FROM person WHERE personId = id;
UPDATE person SET descTextId = LAST_INSERT_ID() WHERE personId = id;
SET i = i + 1;
END WHILE;
END;
;;
DELIMITER ;
CALL moveToTextTable();
DROP PROCEDURE IF EXISTS moveToTextTable;

function that returns a table sql

I have this function:
DROP FUNCTION IF EXISTS MinCarAltezza;
DELIMITER $$
CREATE FUNCTION MinCarAltezza(altezza INT)
RETURNS SchedaTab TABLE(
Nome varchar(64),
Cognome varchar(64),
ID_Scheda int(10),
Sequenza int(2),
nSerie int(2),
nRipetizioni int(2),
Carico_Minimo decimal(4,1),
Esercizio varchar(30),
PRIMARY KEY(Nome,Cognome,Esercizio)
)
AS BEGIN
INSERT SchedaTab
SELECT DISTINCT U.Nome,U.Cognome,P.ID_Scheda,P.Sequenza,P.nSerie,
P.nRipetizioni,MIN(P.Carico),P.Esercizio
FROM utente AS U, scheda AS S, programma AS P
WHERE U.CF=S.ID_Utente AND S.ID_Scheda=P.ID_Scheda AND U.Altezza>altezza
AND P.Carico<>0
AND S.ID_Ist NOT IN(SELECT CF FROM istruttore WHERE Stipendio>500)
GROUP BY U.Nome,U.Cognome,S.ID_Scheda
RETURN
END $$
DELIMITER ;
that gives me an error in the line 4 where I declare the return type TABLE.
Is there something I'm missing?
That's the db if someone needs it: http://pastebin.com/DWYqVBpa
thank you
In MySQL there is no "table" data type, hence the error message. As MySQL documentation on stored functions says, return value can be
Any valid MySQL data type
Therefore, a stored function cannot return with a table. I would change the function into a stored procedure and
have a select statement at the end of the procedure (MySQL return the result of the last select)
or create a temporary table with the data and return the name of the temporary table in an out parameter

MySQL Stored Procedure returns 0 in parameters from a SELECT

I created a Stored Procedure in MySQL to return some parameters from a SELECT, but doesn't work.
ever returns 0 in userID and otherID.
Someone can help me?
Thanks
DROP PROCEDURE IF EXISTS sp_XPTO;
DELIMITER ;;
CREATE PROCEDURE sp_XPTO(
OUT userID INT UNSIGNED,
OUT otherID INT UNSIGNED,
IN bookID INT UNSIGNED
)
BEGIN
SELECT userID=E.userID, otherID=E.otherID
FROM Exp as E
WHERE E.bookID = bookID;
END ;;
DELIMITER ;
UPDATED:
Sorry,
After I read a lot, I concluded that the error is in how I call the stored procedure, and Ravinder is correct.
Thanks
I am not sure if your intention is to check the existence of a value in database.
Because, your select statement is doing a comparison but not assignment.
SELECT userID=E.userID, otherID=E.otherID
With the above statement, you may be thinking that value of E.userID is assigned to the OUT parameter userID. But the syntax you used is wrong.
In the above statement, both of the expressions return a boolean. As userID value did not match with E.userID the comparison resulted a false and you saw a 0. And it was the same for the otherID variable.
To assign values to variables you have to use INTO instructional operator.
Example:
SELECT E.userID, E.otherID
INTO userID, otherID
Change your stored procedure as below:
DROP PROCEDURE IF EXISTS sp_XPTO;
DELIMITER //
CREATE PROCEDURE sp_XPTO(
OUT userID INT UNSIGNED,
OUT otherID INT UNSIGNED,
IN bookID INT UNSIGNED
)
BEGIN
SELECT E.userID, E.otherID
INTO userID, otherID
FROM Exp as E
WHERE E.bookID = bookID; -- <- there was a ')'. removed.
END;
//
DELIMITER ;
Call the procedure with desired parameters and read the out parameters.
call sp_XPTO( #userID, #otherID, 234 ); -- where book id is 234
select #userID, #otherID;

MySQL Function: Selecting and returning column

I have a table Processes, which consists of an ID (unsigned int, auto_increment) and Name (varchar, unique).
Selecting the following function with any input (whether in the table or not) always results in
ERROR 1172 (42000): Result consisted of more than one row
CREATE FUNCTION LookupOrInsertProcess(nametwo VARCHAR(255))
RETURNS INT UNSIGNED
BEGIN
DECLARE myid INT UNSIGNED;
SELECT ID INTO myid FROM Processes WHERE Name=nametwo;
RETURN myid;
END$$
However, selecting the below function always returns NULL:
CREATE FUNCTION LookupOrInsertProcess(nametwo VARCHAR(255))
RETURNS INT UNSIGNED
BEGIN
DECLARE myid INT UNSIGNED;
SELECT ID INTO myid FROM Processes WHERE Name=nametwo;
RETURN myid;
END$$
Furthermore, please note that the following does return the correct result (numbers 30 and 50 are arbitrary):
CREATE FUNCTION LookupOrInsertProcess(nametwo VARCHAR(255))
RETURNS INT UNSIGNED
BEGIN
DECLARE myid INT UNSIGNED;
SELECT ID INTO myid FROM Processes WHERE Name=nametwo;
IF myid IS NULL THEN
RETURN 30;
ELSE
RETURN 50;
END IF;
END$$
Any help is appreciated.
UPDATE: Edited to remove clash between table column and function param. I don't believe that's the issue.
UPDATE2: Please note that the following appears to work, both when the input param is or is not in the table. Why, without the coalesce(), does the function return NULL even for input params which are in the table?
CREATE FUNCTION LookupOrInsertProcess(nametwo VARCHAR(255))
RETURNS INT UNSIGNED
BEGIN
DECLARE myid INT UNSIGNED;
SELECT ID INTO myid FROM Processes WHERE Name=nametwo;
RETURN COALESCE(myid, 0);
END$$
Column names are not case-sensitive in MySql so you may find that where Name = name means 'give me every row'.
Try changing the input parameter to your procedure (and the corresponding condition in the query) to be srchName or something else different to name.
Try to rename input name to different name like inputName.
Update:
Another suggestion.
DECLARE myid INT UNSIGNED;
SET myid = (SELECT ID FROM Processes WHERE Name=nametwo);
RETURN myid;