hsqldb procedure with date Input parameter - mysql

I need to write a test for some download operation. This operation call procedure from MSSQL database, take result set and java make some stuf. For test I use hsqldb.
My procedure:
CREATE PROCEDURE map.Get1(IN packageName varchar(100),
IN downloadDate DATE)
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
This procedure wan't work, i have an exception
call map.GET1('Genre','2018-03-10');
[42561][-5561] incompatible data type in conversion
java.lang.RuntimeException: org.hsqldb.HsqlException: incompatible data type
in conversion
But this(without date parameter) work well:
CREATE PROCEDURE map.Get1(IN packageName varchar(100))
READS SQL DATA DYNAMIC RESULT SETS 1 BEGIN ATOMIC
DECLARE result CURSOR WITH RETURN FOR SELECT * FROM map.tvschedule FOR READ ONLY;
OPEN result;
END
call map.GET1('Genre');
first needed row
second needed row
I am not going to use input parameter, but i need this procedure to be looking i am going to.
My question is How to use date input parameter with hsqldb procedures?
UPDATE1:
I used TO_DATE and now it works well, but i have no data in my result set, my java code is:
try (CallableStatement callableStatement = connection.prepareCall("{ call
map.GetGenreProtocol( ?, ? ) }")) {
callableStatement.setString(1, packageName);
callableStatement.setDate(2, date);
callableStatement.execute();
ResultSet resultSet = callableStatement.getResultSet();
while (resultSet.next()) {
Interval Interval = new Interval();
Interval.setDuration(resultSet.getInt("duration"));
Interval.setMappingTargetId(resultSet.getInt("mappingTargetId"));
Interval.setGenreId(resultSet.getInt("genreId"));
Interval.setStart(resultSet.getLong("start"));
Interval.setCategoryId(resultSet.getInt("categoryId"));
Interval.setCategoryName(resultSet.getString("categoryName"));
Interval.setGenreName(resultSet.getString("genreName"));
Interval.setDescription(resultSet.getString("description"));
Intervals.add(Interval);
}
}

Use the TO_DATE function.
For example:
call map.GET1('Genre', TO_DATE('2018-03-10', 'YYYY-MM-DD'));
I guess you need to create a function that returns a table instead of a procedure:
CREATE FUNCTION map.Get1(IN packageName VARCHAR(100),
IN downloadDate DATE)
RETURNS TABLE(.....)
READS SQL DATA
BEGIN ATOMIC
....
END;

Related

How to return a String value from a Stored Procedure in MySQL?

I want to return a value from Stored Procedure which I can use in my Spring Boot server in JPA repository.
Here's my Stored Procedure,I want to know how do I return String from the Procedure.
DELIMITER //
CREATE PROCEDURE GetAllProducts(out query varchar(20))
BEGIN
SET query=SELECT title FROM course where description="just a course";
RETURN #query;
END //
DELIMITER ;
I am selecting 1 value from the query table to return it.
Can someone Help?
Thanks!
If you want to use your SP as datasource (like you execute SELECT, not CALL, in external program) you must select to output stream, not to variable:
CREATE PROCEDURE GetAllProducts()
SELECT title
FROM course
WHERE description="just a course";
Got the Solution,Its working now!
Just tweaked the Procedure a little bit:
DELIMITER //
CREATE PROCEDURE GetAllProdcuts(OUT output VARCHAR(50))
BEGIN
SELECT title into output
FROM course
WHERE description="just a course";
END //

r2dbc:Calling Stored Proc in mysql with in and out parameters

Does r2dbc currently supports calling Stored proc in my sql with in/out parameters ?
I am trying to call Stored Proc using databaseClient which returns a String.But I am getting the below exception.Do I have to add anything to call SP from r2dbc.My stored procedure looks like this.How to call SP using database-client
CREATE PROCEDURE usp_get_data(in someId varchar(255),
in someName varchar(255),
out email varchar(255))
BEGIN
SELECT email FROM products where id=:someId and name=:someName LIMIT 1;
END //
Below is my code.
String STORED_PROC = "call usp_get_data(:someId,:someName)"
def result = databaseClient.execute(STORED_PROC)
.bindNull("someId","someId")
.bindNull("someName", "someName")
.as(String.class)
.fetch().one()
Incorrect number of arguments for PROCEDURE usp_get_data expected 3, got 2))
Forgot to add #out in the call stored procedure statement.
modifying my execution below worked.
String STORED_PROC = "call usp_get_data(:someId,:someName,#email);
Select #email"

how i can pass the parameters values to MySql Stored procedure

i have created MySQL stored procedure for the update query.
delimiter //
create procedure studentrcs(sname varchar(20),smark1 int(3),smark2 int(3),stotal int(10),inout Regno int(6))
begin
UPDATE studentrecords set student_name=sname,mark1=smark1,mark2=smark2,total=stotal where Reg_no=Regno;
end;//
After that, through callable statement how i can pass value(at run time) as parameter
CallableStatement calstat=null;
calstat=conn.prepareCall("{call studentrcs(?,?,?,?,?)}"); // **how i can pass the value here(at run time).**
//System.out.println("Update");
calstat.setString(1,tname.getText());
calstat.setString(2, treg.getText());
calstat.setString(3, tmark1.getText());
calstat.setString(4, tmark2.getText());
calstat.setString(5, ttotal.getText());
calstat.executeUpdate();
You have an INOUT parameter specified (Regno) in your stored procedure so you'll need to both specify a value for it's input and register it with the CallableStatement.registerOutParameter method. Also, try setting int values for your int input parameters too.
For example:
CallableStatement calstat=null;
calstat=conn.prepareCall("{call studentrcs(?,?,?,?,?)}"); // **how i can pass the value here(at run time).**
//System.out.println("Update");
calstat.setString(1,tname.getText());
calstat.setInt(2, Integer.parseInt(treg.getText()));
calstat.setInt(3, Integer.parseInt(tmark1.getText()));
calstat.setInt(4, Integer.parseInt(tmark2.getText()));
calstat.setInt(5, Integer.parseInt(ttotal.getText()));
calstat.registerOutParameter(5, Types.NUMERIC);
calstat.executeUpdate();
I'm assuming that all of this is in a try catch finally block so I have left out handling the NumberFormatException that will be raised if tmark1.getText(), tname.getText(), tmark2.getText() or ttotal.getText() return not valid integer values.
More info about calling MySQL routines from Java here
As an aside I don't know why you just don't just dump the SQL update method into it's own Java method and call that rather than creating a stored MySQL routine. Just my opinion though!

Retrieve a value inside a stored procedure and use it inside that stored procedure

delimiter //
CREATE DEFINER=root#localhost PROCEDUREgetData(IN templateName VARCHAR(45),IN templateVersion VARCHAR(45),IN userId VARCHAR(45))
BEGIN
set #version = CONCAT("SELECT saveOEMsData_answersVersion FROMsaveOEMsData WHERE saveOEMsData_templateName = '",templateName,"' ANDsaveOEMsData_templateVersion = ",templateVersion," AND saveOEMsData_userId= ",userId);
PREPARE s1 from #version;
EXECUTE S1;
END // delimiter ;
I am retrieving saveOEMsData_answersVersion, but I have to use it in an IF loop, as in if the version == 1, then I would use a query, else I would use something else. But I am not able to use the version. Could someone help with this?? I am only able to print but not able to use the version for data manipulation. The procedure works fine but I am unable to proceed to next step which is the if condition.
The if condition would have something like the below mentioned.
IF(ver == 1) THEN SELECT "1";
END IF;
IF is allowed inside a Procedure, you can not use IF inside SQL, in SQL you can use CASE, but that doesn't have the same abilities.
Build a second procedure to handle the IF/Then for the results of getData

Getting stored procedure's return value with iBatis.NET

How can I retrieve the return value of a stored procedure using iBatis.NET? The below code successfully calls the stored procedure, but the QueryForObject<int> call returns 0.
SqlMap
<procedure id="MyProc" parameterMap="MyProcParameters" resultClass="int">
MyProc
</procedure>
<parameterMap id="MyProcParameters">
<parameter property="num"/>
</parameterMap>
C# code
public int RunMyProc( string num )
{
return QueryForObject < int > ( "MyProc", new Hashtable { { "num", num } } );
}
Stored Procedure
create procedure MyProc
#num nvarchar(512)
as
begin
return convert(int, #num)
end
FYI, I'm using iBatis 1.6.1.0, .NET 3.5, and SQL Server 2008.
It's not pretty, but this works:
SqlMap
<statement id="MyProc" parameterClass="string" resultClass="int">
declare #num int
exec #num = MyProc #value#
select #num
</statement>
C# code
public int RunMyProc( string num )
{
return QueryForObject < int > ( "MyProc", num );
}
You might want to check out the following article
http://www.barebonescoder.com/2010/04/ibatis-net-stored-procedures-return-values/ on how to retrieve return values.
I've used it in QueryForObject and Insert scenarios where the last statement is a return statement in the stored procedure.
Pay particular attention to the class attribute on the "parameterMap" element. It's a lot prettier than the answer above and I believe it's more inline with the way IBatis.Net was intended to be used.
Stored procedures don't have a return value like functions.
So, I don't think that will work. Try using output parameters instead.
I'm not sure about your application logic, but your procedure would be better like this:
create procedure MyProc
#num nvarchar(512)
as
begin
DECLARE #ReturnValue int
BEGIN TRY
SET #ReturnValue=convert(int, #num)
END TRY
BEGIN CATCH
SET #ReturnValue=0 --procedures can not return null, so set some default here
END CATCH
return #ReturnValue
end