MySQL Forums
Forum List  »  Stored Procedures

SELECT id, myFunc(id) FROM aTable ORDER BY id; returns bad data
Posted by: Mitch Stone
Date: September 22, 2005 04:49PM

Would someone verify that I'm not doing something wonky here, and that this is indeed a bug?

I defined a table with an auto-increment column and several string fields, and created a stored function that takes as its parameter the id field, which concats to of the other string fields. When I query the table with the id and the function call in the SELECT list, the function call produces erroneous data when the query is sorted by the id field. Specifically, the data returned by the function appears to be one-off, and yet it's the same length as the what the correct answer should be.

Thanks,
Mitch

-- Execute the following

DROP TABLE IF EXISTS users;

CREATE TABLE users (
userID int(11) NOT NULL auto_increment,
userName varchar(64) NOT NULL,
firstName varchar(32) default NULL,
lastName varchar(32) default NULL,
CONSTRAINT pkUsers PRIMARY KEY (userID)
);


DROP FUNCTION IF EXISTS GetUserFullName;

DELIMITER %%;
CREATE FUNCTION GetUserFullName(
usrID INTEGER
) RETURNS varchar(32)
BEGIN
DECLARE uName VARCHAR(32) DEFAULT null;

IF EXISTS (SELECT count(*) FROM users WHERE userID = usrID) THEN
SELECT concat(firstName, ' ', lastName) INTO uName FROM users WHERE userID = usrID;
END IF;

RETURN uName;
END%%
DELIMITER ;%%


INSERT INTO users (userName, firstName, lastName) VALUES
('xena','Warrior','Princess'),
('gabrielle','Amazon','Princess'),
('batman','Bruce','Wayne'),
('spiderman','Peter','Parker');

SELECT * FROM users;
-- returns data as expected

SELECT DISTINCT userID, userName, GetUserFullName(userID) AS fullname
FROM users;
-- returns data as expected, default sorted by userID

SELECT DISTINCT userID, userName, GetUserFullName(userID) AS fullname
FROM users ORDER BY userName;
-- returns data as expected, sorted by userName
/*
+--------+-----------+------------------+
| userID | userName | fullname |
+--------+-----------+------------------+
| 3 | batman | Bruce Wayne |
| 2 | gabrielle | Amazon Princess |
| 4 | spiderman | Peter Parker |
| 1 | xena | Warrior Princess |
+--------+-----------+------------------+
*/

SELECT DISTINCT userID, userName, GetUserFullName(userID) AS fullname
FROM users ORDER BY userID;
-- data sorted by userID, but fullname column is WONKY
/*
+--------+-----------+------------------+
| userID | userName | fullname |
+--------+-----------+------------------+
| 1 | xena | Amazon Princesss |
| 2 | gabrielle | Bruce Waynecess |
| 3 | batman | Peter Parke |
| 4 | spiderman | Peter Parker |
+--------+-----------+------------------+
*/

SELECT DISTINCT userID, userName, CONCAT(firstName, ' ', lastName) AS fullname
FROM users ORDER BY userID;
-- returns data as expected, sorted by userID

Options: ReplyQuote




Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.