Calculations within MySQL Stored Procedures












1















I am creating a stored procedure that, when you input a class for an item, it gives you the total value. Because of this, I am having to add a calculation to my query within the SP. I keep getting an error message that reads: #1172 - Result consisted of more than one row. My guess is that it's because it's pulling from two places to get the product. Here is my code for the SP:



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval int)
BEGIN
SELECT (price * numInStock) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;


Now, when I go to input anything and call the procedure, I am getting the error message. Here is how I was attempting to call:



CALL  totalValue('HW',@totalval); 
SELECT @totalval;


Do I need to define the calculation AS something before putting it into totalval? I attempted that, and it still gave me an error message.










share|improve this question























  • You can not hold multiple values in a single variable.

    – Vivek Khandelwal
    Nov 22 '18 at 5:16


















1















I am creating a stored procedure that, when you input a class for an item, it gives you the total value. Because of this, I am having to add a calculation to my query within the SP. I keep getting an error message that reads: #1172 - Result consisted of more than one row. My guess is that it's because it's pulling from two places to get the product. Here is my code for the SP:



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval int)
BEGIN
SELECT (price * numInStock) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;


Now, when I go to input anything and call the procedure, I am getting the error message. Here is how I was attempting to call:



CALL  totalValue('HW',@totalval); 
SELECT @totalval;


Do I need to define the calculation AS something before putting it into totalval? I attempted that, and it still gave me an error message.










share|improve this question























  • You can not hold multiple values in a single variable.

    – Vivek Khandelwal
    Nov 22 '18 at 5:16
















1












1








1








I am creating a stored procedure that, when you input a class for an item, it gives you the total value. Because of this, I am having to add a calculation to my query within the SP. I keep getting an error message that reads: #1172 - Result consisted of more than one row. My guess is that it's because it's pulling from two places to get the product. Here is my code for the SP:



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval int)
BEGIN
SELECT (price * numInStock) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;


Now, when I go to input anything and call the procedure, I am getting the error message. Here is how I was attempting to call:



CALL  totalValue('HW',@totalval); 
SELECT @totalval;


Do I need to define the calculation AS something before putting it into totalval? I attempted that, and it still gave me an error message.










share|improve this question














I am creating a stored procedure that, when you input a class for an item, it gives you the total value. Because of this, I am having to add a calculation to my query within the SP. I keep getting an error message that reads: #1172 - Result consisted of more than one row. My guess is that it's because it's pulling from two places to get the product. Here is my code for the SP:



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval int)
BEGIN
SELECT (price * numInStock) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;


Now, when I go to input anything and call the procedure, I am getting the error message. Here is how I was attempting to call:



CALL  totalValue('HW',@totalval); 
SELECT @totalval;


Do I need to define the calculation AS something before putting it into totalval? I attempted that, and it still gave me an error message.







mysql stored-procedures






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 22 '18 at 2:03









C ReillyC Reilly

365




365













  • You can not hold multiple values in a single variable.

    – Vivek Khandelwal
    Nov 22 '18 at 5:16





















  • You can not hold multiple values in a single variable.

    – Vivek Khandelwal
    Nov 22 '18 at 5:16



















You can not hold multiple values in a single variable.

– Vivek Khandelwal
Nov 22 '18 at 5:16







You can not hold multiple values in a single variable.

– Vivek Khandelwal
Nov 22 '18 at 5:16














1 Answer
1






active

oldest

votes


















2














You basically have multiple rows for itemClass = 'HW' in the hsitems table. SELECT .. INTO documentation specifically states:




The query should return a single row. If the query returns no rows, a
warning with error code 1329 occurs (No data), and the variable values
remain unchanged. If the query returns multiple rows, error 1172
occurs (Result consisted of more than one row). If it is possible that
the statement may retrieve multiple rows, you can use LIMIT 1 to limit
the result set to a single row.




However, based on your variable name, I feel that you are trying to SUM(price*numinstock) instead to get the total value. This will also ensure implicit aggregation (GROUP BY) into a single row.



Also, by declaring OUT totalval INT, your output will be converted to int, and you will lose values after decimal. So I have changed it to OUT totalval DECIMAL(10,2). When dealing with currency numbers, it is preferred to use DECIMAL instead of floating point types like FLOAT and DOUBLE.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval DECIMAL(10,2))
BEGIN
SELECT SUM(price * numInStock) into totalval -- changed to SUM()
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;




If you want to format the sum value upto two decimal places (like currency), we can utilize Format() function. Also, note that output will be String format now.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval VARCHAR(32))
BEGIN
SELECT FORMAT(SUM(price * numInStock), 2) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;





share|improve this answer


























  • @CReilly what do you mean ? are you using my query exactly ?

    – Madhur Bhaiya
    Nov 22 '18 at 14:22











  • Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

    – C Reilly
    Nov 22 '18 at 14:30






  • 1





    @CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

    – Madhur Bhaiya
    Nov 22 '18 at 14:32











  • It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

    – C Reilly
    Nov 22 '18 at 21:25






  • 1





    @CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

    – Madhur Bhaiya
    Nov 23 '18 at 4:17











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422904%2fcalculations-within-mysql-stored-procedures%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









2














You basically have multiple rows for itemClass = 'HW' in the hsitems table. SELECT .. INTO documentation specifically states:




The query should return a single row. If the query returns no rows, a
warning with error code 1329 occurs (No data), and the variable values
remain unchanged. If the query returns multiple rows, error 1172
occurs (Result consisted of more than one row). If it is possible that
the statement may retrieve multiple rows, you can use LIMIT 1 to limit
the result set to a single row.




However, based on your variable name, I feel that you are trying to SUM(price*numinstock) instead to get the total value. This will also ensure implicit aggregation (GROUP BY) into a single row.



Also, by declaring OUT totalval INT, your output will be converted to int, and you will lose values after decimal. So I have changed it to OUT totalval DECIMAL(10,2). When dealing with currency numbers, it is preferred to use DECIMAL instead of floating point types like FLOAT and DOUBLE.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval DECIMAL(10,2))
BEGIN
SELECT SUM(price * numInStock) into totalval -- changed to SUM()
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;




If you want to format the sum value upto two decimal places (like currency), we can utilize Format() function. Also, note that output will be String format now.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval VARCHAR(32))
BEGIN
SELECT FORMAT(SUM(price * numInStock), 2) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;





share|improve this answer


























  • @CReilly what do you mean ? are you using my query exactly ?

    – Madhur Bhaiya
    Nov 22 '18 at 14:22











  • Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

    – C Reilly
    Nov 22 '18 at 14:30






  • 1





    @CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

    – Madhur Bhaiya
    Nov 22 '18 at 14:32











  • It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

    – C Reilly
    Nov 22 '18 at 21:25






  • 1





    @CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

    – Madhur Bhaiya
    Nov 23 '18 at 4:17
















2














You basically have multiple rows for itemClass = 'HW' in the hsitems table. SELECT .. INTO documentation specifically states:




The query should return a single row. If the query returns no rows, a
warning with error code 1329 occurs (No data), and the variable values
remain unchanged. If the query returns multiple rows, error 1172
occurs (Result consisted of more than one row). If it is possible that
the statement may retrieve multiple rows, you can use LIMIT 1 to limit
the result set to a single row.




However, based on your variable name, I feel that you are trying to SUM(price*numinstock) instead to get the total value. This will also ensure implicit aggregation (GROUP BY) into a single row.



Also, by declaring OUT totalval INT, your output will be converted to int, and you will lose values after decimal. So I have changed it to OUT totalval DECIMAL(10,2). When dealing with currency numbers, it is preferred to use DECIMAL instead of floating point types like FLOAT and DOUBLE.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval DECIMAL(10,2))
BEGIN
SELECT SUM(price * numInStock) into totalval -- changed to SUM()
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;




If you want to format the sum value upto two decimal places (like currency), we can utilize Format() function. Also, note that output will be String format now.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval VARCHAR(32))
BEGIN
SELECT FORMAT(SUM(price * numInStock), 2) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;





share|improve this answer


























  • @CReilly what do you mean ? are you using my query exactly ?

    – Madhur Bhaiya
    Nov 22 '18 at 14:22











  • Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

    – C Reilly
    Nov 22 '18 at 14:30






  • 1





    @CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

    – Madhur Bhaiya
    Nov 22 '18 at 14:32











  • It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

    – C Reilly
    Nov 22 '18 at 21:25






  • 1





    @CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

    – Madhur Bhaiya
    Nov 23 '18 at 4:17














2












2








2







You basically have multiple rows for itemClass = 'HW' in the hsitems table. SELECT .. INTO documentation specifically states:




The query should return a single row. If the query returns no rows, a
warning with error code 1329 occurs (No data), and the variable values
remain unchanged. If the query returns multiple rows, error 1172
occurs (Result consisted of more than one row). If it is possible that
the statement may retrieve multiple rows, you can use LIMIT 1 to limit
the result set to a single row.




However, based on your variable name, I feel that you are trying to SUM(price*numinstock) instead to get the total value. This will also ensure implicit aggregation (GROUP BY) into a single row.



Also, by declaring OUT totalval INT, your output will be converted to int, and you will lose values after decimal. So I have changed it to OUT totalval DECIMAL(10,2). When dealing with currency numbers, it is preferred to use DECIMAL instead of floating point types like FLOAT and DOUBLE.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval DECIMAL(10,2))
BEGIN
SELECT SUM(price * numInStock) into totalval -- changed to SUM()
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;




If you want to format the sum value upto two decimal places (like currency), we can utilize Format() function. Also, note that output will be String format now.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval VARCHAR(32))
BEGIN
SELECT FORMAT(SUM(price * numInStock), 2) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;





share|improve this answer















You basically have multiple rows for itemClass = 'HW' in the hsitems table. SELECT .. INTO documentation specifically states:




The query should return a single row. If the query returns no rows, a
warning with error code 1329 occurs (No data), and the variable values
remain unchanged. If the query returns multiple rows, error 1172
occurs (Result consisted of more than one row). If it is possible that
the statement may retrieve multiple rows, you can use LIMIT 1 to limit
the result set to a single row.




However, based on your variable name, I feel that you are trying to SUM(price*numinstock) instead to get the total value. This will also ensure implicit aggregation (GROUP BY) into a single row.



Also, by declaring OUT totalval INT, your output will be converted to int, and you will lose values after decimal. So I have changed it to OUT totalval DECIMAL(10,2). When dealing with currency numbers, it is preferred to use DECIMAL instead of floating point types like FLOAT and DOUBLE.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval DECIMAL(10,2))
BEGIN
SELECT SUM(price * numInStock) into totalval -- changed to SUM()
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;




If you want to format the sum value upto two decimal places (like currency), we can utilize Format() function. Also, note that output will be String format now.



DELIMITER //
CREATE PROCEDURE totalValue (IN whichClass varchar(5),
OUT totalval VARCHAR(32))
BEGIN
SELECT FORMAT(SUM(price * numInStock), 2) into totalval
FROM hsitems
WHERE itemClass = whichClass;
END //
DELIMITER ;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 23 '18 at 16:31

























answered Nov 22 '18 at 5:08









Madhur BhaiyaMadhur Bhaiya

19.5k62236




19.5k62236













  • @CReilly what do you mean ? are you using my query exactly ?

    – Madhur Bhaiya
    Nov 22 '18 at 14:22











  • Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

    – C Reilly
    Nov 22 '18 at 14:30






  • 1





    @CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

    – Madhur Bhaiya
    Nov 22 '18 at 14:32











  • It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

    – C Reilly
    Nov 22 '18 at 21:25






  • 1





    @CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

    – Madhur Bhaiya
    Nov 23 '18 at 4:17



















  • @CReilly what do you mean ? are you using my query exactly ?

    – Madhur Bhaiya
    Nov 22 '18 at 14:22











  • Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

    – C Reilly
    Nov 22 '18 at 14:30






  • 1





    @CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

    – Madhur Bhaiya
    Nov 22 '18 at 14:32











  • It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

    – C Reilly
    Nov 22 '18 at 21:25






  • 1





    @CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

    – Madhur Bhaiya
    Nov 23 '18 at 4:17

















@CReilly what do you mean ? are you using my query exactly ?

– Madhur Bhaiya
Nov 22 '18 at 14:22





@CReilly what do you mean ? are you using my query exactly ?

– Madhur Bhaiya
Nov 22 '18 at 14:22













Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

– C Reilly
Nov 22 '18 at 14:30





Since this particular query/SP is dealing with money, where would I add the ROUND function to allow the call to return a number with 2 decimals? I added it into the SELECT line and it's still returning as a whole number.

– C Reilly
Nov 22 '18 at 14:30




1




1





@CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

– Madhur Bhaiya
Nov 22 '18 at 14:32





@CReilly Round will not return two decimal places if the value after rounding is a whole number. You can either use Format(number,2) or I generally prefer to do it in the application code.

– Madhur Bhaiya
Nov 22 '18 at 14:32













It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

– C Reilly
Nov 22 '18 at 21:25





It did not work for me. : The query itself returns the right number, but still with no decimals. I'm still trying to figure out what went wrong.

– C Reilly
Nov 22 '18 at 21:25




1




1





@CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

– Madhur Bhaiya
Nov 23 '18 at 4:17





@CReilly why do you need two decimal places ? Any sort of formatting to be done is recommended in the application code, eg: PHP, C++, etc. If using them as number, 2 decimal places won't make any difference. For display purposes, you can format in application code (don't use SQL for display related requirements - prefer to keep SQL end simple as much as u can)

– Madhur Bhaiya
Nov 23 '18 at 4:17


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53422904%2fcalculations-within-mysql-stored-procedures%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

404 Error Contact Form 7 ajax form submitting

How to know if a Active Directory user can login interactively

TypeError: fit_transform() missing 1 required positional argument: 'X'