how to pass integer array as parameter in mysql stored proceedure - mysql

i have to pass two array parameter and two integer parameter in mysql stored procedure and iam totaly unaware of the idea that how to do it. So i need your help.
my java function and proceedure look like this:
int[] amounts={1000,2000,300,10}
int[] fee={1,2,3,4}
java.sql.CallableStatement cs = conn.prepareCall("{ call P_submitAdmissionFee(?,?,?)}");
cs.setInt(1, amounts);
cs.setInt(2, fee);
cs.setInt(3, Integer.parseInt(did));
cs.execute();
mysql proceedure
CREATE DEFINER=`kgt`#`%` PROCEDURE `P_submitAdmissionFee`(
IN amount int(15),
IN fee int,
IN userid int,
Out msg int
)
BEGIN
select Session_ID,Registration_ID into #result,#rid from std_entry_master where Student_ID=sid;
select Fee_Cycle into #fc from school_profile_master;
/*Update sims_accounts.student_feecycledetail set Status='Y',Modified_Date=now(),Modified_By=userid
where Reg_ID=#rid AND Expected_Month_Year <= now();
Update registration_student set Admitted='Y' where Registration_ID=#rid;*/
Insert into student_fee_master(Student_ID,Total_Amount,Fee_For,Created_By,
Created_Date) values(#rid,#total,'A',userid,now());
only update command must be execute according to there size.

Related

How to write a stored procedure to insert values in multiple tables

How do I write a stored procedure to add a person record with multiple addresses?
It is easy if the person has only one address but I'm not sure how to write a stored procedure to add a person with multiple addresses.
Here is the stored procedure to add a person with one address:
DELIMITER $$
CREATE PROCEDURE `log`.`spAddPerson` (
IN personID INT,
IN personName VARCHAR(100),
IN addressLine1 VARCHAR(45),
IN addressLine2 VARCHAR(45),
IN myCity VARCHAR(45),
IN myState VARCHAR(45),
IN myCountry VARCHAR(45)
)
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
BEGIN
ROLLBACK;
END;
START TRANSACTION;
INSERT INTO person VALUES(personID,personName);
-- addressid is automatically generated
INSERT INTO address(Line1, Line2,City,State,Country) VALUES
(addressLine1, addressLine2, myCity,myState, myCountry);
INSERT INTO personAddress(personID, last_insert_id());
COMMIT;
END
The above code works fine. However, I do not know how to handle a person with multiple addresses without writing a separate stored procedure. Is there a simple way to do this?
You cannot pass a variable number of variables to a procedure, nor a non-scalar type.
A possible trick would be building a temporary table with the addresses before calling this procedure. Either the temporary table is pre-determined, or pass its name as a VARCHAR parameter(and use it to build dynamic SQL statements). Eg:
CREATE PROCEDURE spAddPerson (tmp_table VARCHAR(10), ...)
BEGIN
...
PREPARE s AS CONCAT(
'INSERT INTO address (line1, ...) VALUES SELECT * FROM ', tmp_table
);
EXECUTE s;
...
END
-- use it like this
CREATE TEMPORARY TABLE tmp_addresses (line1 VARCHAR(255), ...);
INSERT INTO tmp_addresses VALUES ('1 Cherry Lane'), ... ;
CALL spAddPerson ('tmp_addresses', ...);
However, I would rather split the action in two parts. Do you really want to prevent the creation of the person altogether if its address creation fails? And even then, wouldn't you want to advise your user why the transaction failed (user creation or address creation)?
I would rather treat these two exceptions separately at the application level:
issue a "START TRANSATION"
try to insert a person (call stored proc 1)
if it failed, rollback and notify user
for each address
try to insert an address (call stored proc 2)
if it failed, rollback and notify user
issue a "COMMIT"
> DECLARE #LAST_INSERT_ID INT
> DECLARE #EXECUTION_OK char(1)
> SET #EXECUTION_OK = 1
>
> insert into base_table(imgPath,store,apparelType) values (imgPath,store,apparelType)
>
> SELECT #LAST_INSERT_ID = SCOPE_IDENTITY()
>
> insert into data_table(cvID,color) values (#LAST_INSERT_ID, color)
> GO
>
> If exists( Select cvID from data_table where cvID= #LAST_INSERT_ID)
> Begin
> #EXECUTION_OK = 0
> End

MySQL - use result of nested stored procedure

Is there any way to use the return value of a stored procedure in another stored procedure?
Just a very basic theoretical example:
CREATE PROCEDURE `user_read_name_and_email` (
IN `param_user_id` INT
)
BEGIN
DECLARE `current_user` SET;
SET `current_user` = CALL `user_read`(`param_user_id`);
SELECT `user_name`, `user_email` FROM `current_user`;
END
CREATE PROCEDURE `user_read` (
IN `param_user_id` INT
)
BEGIN
SELECT * FROM `user` WHERE `user_id` = `param_user_id`;
END
I think the one and only workaround is to use temporary tables.
Your example would be possible in MS SQL Server 2008 - there you can assing output of stored procedure to some variable.

Passing input values randomly while executing stored procedure

I have stored procedure, using this Sp am inserting data into employee table and am passing values as input parameters like ,
create procedure sp_inputvalues
#ename varchar(50),
#enum int,
#address varchar(100),
#sal int
as
begin
insert into employee(ename,enum,address,salary)values(#ename,#enum,#address,#sal)
end
To execute the Sp ,
exec sp_inputvalues 'venkat',1252,'nrl',20000
but now my requirement is i need to pass the input values in random order like,
exec sp_inputvalues 1252,'venkat','nrl',20000
if I pass as above format the stored procedure should work and the stored procedure should be remain same.
You need to specify the names of the parameters
exec sp_inputvalues
#enum = 1252,
#ename = 'venkat',
#address = 'nrl',
#sal = 20000;

Call MS SQL Server Stored Procedure with multiple parameters inclusive of table-value parameter

After doing some research here and online I am at a loss as to whether this is possible. What I want to do is call a stored procedure that has several parameters one of which is a table-value parameter.
This is my stored procedure snippet:
ALTER PROCEDURE [dbo].[procName]
#Action nvarchar(10) = 'view'
,#var1 int = 0
,#var2 int = 0
,#var3 myType ReadOnly
I now have another procedure (proc2) that has the following lines:
insert into #varX
select top 5
field1, field2
from
sourceTable
print 'Processing from table values...'
exec dbo.procName 'refresh', -1, 0, #varX
Note that varX and var3 are of the same type MyType
When I execute proc2 I get the error that I am specifying too many arguments for dbo.procName
I am at the point in thinking it is not possible to specify multiple parameters inclusive of a table-value parameter to a stored procedure. I am now tending towards the thought of changing my procName definition to only have one parameter (as all of the examples online seem to have) and have my table-value paramter act as an array of parameter values inclusive of the information I had in my previous select statement (in proc2). If however it is possible to do this call, please illustrate how this is done.
Thanks
This compiles and runs for me:
create type TT as table (ID int not null);
go
create procedure P1
#Val1 int,
#Val2 TT readonly,
#Val3 int
as
select #Val1 as Val1,ID,#Val3
from #Val2;
go
create procedure P2
as
declare #T TT;
insert into #T(ID) values (1),(2)
exec P1 10,#T,13
go
exec P2
Result:
Val1 ID
----------- ----------- -----------
10 1 13
10 2 13
So, I don't know what your issue is, but it's not being able to mix table and non-table parameters.

Calling a stored procedure within an IF statement MySQL

Does anybody know if this is allowed?
IF CALL GET_RIGHT_NODE(edge) = 15
THEN
SELECT "IT WORKS";
I'm getting an error on this syntax, is it possible any other way?
The return values from stored procedures should be captured in OUT paramters (whereas those from user defined functions can be captured as #returnValue = function()).
So, your GET_RIGHT_NODE should take an OUT parameter and set it to the return value.
CREATE PROCEDURE GET_RIGHT_NODE
(
#edge INT,
#returnValue INT OUTPUT
)
AS
-- Definition of the proc.
then you would call the procedure as follows:
DECLARE #returnValue INT
CALL GET_RIGHT_NODE(#edge, #returnValue)
IF (#returnValue = 15)
THEN
SELECT 'IT WORKS'