Passing input values randomly while executing stored procedure - sql-server-2008

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;

Related

MySQL Stored procedure returning all results ignoring WHERE claus

I have two stored procs which i call from my laravel application.
My laravel application passes in a cID parameter which is then passed to the stored procedure as the "where clause". But it seems something is going astray and possibly my variables arent set up properly.
Also i know that laravel IS passing the correct cID to my stored proc because i enabled the logs for mysql to see if it was passing any params.
Also the stored procedure select statement runs fine as a query if i manually set the ClientID = '';
My stored proc sends ALL clients and cards to the view, totally ignoring the where clause.
Laravel code:
Route::get('/clients/{cID}', function ($cID) {
$details = DB::select('CALL sp_Details(' . DB::raw($cID) . ')');
$cards = DB::select('CALL sp_Cards(' . DB::raw($cID) . ')');
return view('client.show', compact('details','cards'));
});
Any my Stored Proc
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_Details`(IN cID int )
BEGIN
SET #ClientID = cID;
SELECT
ClientID,
Client_Name
FROM accounts
where #ClientID = cID;
END
Stored Proc #2
CREATE DEFINER=`root`#`localhost` PROCEDURE `sp_Cards`(cID int)
BEGIN
SET #ClientID = cID;
SELECT
ClientID,
Code
FROM cards
where cID = #ClientID;
END
You are using local variables like #ClientID and you are confusing it to column names, must change your code to avoid them and there is no necessary use the local variable:
CREATE PROCEDURE `sp_Details`(IN cID int )
BEGIN
SELECT
ClientID,
Client_Name
FROM accounts
where ClientID = cID;
END
The other proc:
CREATE PROCEDURE `sp_Cards`(cID int)
BEGIN
SELECT
ClientID,
Code
FROM cards
where ClientID = cID;
END

how to pass integer array as parameter in mysql stored proceedure

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.

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

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'

stored procedure returning only reference fields

have a stored procedure with 1 input and 2 reference parameters. When assigning the stored procedure to a linq-query the query is not executed. What should I do to really execute the query which fills the 2 reference parameters. All examples I found at the internet use 'foreach' statements but the sp isn't returning a list of results.
the stored procedure:
ALTER PROCEDURE [dbo].[usp_GetStraatenPlaatsnaam]
-- Add the parameters for the stored procedure here
-- <#Param1, sysname, #p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
-- <#Param2, sysname, #p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
#POSTCODE nvarchar(6)
,#STRAAT nvarchar(17) out
,#PLAATS nvarchar(18) out
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
#STRAAT = PCT_STRAATNAAM_TNT
,#PLAATS = PCT_WOONPLAATS_TNT
FROM
PCTR
WHERE
PCT_POSTCODE = #POSTCODE
END
The Procedure is a given fact with no allowance to change. How can I call and execute this procedure using LinqToSQL? The Procedure is already used for a number of other processes and has worked for ages.
You are using output parameter as 'out' keyword which merely used in PL-Sql(Oracle)....Instead, you have to use 'OUTPUT' keyword for output parameter in T-Sql(Sql Server)....
ALTER PROCEDURE [dbo].[usp_GetStraatenPlaatsnaam]
-- Add the parameters for the stored procedure here
-- <#Param1, sysname, #p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>,
-- <#Param2, sysname, #p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
#POSTCODE nvarchar(6)
,#STRAAT nvarchar(17) OUTPUT
,#PLAATS nvarchar(18) OUTPUT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
SELECT
#STRAAT = PCT_STRAATNAAM_TNT
,#PLAATS = PCT_WOONPLAATS_TNT
FROM
PCTR
WHERE
PCT_POSTCODE = #POSTCODE
END