stored procedure in mysql returning null - mysql

I have the following table created in mysql database.
create table stud_info(Student_ID int,Name varchar(10),Class varchar(10),Marks int)
I have also created a stored procedure to retrieve the name given the id like below:
DELIMITER //
create procedure selectEmp2(IN num1 INT,OUT name varchar(20))
BEGIN
select Name INTO name from myDB.stud_info where Student_ID=num1;
END//
When I am calling the stored procedure , I am getting null value. Please let me know where I am going wrong.

I think your stored procedure should work, but I would advise giving names to parameters that are likely to be unique. I also prefer explicit variable assignment, because select into can mean different things. Does this work?
DELIMITER //
create procedure selectEmp2(IN in_num1 INT, OUT out_name varchar(20))
BEGIN
select si.Name into out_name
from myDB.stud_info si
where si.Student_ID = in_num1;
END;//

Try this:
DELIMITER //
create procedure selectEmp2(IN _num1 INT,OUT _name varchar(20))
BEGIN
select Name INTO _name
from myDB.stud_info
where Student_ID=_num1;
END//

Related

Not receiving input value in stored procedure

I m passing parameters in stored procedures but unable to fetch records when filtering with inputs.But when I store that Input into variables and pass it to fetch record I get the records.why?
Here is stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = ip_Id;
END$$
DELIMITER;
I tried to take input in variable and pass it to where clause ti fetch records .
Here is the method that I have tried. And it works why?
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN ip_Id VARCHAR (100))
BEGIN
Select ip_Id into #ip_Id;
SELECT * FROM table_namel IAS WHERE Id = #ip_Id;
END$$
DELIMITER;
We didn't know the structure of table_namel, but it seems the table have a field with same name as input parameter.
To prevent that use the parameters with some prefix/surfix:
DELIMITER $$
DROP PROCEDURE IF EXISTS Demo_akg$$
CREATE PROCEDURE Demo_akg (IN p_ip_Id VARCHAR (100))
BEGIN
SELECT * FROM table_namel IAS WHERE Id = p_ip_Id;
END$$
DELIMITER;

SQL stored procedure- where am I going wrong?

I'm trying to create a stored procedure that calculates total revenue from a customer by if it's occupied and the standard rate. I am getting an error message and when I try to call from it I get NULL. Can anyone help? Thanks.
//Delimiter
CREATE PROCEDURE calculateRevenue (in customerIDs int, OUT totalRevenue dec(15,2))
BEGIN
SELECT SUM(Occupied*StandardRate) into totalRevenue FROM climatesouth
WHERE customerIDs = customerID;
END //
delimiter//
call calculateTotal(10, #totalRevenue);
SELECT #totalRevenue;
First you need to give your input parameters different names from the columns. Then you need to use them. Also, DELIMITER goes before the stored procedure definition:
DELIMITER //
CREATE PROCEDURE calculateRevenue (
in in_customerIDs int,
out out_totalRevenue dec(15,2))
BEGIN
SELECT SUM(cs.Occupied cs.* cs.StandardRate) into out_totalRevenue
FROM climatesouth cs
WHERE cs.customerID = in_customerID;
END //
The delimiter assignment is off: your are setting it to // after the create procedure statement.
Also, the parameter name needs to be fixed: your are not using the correct name in the query (your parameter has a trailing 's'), because of which the procedure will not produce the result you expect.
So:
delimiter // -- change the default delimiter here
create procedure calculaterevenue (in p_customerid int, out p_totalrevenue dec(15,2))
begin
select sum(occupied * standardrate) into p_totalrevenue
from climatesouth
where customerid = p_customerid; -- "p_customerid" is the parameter name
end //
delimiter ; -- reset the delimiter, now we can call the procedure
call calculaterevenue(10, #totalrevenue);
select #totalrevenue;
It is easier just to return the result as a result set rather than to use the OUT-parameter. The OUT-parameters are usually used only when calling procedure from another procedure. If you call the procedure from your application, use the result set.
delimiter //
CREATE PROCEDURE calculateRevenue (in_customerID int)
BEGIN
SELECT SUM(Occupied*StandardRate) as totalrevenue
FROM climatesouth
WHERE customerID = in_customerID;
END
//
delimiter ;
call calculateRevenue(10);

MySQL Stored Procedure Returning Wrong Result

I cannot work out what I am doing wrong. My tables has 1262717 rows.
I have this SQL:
SELECT count(hud_case_number) FROM suitecrm.ht_homes where hud_case_number = "abc"
Which correctly replies with 0
When I convert it to a stored procedure:
CREATE DEFINER=`root`#`%` PROCEDURE `hud_does_property_exist`(IN hud_case_number varchar(20))
BEGIN
SELECT count(hud_case_number) FROM suitecrm.ht_homes where hud_case_number = hud_case_number;
END
and call it like this:
Call hud_does_property_exist('abc')
It returns the answer 1262717
I just cannot see what I am doing wrong. I suspect it's something to do with the hud_case_number parameter?
Try This :-
CREATE DEFINER=`root`#`%` PROCEDURE `hud_does_property_exist`(IN
in_hud_case_number varchar(20))
BEGIN
SELECT count(hud_case_number) FROM suitecrm.ht_homes where hud_case_number =
in_hud_case_number;
END

Got error when creating procedure

I want to get the info from table s via inputting specific student name.
Code goes like this:
delimiter //
`CREATE PROCEDURE get_info_hujiamin (IN sn_input VARCHAR)
BEGIN
SELECT *
FROM s
WHERE s.sn=sn_input;
END`
//
Do query like below: here you haven't added length of VARCHAR
DELIMITER //
CREATE PROCEDURE GetOfficeByCountry(IN countryName VARCHAR(255))
BEGIN
SELECT *
FROM offices
WHERE country = countryName;
END //
DELIMITER ;
You can also refer My SQL Procedure

Mysql stored procedure don't take table name as parameter

I've written a stored procedure. It's working fine except taking the table name as input parameter.
Let see my proc in MySQL:
DELIMITER $$
USE `db_test`$$
DROP PROCEDURE IF EXISTS test_proc$$
CREATE DEFINER=`root`#`localhost`
PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SELECT COUNT(*) FROM newsInfoTable WHERE newsServiceName=serviceName;
END$$
DELIMITER ;
Stored procedure calling parameters:
USE db_test;
CALL test_proc('abc','tbl_test_news');
Here the service name parameter is working fine. But if I include the newsInfoTable variable as table input parameter then a error shows.
Table 'db_test.newsinfotable' doesn't exist
Why does this happen only for table parameter? How can I retrieve from this error or
How I pass a table name into a stored procedure as a parameter?
An SP cannot be optimized with a dynamic table name, so many DBs, MySQL included, don't allow table names to be specified dynamically.
One way around this is to use Dynamic SQL.
CREATE DEFINER=`root`#`localhost` PROCEDURE `test_proc`(IN serviceName VARCHAR(10),IN newsInfoTable VARCHAR(100))
BEGIN
SET #sql = CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=?;');
PREPARE s1 from #sql;
SET #paramA = serviceName;
EXECUTE s1 USING #paramA;
END$$
You can use EXECUTE IMMEDIATE for a "less is more" solution (for me, less code = good)
CREATE PROCEDURE test_proc(IN serviceName VARCHAR(10), IN newsInfoTable VARCHAR(100))
BEGIN
EXECUTE IMMEDIATE CONCAT('SELECT COUNT(*) FROM ',newsInfoTable,' WHERE newsServiceName=''', serviceName, '''');
END
that part of a query cannot be dynamic.
you may consider implementing as a string that is executed dynamically at runtime
Although may not be what you want, alternatively, can consider to use conditionally if and prepare the statement.
DELIMITER $$
CREATE PROCEDURE select_count(IN table_name VARCHAR(20))
BEGIN
IF table_name = 'xxx' THEN
SELECT * FROM xxx;
ELSEIF table_name = 'yyy' THEN
...
ENDIF
END$$