I've create a stored procedure in MySQL like the this
CREATE DEFINER=`root`#`localhost` PROCEDURE `my_proc`(IN var1 VARCHAR(25))
BEGIN
select (sum(er)*9)/(out/3) as era from table1 where id = var1 group by id;
END
I have another table that I'd like to get this information from. I'd like to do something like this (pseudo code)
select id, column1, column2, (call my_proc(table1.id)) as era from table1
Basiclly I'm having my stored procedure calculate some information and return it as a column into that query.
Is a stored procedure the right solution here?
**Note in the pseudo-query the table name is supposed to be the same as in the stored procedure.
You can define a stored function instead of a stored procedure.
CREATE DEFINER=`root`#`localhost` FUNCTION `my_func`(IN var1 VARCHAR(25))
RETURNS NUMERIC(9,2)
BEGIN
RETURN (SELECT (SUM(er)*9)/(out/3) AS era FROM table1 WHERE id = var1);
END
Then you can call it simply as you would call a function:
SELECT id, column1, column2, my_func(table1.id) AS era FROM table1
The stored function must be guaranteed to return a single scalar to be usable in your select-list.
I removed the GROUP BY, since it's superfluous.
The example above is kind of suspicious, because there's no reason to call a function like this to calculate the SUM over a single row. But I guess you have something more complex in mind.
Related
I am trying to insert values from one table to another one using the following insert sql query
INSERT INTO [dbo].[table2]
SELECT Exec [StoredProcedure],
[Column1]
,[Column2]
FROM [dbo].[table1]
[table2] has the following columns:
RecNo, <-- INT
Column1, <--VARCHAR(50)
Column2 <--VARCHAR(50)
[StoredProcedure] main purpose is that every time a new row to be inserted in table2 it selects the maximum value from RecNo from table2 and adds 1 to that number to create the next number (sequential).
Here is the script for the stored procedure .
GO
ALTER PROCEDURE [dbo].[UpdateRcnoNumbers]
#MaxRcno INT OUTPUT
AS
BEGIN
SELECT #MaxRcno=MAX(Recno) FROM [table2]
SELECT #MaxRcno=#MaxRcno+1
RETURN #MaxRcno
END
But I am getting an error and I am not able to call the stored procedure ? Any suggestion please .
Thank you in advance
You should write a function for this purpose , read here
The problem is, Stored Procedures don't really return output directly. They can execute select statements inside the script, but have no return value.
MySQL calls stored procedures via CALL StoredProcedureName(); And you cannot direct that output to anything, as they don't return anything (unlike a function).
Here
MySQL Call Command
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
Is it possible to get stored procedure multiple result set to temp table variable?
I wrote a stored procedure like,
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name from Table1 where column1=#param1;
Select Id, Age, Address from Table2 where column1=#param2;
End
When i execute this sp it will return 2 tables(record set's).
Now, i want to get this 2 record sets to 2 temp table variables.
How can i achieve this?
I don't believe a stored procedure can return multiple result sets like you want it to. What I might suggest is to have the stored proc store the results in two global temp tables and then have the calling process (whatever is calling your stored proc) query the two global temp tables and put the results into your temp table variables.
I know this is an old question, and maybe I am misunderstanding what you want, but if you just want to take those two recordsets and put them into temp tables, couldn't you do this:
Create proc test1
(
#param1 int,
#param2 int
)
as
Begin
Select Id, Name
into #temp1
from Table1 where column1=#param1;
Select Id, Age, Address
into #temp2
from Table2 where column1=#param2;
End
now, I dont know what you're trying to achieve with this, but depending on how you're calling that sp, the #temp tables may not be accessible from outside of the sp.
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.