I have created a database called stock trades and two tables (company data and stock_data) I want to create a procedure that will help me find stocks over 500K volume and in the technology sector. sector variable is in table company data and volume variable in stock_data) here is my code so far:
stockcode
create procedure highvolumetechstock
as
select Volume
from stock_data
INNER JOIN Companydata ON stock_data.Volume = Companydata.Sector
where Sector = 'Technology' and Volume > 500000
exec highvolumetechstock
Here is how to declare in procedure in mysql. Please note that, as commented, using a procedure is does not make a lot of sense, as you could obtain the same result with a simple SQL query.
delimiter //
CREATE PROCEDURE highvolumetechstock()
BEGIN
SELECT Volume
FROM stock_data
INNER JOIN Companydata ON stock_data.Volume = Companydata.Sector
WHERE Sector = 'Technology' AND Volume > 500000
END //
delimiter ;
Then you execute the procedure with :
CALL highvolumetechstock();
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');
I am having a problem loading data from MySQL into Matlab.
I am just running a stored procedure. When I run it from MySQL it returns 1,500 rows.
When I run it from Matlab, it will return 1 record when I first start up Matlab. If I re-run it a few times I might eventually get 1,500 rows. Earlier it took 30 minutes before it would return 1,500 rows.
My store procedure is:
-- --------------------------------------------------------------------------------
-- Routine DDL
-- Note: comments before and after the routine body will not be stored by the server
-- --------------------------------------------------------------------------------
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE `latest_hl_tradables`()
BEGIN
DECLARE latest_date DATE ;
SELECT max(available_on_platform.on_date)
FROM available_on_platform
INNER JOIN platform ON (available_on_platform.platform_id=platform.id)
where platform.name='aaa'
INTO latest_date;
SELECT fund_id,fund_class_id,fund_class.name as fund_class_name,sedol,isin,max(on_date) as on_date
FROM available_on_platform
INNER JOIN fund_class ON (fund_class.id=available_on_platform.fund_class_id)
INNER JOIN platform ON (available_on_platform.platform_id=platform.id)
WHERE on_date>=latest_date-5
AND platform.name='aaa'
GROUP BY fund_class_id,fund_class.name,sedol,isin;
END
My Matlab Code is:
conn=database('fund_data3','','');
sql = 'call latest_hl_tradables()';
curs = exec(conn,sql);
curs = fetch(curs);
Background - I have a DB created from a single large flat file. Instead of creating a single large table with 106 columns. I created a "columns" table which stores the column names and the id of the table that holds that data, plus 106 other tables to store the data for each column. Since not all the records have data in all columns, I thought this might be a more efficient way to load the data (maybe a bad idea).
The difficulty with this was rebuilding a single record from this structure. To facilitate this I created the following procedure:
DROP PROCEDURE IF EXISTS `col_val`;
delimiter $$
CREATE PROCEDURE `col_val`(IN id INT)
BEGIN
DROP TEMPORARY TABLE IF EXISTS tmp_record;
CREATE TEMPORARY TABLE tmp_record (id INT(11), val varchar(100)) ENGINE=MEMORY;
SET #ctr = 1;
SET #valsql = '';
WHILE (#ctr < 107) DO
SET #valsql = CONCAT('INSERT INTO tmp_record SELECT ',#ctr,', value FROM col',#ctr,' WHERE recordID = ',#id,';');
PREPARE s1 FROM #valsql;
EXECUTE s1;
DEALLOCATE PREPARE s1;
SET #ctr = #ctr+1;
END WHILE;
END$$
DELIMITER ;
Then I use the following SQL where the stored procedure parameter is the id of the record I want.
CALL col_val(10);
SELECT c.`name`, t.`val`
FROM `columns` c INNER JOIN tmp_record t ON c.ID = t.id
Problem - The first time I run this it works great. However, each subsequent run returns the exact same record even though the parameter is changed. How does this persist even when the stored procedure should be dropping and re-creating the temp table?
I might be re-thinking the whole design and going back to a single table, but the problem illustrates something I would like to understand.
Unsure if it matters but I'm running MySQL 5.6 (64 bit) on Windows 7 and executing the SQL via MySQL Workbench v5.2.47 CE.
Thanks,
In MySQL stored procedures, don't put an # symbol in front of local variables (input parameters or locally declared variables). The #id you used refers to a user variable, which is kind of like a global variable for the session you're invoking the procedure from.
In other words, #id is a different variable from id.
That's the explanation of the immediate problem you're having. However, I would not design the tables as you have done.
Since not all the records have data in all columns, I thought this might be a more efficient way to load the data
I recommend using a conventional single table, and use NULL to signify missing data.
So far I have tried many different ways of accessing the data on three tables using a stored procedure. First I tried a simple select statement :
create procedure reportCodes () begin
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
end$$
(names changed to protect the guilty)
Running the stored procedure in phpmyadmin results in the first instance of the record (1 out of two ‘true’ in the test database). Running just:
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id;
in the phpmyadmin SQL tab returns both records. Then I tried a temp table:
create procedure reportCodes () begin
CREATE TEMPORARY TABLE used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers, Owner, location
WHERE Numbers.used = true AND Numbers.id=numOwner.Numbers_id AND
Numbers.id=location.Numbers_id);
SELECT * FROM used_numbers; end$$
Returns 1 of 2 records as the procedure but both records in console. Finally I tried changing my table to a join:
CREATE PROCEDURE reportCodes()
begin
create temporary table used_numbers AS (
SELECT Numbers.serial_numb, numOwner.lName, numOwner.fName, numOwner.email,
location.long, location.lat, Numbers.dateValidated
FROM Numbers JOIN numOwner
ON Numbers.id=numOwner.Numbers_id
JOIN location ON
numOwner.Numbers_id=location.Numbers_id
WHERE Numbers.used = true
);
SELECT * FROM used_numbers; end$$
Same results as above. I’m at a loss as to why running just the SQL would show both test records but running the procedure with the exact same code only yields one.
Thanks
in your query, numOwners isn't a valid table being selected against, so something's wrong. Have you tried running your SQL in the Query window in phpMyAdmin to ensure that the EXACT same query is returning 2 rows?
I presume the "Owner" table is supposed to be "numOwner", so I've re-written the stored procedure call below. Also, I'm not sure what types of values you're storing in Numbers.used to evaluate to "TRUE". I will presume you're using a TINYINT(1), so I've altered that, as well. I hope this helps.
DELIMITER $$
USE `db`$$
DROP PROCEDURE IF EXISTS `reportCodes`$$
CREATE PROCEDURE `reportCodes`()
BEGIN
SELECT
n.serial_numb,
o.lName,
o.fName,
o.email,
l.long,
l.lat,
n.dateValidated
FROM Numbers n
INNER JOIN numOwner o ON n.id=o.Numbers_id
INNER JOIN location l ON n.id=l.Numbers_id;
WHERE n.used = 1
END$$
DELIMITER ;
I am new to Mysql. I want to create a stored procedure that update the price for a product. I use jdbc to implement this stored procedure.But my stored procedure command failed many times. The following is mysql code. "p" means product, "des" means the decrease in price and "pro" means product name. The CallableStatement in java will give the product name and the decrease for that the product's price. Thus the stored procedure will decrease the price for the corresponding product.
`CREATE PROCEDURE updateP(IN pro CHAR(10), IN des INT)`
`
BEGIN`
UPDATE customers
`DECLARE p INT;`
`SET p = (SELECT price FROM customers WHERE product= pro);`
`SELECT p;`
`p = p - des;`
END
I think this is all you need:
DELIMITER $
CREATE PROCEDURE updateP(IN pro CHAR(10), IN des INT)
BEGIN
UPDATE customers
SET price = price-des
WHERE product= pro;
END$
DELIMITER ;
That should decrease the current price by the decrease amount passed in as parameter.
One suggestion: don't name your procedures updateP use full names like sprocUpdateProduct or UpdateProduct_sp; if tomorrow you need to update a person you no longer will know whether updateP updates a Person or a Product. The same goes for variables, use full names.