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
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 wanted to retrieve a value from the select statement and use it in the insert statement. But, both these operations to be done in a single stored procedure. I'm trying something like below.
CREATE PROCEDURE Illness_sp (IN DoctorNameIn varchar(255), IN IllnessNameIn varchar(255), IN IllnessSevIn int(10))
BEGIN
select doctorId from doctor where DoctorName=DoctorNameIn;
INSERT INTO Illness(IllnessName, IllnessSeverity, PreferredDoctorId) Values(IllnessNameIn, IllnessSevIn, doctorId);
END
I have two statements inside this stored procedure:
retrieving doctorId from the SELECT statement; and
wanted to INSERT it into another table in the second statement.
INSERT INTO `Illness`(IllnessName, IllnessSeverity, PreferredDoctorId)
SELECT IllnessName, IllnessSeverity, PreferredDoctorId
FROM `SomeTable`
WHERE ...
I'm trying to pass a string value to a stored procedure from my code but it is not executing the query correctly. Following is the code of stored procedure.
I want to pass multiple values to the stored procedure like '2,3,4'. But when I do it only takes 2 and throws an error.
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE ID IN (RECORD_ID);
END
You have to use FIND_IN_SET() function instead of IN operator
Try this:
CREATE PROCEDURE `USP_INSERT_PROCESSED_ETC_RECORDS_TO_MAIN_TABLE`
(
IN RECORD_ID VARCHAR(20)
)
BEGIN
SELECT * FROM TX_TOLL_TRANSACTION_RECORD_DETAIL_STAGING
WHERE FIND_IN_SET(ID, RECORD_ID);
END
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.
Let's say we have a stored procedure selecting something from a table:
CREATE PROCEDURE database.getExamples()
SELECT * FROM examples;
How can I use the result from this procedure in a later select?
(I've tried
SELECT * FROM (CALL database.getExamples())
but with no success.)
Should I use SELECT... INTO outVariable in the procedure? Or should I use a function returning the table instead?
Reformulated the question in this thread: Can a stored procedure/function return a table?.
Obviously, it isn't possible without the use for temp tables.
CREATE TABLE #TempTable
(OID int IDENTITY (1,1),
VAr1 varchar(128) NOT NULL,
VAr2 varchar(128) NOT NULL)
Populate temporary table
INSERT INTO #TempTable(VAr1 , VAr2 )
SELECT * FROM examples
In SQL server you can then do SELECT * FROM database.getExamples()
If you want to re-use the 'procedure' then, yes, I would put it into a table valued function.
Otherwise you could just SELECT INTO a #temporary table inside the stored procedure.