Using dynamic DB Schema name when defining procedure in esql - ibm-app-connect

I'm using AppConnect v12 and I'm trying to write a DB stored procedure from esql using the below piece of code:
CREATE PROCEDURE getCustomer_SP (
IN custNo INT,
INOUT responseCode CHAR)
RETURNS INT
LANGUAGE DATABASE
EXTERNAL NAME "DBSchemaDev.SPName";
where the DBSchema varies from environment to another, so I need it to be variable according to the environment and I will get its value from config. file
For example, for test environment, it is called DBSchemaTest and so on..
I tried the below
DECLARE DBSchema CHARACTER 'DBSchemaDev';
CREATE PROCEDURE getCustomer_SP (
IN custNo INT,
INOUT responseCode CHAR)
RETURNS INT
LANGUAGE DATABASE
EXTERNAL NAME DBSchema || ".SPName";
but it is not working,
I tried also below
DECLARE DBSchema CHARACTER 'DBSchemaDev';
CREATE PROCEDURE getCustomer_SP (
IN custNo INT,
INOUT responseCode CHAR)
RETURNS INT
LANGUAGE DATABASE
EXTERNAL NAME "{DBSchema}.SPName";
but it is not working as well,
so I was wondering if there is possibility to use variables in the EXTERNAL NAME field

Solution found. I used to call the stored procedure as below
DECLARE DBSchema CHARACTER 'esb'; --where esb is retrieved from config. file
CALL getCustomer_SP(custNo, responseCode) IN Database.{DBSchema};
where I removed the schema name from the stored procedure definition as below
CREATE PROCEDURE getCustomer_SP (
IN custNo INT,
INOUT responseCode CHAR)
RETURNS INT
LANGUAGE DATABASE
EXTERNAL NAME "SPName";

Related

User-Defined Table Type - Dynamic SQL Function Call - Not switching databases (thows error)

I need a SQL guru, this is driving me crazy...
I'm getting an error when executing dynamic SQL with a user-defined Table Type:
Error: Operand type clash: VariableTableType is incompatible with VariableTableType
Problem: It doesn’t seem to really declare the VariableTableType in the [Database1] database even though the Use [Database1] is right above it.
The XT function has to run in the [Database1] database for it to work.
If I run the exec with the dbname, it works- ex: exec database1.dbo.sp_executesql #cmdTest – but I have many client databases that have the same database structure (different data) where I have to pass in the client database name so I can’t hardcode it that way.
If I make #dbname=’MainDatabase’ it will run – just doesn’t run against the right database.
[MainDatabase]
Contains main stored proc that has the dynamic SQL:
#dbname=’Database1’
DECLARE #cmdTEST NVARCHAR(MAX)=N'USE '+#dbname +'
DECLARE #t as dbo.VariableTableType
INSERT #t
SELECT VariableName,[Value],InstanceID FROM #tVarTble
SELECT dbo.XT(''Test string'',7153,45,#t)RtnValue
'
exec sp_executesql #cmdTEST
Also tried passing in the VariableTableType and put the declaration outside of the dynamic sql.
--exec sp_executesql #cmdTEST,N'#t VariableTableType READONLY',t=#t
[Database1] (this function also exists in the [MainDatabase]
ALTER FUNCTION [dbo].[XT]
(
#text nvarchar(max),
#instanceID INT,
#storeID INT,
#vt dbo.VariableTableType READONLY
)
RETURNS nvarchar(max)
AS BEGIN
… code here
RETURN #return
END
VariableTableType exists in the [MainDatabase] and [Database1]
CREATE TYPE [dbo].[VariableTableType] AS TABLE(
[VariableName] [NVARCHAR](50) NULL,
[Value] [NVARCHAR](50) NULL,
[InstanceID] [INT] NULL
)

MySql syntax error on procedure parameter

I am trying to write a simple procedure but am encountering a syntax error at the first parameter. As best I can tell I'm following the syntax of CREATE PROCEDURE correctly.
I am limited to accessing my database with phpMyAdmin. Here is the create script I'm trying to run:
DROP PROCEDURE IF EXISTS product_index_swap/
CREATE PROCEDURE product_index_swap (#id INT, #oldIndex INT, #newIndex INT)
BEGIN
DECLARE #swapID;
SET #swapID = (SELECT `id` FROM `product` WHERE `order_index` = #newIndex LIMIT 1);
UPDATE `products` SET `order_index` = (CASE WHEN `id` = #id THEN #newIndex
WHEN `id` = #swapID THEN #oldIndex END)
WHERE `id` IN (#id, #swapID);
END
I am using the option on phpMyAdmin to change the delimiter to /.
I receive a syntax error "near '#id INT, #oldIndex INT....". I thought I may encounter more delimiter errors since I'm not entirely clear on the scope of them. I believe if that was the problem the error would be on a new line in the procedure when it failed to understand a semicolon, not at the parameters declaration.
You're using the Microsoft SQL Server convention of putting # before all the parameters and local variables. MySQL doesn't do this.
In MySQL syntax, procedure parameters have no sigil.
Also parameters are typically declared IN or OUT or INOUT.
CREATE PROCEDURE product_index_swap (IN id INT, IN oldIndex INT, IN newIndex INT)
BEGIN
DECLARE swapID;
...
MySQL variables that have the # sigil are session variables.
See also:
https://dev.mysql.com/doc/refman/5.7/en/create-procedure.html
https://dev.mysql.com/doc/refman/5.7/en/declare-local-variable.html
https://dev.mysql.com/doc/refman/5.7/en/set-variable.html
In MySQL, the #var variables are session level variables.
Use normal variables without the # and make sure you do not have conflict with column names:
CREATE PROCEDURE product_index_swap (in_id INT, in_oldIndex INT, in_newIndex INT)
BEGIN
DECLARE v_swapID int;
SELECT id into v_swapID
FROM product
WHERE order_index = in_newIndex
LIMIT 1;
UPDATE products
SET order_index = CASE WHEN id = in_id THEN in_newIndex
WHEN id = v_swapID THEN in_oldIndex
END
WHERE id IN (in_id, v_swapID);
END

mysql stored procedure encode out parameter string

I have this stored procedure:
CREATE PROCEDURE get_mysql_user_pass(
IN user_type VARCHAR(16),
IN aes_passphrase VARCHAR(255),
OUT mysql_user VARCHAR(16),
OUT mysql_pass VARCHAR(32),
)
READS SQL DATA
SQL SECURITY INVOKER
BEGIN
DECLARE aes_key VARCHAR(255);
SET #aes_key = SHA1(aes_passphrase);
SELECT
AES_DECRYPT(user_column,#aes_key),
AES_DECRYPT(pass_column,#aes_key)
INTO
mysql_user, mysql_pass
FROM my_table
WHERE my_column = user_type;
END;
The problem:
If I use alpha_numeric like "hosi654dl25lkjdfwhatever" as mysql_pass everything OK, but if I have something like this "rIµ°uþÁYfM09ÃÁôQÇOÅ°jýµàÈWfYf¡½4" as mysql_pass returns grabbled string like "rIµ°uþÃYfM09ÃÃôQÇOÅ°jýµà ÈWfYf¡½4".
I call it from PHP
The question:
Is there any method to encode out string parameter "mysql_pass" and make it readable in PHP? (because the decryption seem be correct)

Error :write function in mysql

I have written a mysql function to check duplicate values in table. But instead return result of function, it returns number of rows in table. I don't know why and how to solve this problem. Please help me
here is my mysql funcion:
CREATE DEFINER=`root`#`localhost` FUNCTION `check_duplicate`(tenMH varchar(45),
thongtin varchar(45),mausac varchar(45),
kichco varchar(45),giatran double,mancc int,madm int) RETURNS int(11)
BEGIN
declare result int default 0;
select count(*) into result from phpdb.mathang where (TenMH=tenMH) and (ThongTin=thongtin)
and(MauSac=mausac) and(KichCo=kichco) and(GiaTran=giatran)and(MaNCC=mancc) and(MaDM=madm);
RETURN result;
END
Your function returned exactly what it should do.
If you want to fetch those duplicate records you better go for a PROCEDURE but not a FUNCTION.
Functions are to return a single row, single column results.
Procedures can be defined to perform some operations and optionally return multi value and multi row outputs.
Convert your function to a procedure so that you can capture duplicate records as a resultset.
And as a precaution, input parameter names should have different naming convention than the table columns for restricting the name ambiguity.
Your procedure should be looking as follows:
drop procedure if exists get_duplicates;
delimiter //
create definer=`root`#`localhost`
procedure `get_duplicates`(
in _tenmh varchar(45),
in _thongtin varchar(45),
in _mausac varchar(45),
in _kichco varchar(45),
in _giatran double,
in _mancc int,
in _madm int )
begin
select * from phpdb.mathang
where tenmh = _tenmh
and thongtin = _thongtin
and mausac = _mausac
and kichco = _kichco
and giatran = _giatran
and mancc = _mancc
and madm = _madm;
end;
//
delimiter ;
Refer to Documentation:
CREATE PROCEDURE and CREATE FUNCTION Syntax

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.