I am calling one procedure from another
CREATE PROCEDURE choice_select(IN choice_value BIGINT)
BEGIN
DECLARE AA varchar(100);
CALL TEST(choice_value,AA);
SELECT description
FROM academic
where {select a};
END
TEST is my another procedure and I want to pass the value returned from TEST procedure; in the WHERE clause, wherea is an output variable.
Plan A:
Use FIND_IN_SET function to filter records -
BEGIN
SET #aa = NULL;
CALL test(#aa); -- test should return a string like this - '1,2,3,4,5'
SELECT description FROM academic WHERE FIND_IN_SET(academic_id, #aa);
END
Plan B:
Populate a (temporary) table in the test procedure, then join this table with academic table.
Related
I am creating a very simple store procedure with a query, but when i use the store procedure IN parameter in the query it gets stuck and does not execute the query, but if i put the value direct to the query it works.
This works:
CREATE PROCEDURE `cap-reports`.ffap_test()
BEGIN
select * FROM students WHERE name='Fernando';
END
This does not, i spent 10 minutes and it never returned
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM students WHERE name=pName;
END
call `cap-reports`.ffap_test('Fernando');
What mistake i am doing here? I never had this problem before
This procedure works for me. Maybe it's the difference in database of the procedure and the students table? Or a missing semi-colon?
CREATE PROCEDURE `cap-reports`.ffap_test(IN pName VARCHAR(10))
BEGIN
select * FROM `cap-reports`.members m WHERE m.Username = pName;
END
;
CALL `cap-reports`.ffap_test('winkbrace');
It appears that performing a where clause on a table with Mysql works as expected (first code example).
However, when a where clause is performed on a view, the where clause appears to be ignored.
To keep things simple, I've tested a simple where clause on a standard table- this performs as expected, additionally a 'proof test' was ran to see if a mysql procedure can do a simple select * on a view (it did). Thus, it's only when the where clause is added to quiers on views.
How can I perform a WHERE query within a stored procedure on a View?
Example code 1 (working with normal table):
DELIMITER //
CREATE PROCEDURE p1 (id INT, fname VARCHAR(250))
BEGIN
DECLARE staffId INT;
DECLARE name VARCHAR(250);
SET name = fname;
SET staffId = id;
SELECT * FROM Staff WHERE ID_Staff = staffId AND Fname = name;
END; //
DELIMITER
Result set returned from the above performs as expected, acting upon the where clause.
However, with when using a where clause on a view the where clause is ignored:
Example code (not working as expected)
DELIMITER //
CREATE PROCEDURE p2 (year INT)
BEGIN
DECLARE a CHAR(4);
SET a = year;
SELECT * FROM TotalHoursView WHERE Year = "2014";
END; //
DELIMITER ;
Note: 'TotalHoursView' is a view, not a table.
The result of the second procedure ignores the where clause, always returning the full contents of the view (SELECT * FROM TotalHoursView).
Does Mysql not support where clauses on views when called in a stored procedure?
http://dev.mysql.com/tech-resources/articles/mysql-storedprocedures.pdf
Try to use another names for your variables, it's not CASE sensitive
I'm work with store procedures, supose that I've the following procedure that return a value, and this value I use in other query.
CREATE PROCEUDRE filter(IN string varchar(1000), OUT salida varchar(1000))
BEGIN
.....
END
And I want make a insert with a select query for example:
INSERT INTO otherTable
SELECT filter(concat_group(column)) , value1,value2 from mytable
GROUP BY column,value,value2;
which is the correct way to do this?
Generally, you cannot call a stored procedure in the SQL select statement. What you want is like custom scalar functions.
reference
mysql scalar function with for loop
i just created a stored procedure that take a parameter(example id) and copies columns related to that id from one table to another table.
How can i create stored procedure that takes sub query results as parameter,database is mysql..
This is my example..i want to pass query that select id from table to procedure..
DELIMITER $$
DROP PROCEDURE IF EXISTS `test`.`sasi`$$
CREATE DEFINER=`root`#`localhost` PROCEDURE `sasi`(IN idno int(4))
BEGIN
INSERT INTO user5(id,email,address,fullname,gender,phonenumber)
SELECT id,email,address,fullname,gender,phonenumber FROM user1 where id != idno;
END$$
DELIMITER ;
call sasi(4);
To pass the results of a query into your stored procedure, wrap the query in brackets.
For example:
call sasi((select max(id) from sometable where somecondition));
You must make sure the query only returns one row and one column.
Edited:
If you want to call the procedure multiple times, once for each row, change your procedure to be a FUNCTION:
CREATE FUNCTION sasi(idno int(4))
RETURNS int(4)
BEGIN
INSERT INTO user5(id,email,address,fullname,gender,phonenumber)
SELECT id,email,address,fullname,gender,phonenumber FROM user1 where id != idno;
RETURN idno;
END
Then call it like this:
select sasi(id)
from table
where ...
sasi(id) will get called for every row matching the where clause.
I'm new to stored procedure and I don't know much.
I'm testing with an example. Could you help me?
Here is my stored procedure
DELIMITER $$
DROP PROCEDURE IF EXISTS dictionarytable$$
CREATE PROCEDURE dictionarytable(id VARCHAR(20),name
VARCHAR(20),work VARCHAR(20),place VARCHAR(20),mobileno
VARCHAR(20),bike VARCHAR(20),car VARCHAR(20),homeno
VARCHAR(20),dictionaytype VARCHAR(20),meaning VARCHAR(20),sentence
VARCHAR(20),antonym VARCHAR(20),synonym VARCHAR(20))
BEGIN
select
id,name,work,place,mobileno,bike,car,homeno,dictionaytype,meaning,sentence,antonym,synonym
from dictionary INTO dictionarytable; END $$
DELIMITER ;
I wanted id,name,13 columns from dictionary(table) to be called in stored procedure dictionarytable
the query in the Begin is wrong could you specify a query to display all 13 columns
You cannot pass field values INTO the procedure, you can pass them INTO user variables, declared variables or OUT paramaters. Note, that only one record can be passed when INTO clause is used. For example:
SET #var1 = NULL;
SELECT column1 INTO #var1 FROM table;
If you want to copy more then one record, then you can use INSERT INTO...SELECT statement to copy data-set to second table. For example:
INSERT INTO table2 SELECT column1 FROM table;
Also, if you want to use variables or parameters as identifiers (field names in your case), then you should use prepared statements.