A similar question about sql-server has been asked here. I'm wondering if its possible in MySql.
edit:
I want to use result set returned from procedure in view.
If you want to get result-set and use routine in FROM clause - NO. Stored routines (procedures or functions) in MySQL cannot return tables as result value.
But you can use functions as simple values, for example -
DELIMITER $$
CREATE FUNCTION mul10(Param1 INT)
RETURNS INT(11)
BEGIN
RETURN Param1 * 10;
END
$$
DELIMITER ;
CREATE OR REPLACE VIEW view1
AS
SELECT mul10(2) AS column1;
SELECT column1 FROM view1;
----------
20
Related
I am struggling in creating a stored function, as I am a complete beginner in databases.
My aim is to turn this query into a stored function.
SELECT AVG(TIME_TO_SEC(TIMEDIFF(ShippedDate,OrderDate))) /86400 as OrdersAverage
FROM orders;
The aim of this query is returning the average of difference between the dates of all the orders that were placed when compared to the day they were shipped.
When i tried, i got an error.
This was my definitely wrong stored function:
DELIMITER //
CREATE FUNCTION OrderFulfilmentCycleTime()
RETURNS VARCHAR(10)
BEGIN
SELECT AVG(TIME_TO_SEC(TIMEDIFF(ShippedDate,OrderDate))) / 86400
FROM orders;
END//
DELIMITER ;
Thanks.
A MySQL function can't return a resultset i.e. the return from a SELECT statement.
In a MySQL function, use a RETURN statement to return a scalar value.
As an example:
DELIMITER $$
CREATE FUNCTION OrderFulfilmentCycleTime()
RETURNS DECIMAL(22,9)
READS SQL DATA
BEGIN
DECLARE ln_days DECIMAL(22,9);
SELECT AVG(TIMESTAMPDIFF(SECOND,o.orderdate,o.shippeddate))/86400
FROM orders o
INTO ln_days;
RETURN ln_days;
END$$
DELIMITER ;
An example of calling the function:
SELECT OrderFulfilmentCycleTime();
If we need a MySQL stored program to return a resultset, we can define a PROCEDURE (rather than a FUNCTION).
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 wanted to know if it was possible to do something like:
CREATE ALIAS SOLR_DATE_FORMAT FOR DATE_FORMAT(date_val,'%Y-%m-%dT%TZ')
DATE_FORMAT(date_val,'%Y-%m-%dT%TZ') exists in MySQL, but I'd like to give it another name and make it a one argument function, because in my unit tests I use another DB (H2) on which I defined such a SOLR_DATE_FORMAT function
You can create your own function:
DELIMITER $$
create function SOLR_DATE_FORMAT( date_val )
returns char(20)
begin
return DATE_FORMAT(date_val,'%Y-%m-%dT%TZ');
end$$
DELIMITER ;
EDITED Fixed returned type. Thanks eggyal!
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.