mysql stored procedure, use column name as an argument - mysql

i have here a table of tapes with 3 fields:
Tapes
TapesID Title Qty
T1 BatDog 3
T2 UnderCat 2
T3 IronMouse 1
T4 Boys Zone 1
T5 RoboCat 1
i want to create a stored procedure that extract and displays specific id’s of tape by entering tape title as argument to the procedure. The procedure should be called list_tspec_id.
can u help me out?im having a hard time with this...
here's my code but it's not right:
create procedure tapesid
#columnname varchar
AS
begin
select #columnname from tapes
end
exec tapesid 'title'

You've tagged this as MySQL, so... here's a stored procedure, though it's unclear from your example what you're really wanting it to do or why.
The variables passed into a stored procedure as arguments don't use '#' in front of them.
DELIMITER $$
CREATE PROCEDURE list_tspec_id (IN my_title VARCHAR(254))
BEGIN
SELECT TapesID, Title, Qty FROM Tapes WHERE Title = my_title;
END $$
DELIMITER ;
then...
mysql> CALL list_tspec_id('BatDog');

Related

MySQL query does not execute if i use the Stored Procedure parameter in the query

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');

How to make a stored procedure in mySQL

I'm quite new to stored procedures in mySQL and I have been having trouble with some of my homework that I have been assigned.
It's asking me to accept cust_code as a procedure parameter in my database, and then it will delete said customer with the cust_code.
Then it will delete all associated rows with this customer in the tables 'Invoice', 'line'.
To create a procedure with a integer parameter do something like this:
DELIMITER $$
CREATE PROCEDURE `<procedure_name>`(IN `in_cust_code` INT)
BEGIN
DELETE
...
WHERE 1
AND `<table_name>`.`cust_code` = in_cust_code
...
END
DELIMITER ;

MySQL Procedure structure

I am trying to create a procedure to allow users to rent a car, there are numerous tables in my DB, the ones I am using are 'car' and 'customer'. I want the user to be able to insert a car registration and their mobile number, from here, a search will be conducted from the 'car' table to see if they car registration they inputted matches any stored in the 'car' table. Here is what I have so far -
CREATE PROCEDURE new_loan
(
IN `#car_reg` VARCHAR(10) ,
IN `#mobile_no` int)
BEGIN
SELECT carReg
FROM car
WHERE (carReg = car_reg);
END$$
DELIMITER ;
it brings up nothing except an empty car_reg even when the input data matches that in the car table.
Thanks
You need to be more consistent in your variable names.
`#car_reg` != car_reg
Something like this should work:
DELIMITER $$
CREATE PROCEDURE new_loan
(
IN v_car_reg VARCHAR(10) ,
IN v_mobile_no int)
BEGIN
SELECT carReg
FROM car
WHERE (carReg = v_car_reg);
END$$
DELIMITER ;

Use value of one procedure in another procedure

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.

how to call multiple columns in a stored procedure

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.