I am new to stored procedures in Mysql and working on some basic stored procedures to fetch data from a single table based on multiple conditions. I am a bit confused in IN and OUT parameters which stored procedure accept and returns respectively.
Below is the select statement for which I am creating a stored procedure.
select name from item where id in
(select item_id from stock where created_by_id != processor_id and processor_id in
(select id from users where type ='Manager'));
name is a column in item table from which I want to get data based on a condition. when I am running a above query..it returns me correct output as below.
#name
'Samsung'
'Pant'
but when I am running the same statement using a stored procedure as below.
DROP PROCEDURE IF EXISTS sp_ItemName3;
delimiter //
CREATE PROCEDURE sp_ItemName3 (IN userType varchar(255), OUT Name varchar(255))
BEGIN
select name from item where id in
(select item_id from stock where created_by_id != processor_id and processor_id in
(select id from users where type = userType));
END
//
Call sp_ItemName3('Manager',#Name);
After calling the stored procedure it returns me two NULL rows. what is the reason? mysql workbench is not returning any error/exception.
Correct me..if I am doing something wrong...!!
Related
I have a stored procedure in MYSQL where I am passing one parameter which is passed into an SQL statement as you can see below however the result is returning a count of 0 where I am expecting a count of 2.
Stored Procedure:
CREATE DEFINER=`admin`#`%` PROCEDURE `EmployeesRecords`(IN employee_id varchar (1000))
BEGIN
--
declare v_count int ;
--
select count(*)
into v_count
from employees
where employees_id IN (employee_id);
--
END
One or many employee Id's can be passed into the parameter employee_id.
when Calling Stored Procedure like this : CALL EmployeesRecords('2,3'); This returns a count of 0 where I am expecting a count of 2
As for the parameter itself, I have tried various methods including changing it in the procedure to have it as "IN ('2','3') in the SQL condition however it still does not work.
However what I have noticed is that when passing one employee Id, it works successfully such as CALL EmployeesRecords('2');
Can anyone guide me to what I am doing wrong please?
Why is my query output different between SP and query execution. I am executing a simple select query to assign some value..
SP:
CREATE DEFINER=`root`#`localhost` PROCEDURE `INS_EXPENSES`(
IN CATNAME varchar(100),
IN SUBCATNAME varchar(100),
IN AMOUNT INT,
IN USERID INT,
IN DESCRIPTIONS varchar(100)
)
BEGIN
SELECT SUBCATNAME;
SELECT * FROM SUBCAT WHERE SUBCATNAME='MISC';
END
THE ABOVE SELECT QUERY RETURNS ALL DATA FROM THE SUBCAT TABLE. WHERE IN SAME QUERY WORKS FINE IF EXECUTED OTHERWISE.
Your procedure has parameter SUBCATNAME and the WHERE-clause is using that value instead of the column with same name when it compares with the "MISC"-string.
You should use a prefix for the procedure parameters so that they would not get mixed up with column names (use parameter names like in_SUBCATNAME or similar)
I have a table called Contacts with a field called person_id that I have connected to a java application.
If no value is specified for person_id in the application, I want to select everything from the contacts table using a stored procedure.
The operation I want to perform is this:
Select * from Contacts where (person_id like "%")
For this I have written a stored procedure shown below:
Delimiter $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `selectTest2`(In p_id int(11))
BEGIN
if p_id = null then
set p_id = "%";
end if;
select * from Contacts where (person_id like p_id);
END $$
Delimiter ;
However when I run this procedure in my sql using the following
call selectTest2(null)
The table that is returned is blank. How do I make it show all the values in the table?
The parameter p_id gets its value from a text box in the application. If the user has entered an id, I want the procedure to show only that particular record else I want it to show all records.
What have I done wrong and how do I correct it? I am aware that p_id is an int however I tried the same thing with other fields of type varchar and the table failed to return any value.
Try using case statement in where clause like below
WHERE CASE WHEN p_id IS NOT NULL THEN person_id = p_id ELSE TRUE END
Hope this should solve your problem
Im using SQL Server 2008. I have a result set from the stored procedure and i want to fire some more queries on the result set for e.g order by desc /asc and some querying. So what is the best way to do it. Using views or by using OPENQUERY.
Please help.
You can save results of stored procedure calls in any table or table variable that has same number and type of fields as the result set returned by the stored procedure, for example:
CREATE TABLE #temp(col1 INT, col2 VARCHAR(10))
INSERT INTO #temp(col1, col2)
EXEC usp_Proc1(#param1)
SELECT *
FROM #temp
Condition is that usp_Proc1 returns rows consisting of columns of type INT and VARCHAR(10) (in that order).
I noticed that a select statement inside my stored procedure is always returning the same value, no matter what parameter I'm calling my stored procedure with. Here's the code:
DELIMITER $$
CREATE PROCEDURE TEST(IN id INT)
BEGIN
DECLARE x INT DEFAULT 0;
select id;
SELECT paper_id
INTO x
FROM Paper
WHERE ID = id
limit 1;
select x;
END$$
x always returns the same value no matter what id I call test with. I noticed the value of x is always the value of paper_id in the first row of my Paper table.
However, if I run the same query outside of the stored procedure, I get the expected value.
What is going wrong inside the stored procedure which skews that value?
I'm not that familiar with MySQL's stored procedures, but could it be that the expression WHERE ID = id is evaluated as "all rows from Paper where the value in the column ID equals the value in the column ID" and simply ignores your parameter?
Try to rename your parameter to something that is different from the column name in your query.