Select query output in mysql and differes between SP and query execution - mysql

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)

Related

MYSQL - Parameter being lost when using it in SQL statement

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?

In and Out Paramater Confusion in stored procedure of Mysql

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...!!

Selecting all values from a table in mysql using a stored procedure

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

MYSQL: stored procedure not executing query correctly with 'IN' clause correctly

I'm trying to pass a string value to a stored procedure from my code but it is not executing the query correctly. Following is the code of stored procedure.
I want to pass multiple values to the stored procedure like '2,3,4'. But when I do it only takes 2 and throws an error.
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE ID IN (RECORD_ID);
END
You have to use FIND_IN_SET() function instead of IN operator
Try this:
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE FIND_IN_SET(ID, RECORD_ID);
END

Querying on the stored procedure result set

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).