Result consisted of the more than one row error correction

Source: Internet
Author: User

Today, as usual, we sent out a list of props.

I soon produced a script to release the props, to the elder brother to execute. But then he came to me and said that most of the accounts were executed successfully, and there was a small number of account execution errors. The specific error is:

Mysql> call Guocui3. Issueavataritems (' jiangming ', @ResultString);
ERROR 1172 (42000): result consisted of the more than one row


When you see this error, you know that there are more than one row when you use SELECT INTO, causing issueavataritems the stored procedure to execute an error.

This opens the stored procedure to view its contents as follows:

DROP PROCEDURE IF EXISTS issueavataritems;
	CREATE PROCEDURE issueavataritems (in PlayerName VARCHAR (m), out resultstring VARCHAR ()) BEGIN DECLARE int;
	DECLARE AID int;

	DECLARE Rcount int;

	SELECT ' Start processing player info ... ' into resultstring;
	SELECT Account.accountid into the AID from account where UserName = PlayerName;
	/*get the AccountId from the player's username*/SELECT found_rows () into Rcount; if (rcount=0) THEN # If there is no such player with the username SELECT CONCAT (' Player \ ', playername, ' \ ' are not fo und in gamedb! ')
	into resultstring; ELSE SELECT DISTINCT characterattr.gender into Gender from Characterattr, Gamecharacter WHERE characterattr.character 
		ID = Gamecharacter.characterid and gamecharacter.accountid= AID;
		IF (Gender =0) THEN # It is a Male call Issuemaleavataritem (AID);
		ELSEIF (Gender =1) THEN # If it is a Female call Issuefemaleavataritem (AID);

		End IF; SELECT ItemID from Playeritem WHERE accountid= Aid;/*checking inserting results*/SELECT found_rows () into Rcount; IF (rcount>5) THEN SELECT CONCAT (' Player: \ ', PlayerName, ' \ has been successfully given ', Rcount, ' Avatar items!
			') into resultstring; ELSE SELECT CONCAT (' Player: \ ', PlayerName, ' has errors in issuing Avatar ')
			into resultstring;
	End IF;
	SELECT resultstring;

End IF; End;

There are a lot of places to use SELECT into, minus two places to use Select Found_rows (), and there are only two left:

SELECT Account.accountid into the AID from account where UserName = PlayerName;

And

SELECT DISTINCT Characterattr.gender into Gender from Characterattr, Gamecharacter
			WHERE Characterattr.characterid = Gamecharacter.characterid
			

I think carefully, because the new increase in the game of landlords, the corresponding role of the game (Character) also increased, so the Characterattr table selected gender also may be multiple: for the game has been registered landlords, there will be two Character, Such a player will make an error while the stored procedure is executing, and if it is not registered to the landlord game, because there is only one role in the game, there will be no error at the time of execution.

Now that you know what's wrong, there's a way to fix it:

DROP PROCEDURE IF EXISTS issueavataritems;
	CREATE PROCEDURE issueavataritems (in PlayerName VARCHAR (m), out resultstring VARCHAR ()) BEGIN DECLARE int;
	DECLARE AID int;

	DECLARE Rcount int;

	SELECT ' Start processing player info ... ' into resultstring;
	SELECT Account.accountid into the AID from account where UserName = PlayerName LIMIT 1;
	/*get the AccountId from the player's username*/SELECT found_rows () into Rcount; if (rcount=0) THEN # If there is no such player with the username SELECT CONCAT (' Player \ ', playername, ' \ ' are not fo und in gamedb! ')
	into resultstring; ELSE SELECT DISTINCT characterattr.gender into Gender from Characterattr, Gamecharacter WHERE characterattr.character 
		ID = Gamecharacter.characterid and gamecharacter.accountid= AID LIMIT 1;
		IF (Gender =0) THEN # It is a Male call Issuemaleavataritem (AID);
		ELSEIF (Gender =1) THEN # If it is a Female call Issuefemaleavataritem (AID);

		End IF; SELECT ItemID from Playeritem WHERE Accountid= aid;/*checking inserting results*/SELECT found_rows () into Rcount; IF (rcount>5) THEN SELECT CONCAT (' Player: \ ', PlayerName, ' \ has been successfully given ', Rcount, ' Avatar items!
			') into resultstring; ELSE SELECT CONCAT (' Player: \ ', PlayerName, ' has errors in issuing Avatar ')
			into resultstring;
	End IF;
	SELECT resultstring;

End IF; End;

Note that the height of the part, in fact, where there are multiple results can be added to the limit 1, so that you can ensure that a unique row of data in a column can be assigned to the corresponding variable.

After modification, normal execution is performed on the test environment. has been cloth to production up, and then carry out the script to release props, all normal.

This question reminds us that when using a stored procedure or function, if you use the SELECT INTO statement, make sure that the returned data is the only column. Of course, if you're using a system function such as count (), Found_rows (), you don't have to worry about that.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.