I need to call a stored procedure that have 2 IN Param into a stored procedure and the 2 IN Param are returned from a SELECT
Ex
CREATE PROCEDURE 'xxxx'
BEGIN
....
CALL MyProcedure ((SELECT InParam1,InParam2 FROM Table WHERE Id=1),#Out1,#Out2);
....
END
You cannot pass a query to a stored procedure. Fetch the values into the local variables and use those in procedure call. Also, use local variables instead of user-defined variables as out-parameters.
The reason for using local variables is that the user-defined variables can inadvertently change when you call another procedure.
CREATE PROCEDURE 'xxxx'
BEGIN
declare v_in1 int;
declare v_in2 int;
declare v_out1 int;
declare v_out2 int;
select InParam1,InParam2 into v_in1, v_in2
FROM Table
WHERE Id=1;
CALL MyProcedure (v_in1, v_in2, v_out1, v_out2);
....
END
Related
I'm trying to call a stored procedure from another stored procedure and store the value in a variable. The inner stored procedure basically checks if something exists and uses a select statement to return a zero or one. I keep getting an error. In this situation, MySQL is saying "=" is not valid at this position, expecting ";"
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
DECLARE exist TINYINT;
EXECUTE exist = CardNames_CheckExist searchedCard
IF (exist = 0)
INSERT INTO card_names (name)
VALUE(searchedCard)
END
You have to rewrite you other stored procedure, that you don't need btw, to give back a result
CREATE PROCEDURE CardNames_CheckExist (IN searchedCard VARCHAR(50), OUT result TINYINT )
BEGIN
--do some stuzff
result = 1
END
CREATE PROCEDURE `CardNames_Add` (searchedCard VARCHAR(50))
BEGIN
CALL CardNames_CheckExist(searchedCard,#result);
IF (#result = 0) THEN
INSERT INTO card_names (name)
VALUES (searchedCard);
END IF;
END
I'm trying to use input parameters of a stored procedure within a cursor of the procedure.
Calling the procedure as follows results in an Error
-- -role- -table- -cond-
CALL grantRoleToUsersFromWhere('Student', 'studenten', true);
Error Code: 1146. Table 'uni4.utable' doesn't exist
This tells me that the parameter 'userTable' was not written to the variable 'uTable' OR 'uTable' is not recognized as a variable at all by the cursor Statement.
I tried different approaches storing / using the parameters. e.g. use them directly or store them in a Variable with a SET statement. However, if I try to use SET uTable=userTable; before the cursor declaration, MySQL WorkBench won't accept the Procedure declaration.
I spent quite some time on this but I think I'm missing an important yet simple part :-)
DROP PROCEDURE IF EXISTS grantRoleToUsersFromWhere;
DELIMITER $$
CREATE DEFINER=`root`#`localhost` PROCEDURE grantRoleToUsersFromWhere(IN grantRole VARCHAR(30), IN userTable VARCHAR(30), IN addCondition VARCHAR(50))
BEGIN
DECLARE workUser VARCHAR(30) default '';
DECLARE gRole VARCHAR(30) default grantRole;
DECLARE uTable VARCHAR(30) default userTable;
DECLARE aCond VARCHAR(50) default addCondition;
DECLARE cur1 CURSOR FOR SELECT Name FROM uTable WHERE aCond;
OPEN cur1;
read_loop: LOOP
FETCH cur1 INTO workUser;
GRANT gRole TO workUser;
END LOOP;
CLOSE cur1;
END $$
DELIMITER ;
Create dynamic cursors directly is not possible. You can however use VIEW's to achieve the same thing. See sample.
CREATE PROCEDURE p1 (select_statement VARCHAR(255))
BEGIN
DECLARE v1,v2 VARCHAR(255);
DECLARE c CURSOR FOR SELECT * FROM t;
SET #v = CONCAT('create temporary table t as ',select_statement);
PREPARE stmt1 FROM #v;
EXECUTE stmt1;
OPEN c;
FETCH c INTO v1,v2;
SELECT v1,v2;
END//
' DECLARE cur1 CURSOR FOR SELECT Name FROM uTable WHERE aCond;' is not possible mysql does not do variable substitution (for table name). Your read loop is infinite because you don't declare a continuation handler and test for not found in the read loop.
I have created a procedure in which I am passing a xml type data. I am trying to read that data but it is always giving null.
delimiter //
create procedure SP_LogIn(xml text)
begin
declare AgentId varchar(30);
declare pass varchar(30);
set #AgentId=ExtractValue(#xml,'/operation/userName');
set #Pass=ExtractValue(#xml,'/operation/paasword');
select * from am_agentmasteraccount where am_AgentId=#AgentId and am_AgentPassword=#Pass;
end //
delimiter;
here I am calling the procedure
call SP_Login('<operation><userName>RAJ0560111</userName><password>rajpratha</password></operation>');
try this it would work.
delimiter //
create procedure SP_LogIn(xml text)
begin
declare AgentId varchar(30);
declare pass varchar(30);
set AgentId:=(ExtractValue(xml,'/operation/userName'));
set Pass:=(ExtractValue(xml,'/operation/password'));
select * from am_agentmasteraccount where am_AgentId=AgentId and am_AgentPassword=Pass;
select AgentId,pass;
end //
delimiter;
I have a stored procedure which returns a value (ex: GetData)
How can I declare a variable in another function to get this value?
Something like this
Create Function Test
Return Int
Begin
Declare #data My_Data_Type
Declare #count int
SET #data = exec GetData
Select #count = count(*)
From #data
Return #count
End
Please help me !!!!
If your stored procedure returns table then do
declare #data as table(col1 int, col2 varchar(50))
insert into #data exec GetData
Select #count = count(*) from #data
If you want to return scalar valus e.g. int, varchar, use OUTPUT parameter
CREATE PROC GetData
(
#retVal int OUTPUT
)
AS
BEGIN
SET #retVal = 123
Return 0
END
Then retrieve it this way
declare #data int
EXEC GetData #data OUTPUT
Have a look at Using a Stored Procedure with Output Parameters
A SQL Server stored procedure that you can call is one that returns
one or more OUT parameters, which are parameters that the stored
procedure uses to return data back to the calling application.
Also Returning Data by Using OUTPUT Parameters
If you specify the OUTPUT keyword for a parameter in the procedure
definition, the stored procedure can return the current value of the
parameter to the calling program when the stored procedure exits. To
save the value of the parameter in a variable that can be used in the
calling program, the calling program must use the OUTPUT keyword when
executing the stored procedure.
Is it possible to call a procedure from a function in MySQL? I get the error "not allowed to return a result set from a function." I want the results of the procedure call to be inserted into the function variables the same as if I had done a SELECT INTO directly in the function.
The function is (simplified) defined as
create function my_function()
returns int deterministic
begin
declare some_parameter int;
declare the_result int;
call my_procedure(some_parameter, the_result)
return the_result;
end;
The procedure is (simplified) defined as:
create procedure my_procedure(in my_parameter int, out my_result int)
begin
select 1
from dual;
end;
In essence, no. Functions are looking for a datatype, not a record (which is what is returned from the procedure).