I'm trying to call a procedure from within my Java code. I first create a CallableStatement, then I associate my parameters with setString(x, "value").
My procedure SELECT the first result in a query. I can execute the procedure from SQL Server without a problem and get a result. From my Java code, I can't get anything except my IN parameters which are in my ResultSet...I don't understand how it is possible.
Here is the procedure creation script :
CREATE PROCEDURE [dbo].[sp_getTask]
-- Add the parameters for the stored procedure here
#actionTypeParam nvarchar(50) = '',
#serverNameParam nvarchar(10) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT #actionTypeParam, #serverNameParam
SELECT TOP 1 actionId, actionType, partner, fileProt FROM dbo.Ordonnancement WHERE actionType = #actionTypeParam AND serverName = #serverNameParam AND actionState = 'ACTIVE' ORDER BY actionRetry ASC, actionLocalDateTime ASC;
END
GO
And here is my Java code calling this procedure :
private static List<Action> getSyncTask() {
List<Action> tasks = new ArrayList<Action>();
Connection cnx;
ResultSet rs = null;
try {
cnx = centralDb.getConnection();
CallableStatement cstmt = cnx.prepareCall(PMASchedulerProcedures.GET_TASK);
cstmt.setString(1, "sync");
cstmt.setString(2, PMAUtils.getHostName());
cstmt.execute();
rs = cstmt.getResultSet();
} catch (SQLException e) {
logger.error("[" + String.format("%-25s","Sync program")+" ] : "
+ "Cannot get SQL connection : " + e.getMessage());
}
try {
while (rs.next()) {
Action action = new Action();
action.setActionId(rs.getInt(1));
action.setActionType(ActionType.fromName(rs.getString(2)));
action.setPartner(rs.getString(3));
action.setFileProt(rs.getString(4));
tasks.add(action);
}
} catch (SQLException e) {
logger.error("[" + String.format("%-25s","Sync program")+" ] : "
+ "Cannot gather action informations : " + e.getMessage());
}
return tasks;
}
I get the following result :
rs.getString(1) : sync
rs.getString(2) : PF11-40A
Which are the two parameters I set in my CallableStatement.
If anyone has an idea about what happened here, I would be glad to hear it :)
I know nothing about Java, but your SQL stored procedure is returning two sets of results. I suspect the first SELECT, which is just getting the parameters that were passed in, was set up for some type of testing. Try getting rid of the SELECT of the parameters like below:
CREATE PROCEDURE [dbo].[sp_getTask]
-- Add the parameters for the stored procedure here
#actionTypeParam nvarchar(50) = '',
#serverNameParam nvarchar(10) = ''
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
-- Insert statements for procedure here
SELECT TOP 1 actionId, actionType, partner, fileProt FROM dbo.Ordonnancement WHERE actionType = #actionTypeParam AND serverName = #serverNameParam AND actionState = 'ACTIVE' ORDER BY actionRetry ASC, actionLocalDateTime ASC;
END
GO
Related
I want to use procedure multiple times to get many table select from oracle database
My Oracle procedure
PROCEDURE getInfo(
Status IN VARCHAR2,
P_CUR OUT REFCURSOR)
AS
BEGIN
OPEN P_CUR FOR
SELECT *
FROM TABLE
WHERE TABLE.STATUS = Status
END;
Here is my Java call the the procedure. It doesn't work, I can not set registerOutParameter for PreparedStatement to get the cursor data.
PreparedStatement pstmt = null;
pstmt = cnn.prepareCall("{call " + schemaName + ".LOC_EXCHANGE.getInfo(?,?)}");
for (Entity entity : ListEntity) {
int i = 1;
pstmt.setString(i++, entity.getTxnId());
pstmt.registerOutParameter(i, OracleTypes.CURSOR);
pstmt.addBatch();
}
pstmt.executeBatch();
cnn.commit();
rs = (ResultSet) pstmt.getObject(i);
Fetching the data in bulk rather than in batch
I don't think you can batch those calls. You could probably do an anonymous block, instead:
BEGIN
LOC_EXCHANGE.getInfo(?, ?);
LOC_EXCHANGE.getInfo(?, ?);
LOC_EXCHANGE.getInfo(?, ?);
...
END;
Avoiding N+1
Personally, I think you're producing an N+1 problem here. rather than fetching data for each individual entity.getTxnId(), how about refactoring your getInfo() procedure to allow for returning data for a list of txnids? Do this:
CREATE TYPE txnids AS TABLE OF VARCHAR2(4000);
And then declare:
PROCEDURE getInfo(
Status IN txnids ,
P_CUR OUT REFCURSOR)
AS
BEGIN
OPEN P_CUR FOR
SELECT *
FROM TABLE
WHERE TABLE.STATUS IN (
SELECT * FROM TABLE (Status)
)
ORDER BY TABLE.STATUS
END;
Since you're projecting *, you'll still be able to access the STATUS column in order to group data in the client, after fetching it all.
drop PROCEDURE if exists insert_poo;
DELIMITER $$
CREATE PROCEDURE insert_poo(IN barcode varchar(250),IN qty float,IN amount float,IN vat float,IN
description varchar(250),IN clrk_code varchar(20),IN mechno varchar(20),IN bill_date datetime)
BEGIN
DECLARE unit_pric float;
DECLARE itemcode varchar(150);
SET unit_pric =(select retail1 FROM `mytable` WHERE `mytable`.barcode = barcode);
SET itemcode =(select prod_id FROM `mytable` WHERE `mytable`.barcode = barcode);
INSERT into mytable2(clrk_code,tran_code,tran_desc,tran_qty,unit_price,tran_amt,bill_date,tax)values(clrk_code,barcode,description,qty, unit_pric,amount,bill_date,vat)
END $$
DELIMITER ;
Kindly help with any solution on how to create and call it. thanks in advance
Probably you can use the following example as a reference:
The below one is the Store Procedure itself:
connect anyDbName/anyDbName
CREATE OR REPLACE PROCEDURE any_storeProcedure_name
-- Following are some example parameters you may use in your SP
(
id varchar2,
name_param varchar2,
-- The control status of the operation
statusOperation_out OUT VARCHAR2
)
AS
BEGIN
statusOperation_out := 'in_proccess';
insert into property_name values('Name', id, name_param);
COMMIT;
statusOperation_out := 'ok';
EXCEPTION
WHEN OTHERS THEN
statusOperation_out := 'error';
ROLLBACK;
END;
/
And the following is the Java method call that is using the previous SP:
public long addProperties(String id, String name) {
//The string sql syntax for calling the store procedure
String sql = "{CALL any_storeProcedure_name("
+ "id => ?, "
+ "name => ?)}";
try (Connection conn = CreateConnection.getDSConnection();
CallableStatement cs = conn.prepareCall(sql)) {
//The following are the parameters for the store procedure
cs.setString (1, id);
cs.setString (2, name);
//Following are the parameters to get some outputs from the store procedure
cs.registerOutParameter(3, Types.VARCHAR);
cs.executeQuery();
//The return varible from the store procedure is the one
//that is being used for feedback on whether the SP ran fine.
if (cs.getString(3).equalsIgnoreCase("ok")) {
System.out.println("Feedback from SP is: " + cs.getString(3));
return 1;
} else {
return 0;
}
} catch (SQLException e) {
e.printStackTrace();
return 0;
}
}
So hope this can give some reference, by the way the DB I'm using is Oracle 11g. But I recall it's very similar to MySQL DB.
I have a stored procedure that calls another stored procedure. The inner stored procedure returns a result set. After using a CallableStatement to execute the calling stored procedure I am unable to get the result set returned by called stored procedure.
I tried both execute and executeQuery for execution of callable statement. When I execute the calling stored procedure from SQL Server I am getting proper results.
Calling procedure:-
ALTER PROC [User].[Get_Data]
(#UserID NVARCHAR(20))
AS
BEGIN
Select 'User Data'
Exec [Order].[Get_Order] #UserID
END
Called procedure:-
ALTER PROC [Order].[Get_Order]
(#UserID NVARCHAR(20))
AS
BEGIN
Select * from orders where userId=#UserID
END
Your outer stored procedure is returning two result sets:
The results from Select 'User Data'
The results from Exec [Order].[Get_Order] #UserID
You need to call .getMoreResults() in order to retrieve the second result set, e.g.,
CallableStatement cs = connection.prepareCall("{CALL Get_Data (?)}");
cs.setString(1, "gord");
ResultSet rs = cs.executeQuery();
System.out.println("[First result set]");
while (rs.next()) {
System.out.printf("(No column name): %s%n", rs.getString(1));
}
cs.getMoreResults();
rs = cs.getResultSet();
System.out.println();
System.out.println("[Second result set]");
while (rs.next()) {
System.out.printf("userId: %s, orderId: %s%n",
rs.getString("userId"), rs.getString("orderId"));
}
producing
[First result set]
(No column name): User Data
[Second result set]
userId: gord, orderId: order1
userId: gord, orderId: order2
(Tested using mssql-jdbc-6.2.1.jre8.jar connecting to SQL Server 2014.)
For more details, see
How to get *everything* back from a stored procedure using JDBC
You cannot select the results of a stored procedure directly within SQL Server itself. You need to first insert the result into a temp table as per example below.
Example use:
-- Create a tempoary table to store the results.
CREATE TABLE #UserOrderDetail
(
UserData NVARCHAR(50) -- Your columns here
)
-- Insert result into temp table.
-- Note that the columns returned from procedure has to match columns in your temp table.
INSERT INTO #UserOrderDetail
EXEC [Order].[Get_Order] #UserID
-- Select the results out of the temp table.
SELECT *
FROM #UserOrderDetail
If the intent is to simply return one or more result sets to a client application, you should ensure that the SET NOCOUNT ON statement is added to the top of your stored procedures, this will prevent SQL Server from sending the DONE_IN_PROC messages to the client for each statement in the stored procedure. Database libraries like ODBC, JDBC and OLEDB can get confused by the row counts returned by the various insert and update statements executed within a SQL Server stored procedures. Your original procedure will look as follow:
ALTER PROC [User].[Get_Data]
(
#UserID NVARCHAR(20)
)
AS
BEGIN
SET NOCOUNT ON
SELECT 'User Data'
EXEC [Order].[Get_Order] #UserID
END
The correct way to do this with JDBC
Getting this right with JDBC is quite hard. The accepted answer by Gord Thompson might work, but it doesn't follow the JDBC spec to the word, so there might be edge cases where it fails, e.g. when there are interleaved update counts (known or accidental), or exceptions / messages.
I've blogged about the correct approach in detail here. The Oracle version is even more tricky, in case you need it. So here it goes:
// If you're daring, use an infinite loop. But you never know...
fetchLoop:
for (int i = 0, updateCount = 0; i < 256; i++) {
// Use execute(), instead of executeQuery() to handle
// leading update counts or exceptions
boolean result = (i == 0)
? s.execute()
: s.getMoreResults();
// Warnings here
SQLWarning w = s.getWarnings();
for (int j = 0; j < 255 && w != null; j++) {
System.out.println("Warning : " + w.getMessage());
w = w.getNextWarning();
}
// Don't forget this
s.clearWarnings();
if (result)
try (ResultSet rs = s.getResultSet()) {
System.out.println("Result :");
while (rs.next())
System.out.println(" " + rs.getString(1));
}
else if ((updateCount = s.getUpdateCount()) != -1)
System.out.println("Update Count: " + updateCount);
else
break fetchLoop;
}
Using jOOQ
Note that in case you're using jOOQ, you could leverage code generation for your stored procedures and call the simplified API to do this in a few lines only:
GetDatap = new GetData();
p.setUserId("gord");
p.execute(configuration);
Results results = p.getResults();
for (Result<?> result : results)
for (Record record : result)
System.out.println(record);
Disclaimer: I work for the company behind jOOQ
I am forced to use createSQLQuery to insert values into tables with an Identity column (the first column and the primary key) using hibernate. Using hibernate classes are not an option since the tables are created on the fly for each customer that is added to the system. I have run the query and it successfully inserts into the table. I then execute a "select scope_identity()" and it always returns null. "select ##Identity" works but that is not guaranteed to be the correct one. I have also tried to append "select scope_identity()" to the insert query. Then I tried query.list() and query.uniqueResult() both of which throw the hibernate exception of "No Results ..."
Session session = DatabaseEngine.getSessionFactory().openSession();
String queryString = "insert into table1 (dataid) values (1)"
SQLQuery query = session.createSQLQuery(insertQueryString);
query.executeUpdate();
query = session.createSQLQuery("select scope_identity()");
BigDecimal entryID = (BigDecimal)query.uniqueResult();
The simple example table is defined as follows:
"CREATE TABLE table1 (EntryID int identity(1,1) NOT NULL," +
"DataID int default 0 NOT NULL, " +
"PRIMARY KEY (EntryID))";
Is there a way I am missing to use scope_identity() with createSQLQuery?
Actually the SQLServerDialect class used by Hibernate uses the same "scope_identity()" too.
The reason why it's not working is because you need to execute those in the same statement or stored procedure.
If you execute the scope_identity() call in a separate statement, SQL Server will not be able to give you last inserted identity value.
You cannot do it with the SQLQuery, even Hibernate uses JDBC to accomplish this task. I wrote a test on GitHub to emulate this and it works like this:
Session session = entityManager.unwrap(Session.class);
final AtomicLong resultHolder = new AtomicLong();
session.doWork(connection -> {
try(PreparedStatement statement = connection.prepareStatement("INSERT INTO post VALUES (?) select scope_identity() ") ) {
statement.setString(1, "abc");
if ( !statement.execute() ) {
while ( !statement.getMoreResults() && statement.getUpdateCount() != -1 ) {
// do nothing until we hit the resultset
}
}
try (ResultSet rs = statement.getResultSet()) {
if(rs.next()) {
resultHolder.set(rs.getLong(1));
}
}
}
});
assertNotNull(resultHolder.get());
The code uses Java 8 lambdas instead of anonymous classes, but you can easily port it to Java 1.7 too.
Hi StackOverflow community :)
I come to you to share one of my problems...
I have to extract a list of every table in each database of a SQL Server instance, I found this query :
EXEC sp_msforeachdb 'Use ?; SELECT DB_NAME() AS DB, * FROM sys.tables'
It works perfectly on Microsoft SQL Server Management Studio but when I try to execute it in my Java program (that includes JDBC drivers for SQL Server) it says that it doesn't return any result.
My Java code is the following :
this.statement = this.connect.createStatement(); // Create the statement
this.resultats = this.statement.executeQuery("EXEC sp_msforeachdb 'Use ?; SELECT DB_NAME() AS DB, * FROM sys.tables'"); // Execute the query and store results in a ResultSet
this.sortie.ecrireResultats(this.statement.getResultSet()); // Write the ResultSet to a file
Thanks to anybody who will try to help me,
Have a nice day :)
EDIT 1 :
I'm not sure that the JDBC driver for SQL Server supports my query so I'll try to get to my goal in another way.
What I'm trying to get is a list of all the tables for each database on a SQL Server instance, the output format will be the following :
+-----------+--------+
| Databases | Tables |
+-----------+--------+
So now I'm asking can someone help me to get to that solution using SQL queries thru Java's JDBC for SQL Server driver.
I also wish to thanks the very quick answers I got from Tim Lehner and Mark Rotteveel.
If a statement can return no or multiple results, you should not use executeQuery, but execute() instead, this method returns a boolean indicating the type of the first result:
true: result is a ResultSet
false : result is an update count
If the result is true, then you use getResultSet() to retrieve the ResultSet, otherwise getUpdateCount() to retrieve the update count. If the update count is -1 it means there are no more results. Note that the update count will also be -1 when the current result is a ResultSet. It is also good to know that getResultSet() should return null if there are no more results or if the result is an update count.
Now if you want to retrieve more results, you call getMoreResults() (or its brother accepting an int parameter). The return value of boolean has the same meaning as that of execute(), so false does not mean there are no more results!
There are only no more results if the getMoreResults() returns false and getUpdateCount() returns -1 (as also documented in the Javadoc)
Essentially this means that if you want to correctly process all results you need to do something like below:
boolean result = stmt.execute(...);
while(true)
if (result) {
ResultSet rs = stmt.getResultSet();
// Do something with resultset ...
} else {
int updateCount = stmt.getUpdateCount();
if (updateCount == -1) {
// no more results
break;
}
// Do something with update count ...
}
result = stmt.getMoreResults();
}
NOTE: Part of this answer is based on my answer to Java SQL: Statement.hasResultSet()?
If you're not getting an error, one issue might be that sp_msforeachdb will return a separate result set for each database rather than one set with all records. That being the case, you might try a bit of dynamic SQL to union-up all of your rows:
-- Use sys.tables
declare #sql nvarchar(max)
select #sql = coalesce(#sql + ' union all ', '') + 'select ''' + quotename(name) + ''' as database_name, * from ' + quotename(name) + '.sys.tables'
from sys.databases
select #sql = #sql + ' order by database_name, name'
exec sp_executesql #sql
I still sometimes use INFORMATION_SCHEMA views as well, since it's easier to see the schema name, among other things:
-- Use INFORMATION_SCHEMA.TABLES to easily get schema name
declare #sql nvarchar(max)
select #sql = coalesce(#sql + ' union all ', '') + 'select * from ' + quotename(name) + '.INFORMATION_SCHEMA.TABLES where TABLE_TYPE = ''BASE TABLE'''
from sys.databases
select #sql = #sql + ' order by TABLE_CATALOG, TABLE_SCHEMA, TABLE_NAME'
exec sp_executesql #sql
Be aware that this method of string concatenation (select #sql = foo from bar) may not work as you intend through a linked server (it will only grab the last record). Just a small caveat.
UPDATE
I've found the solution !
After reading an article about sp_spaceused being used with Java, I figured out that I was in the same case.
My final code is the following :
this.instances = instances;
for(int i = 0 ; i < this.instances.size() ; i++)
{
try
{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
this.connect = DriverManager.getConnection("jdbc:sqlserver://" + this.instances.get(i), "tluser", "result");
this.statement = this.connect.prepareCall("{call sp_msforeachdb(?)}");
this.statement.setString(1, "Use ?; SELECT DB_NAME() AS DB, name FROM sys.tables WHERE DB_NAME() NOT IN('master', 'model', 'msdb', 'tempdb')");
this.resultats = this.statement.execute();
while(true)
{
int rowCount = this.statement.getUpdateCount();
if(rowCount > 0)
{
this.statement.getMoreResults();
continue;
}
if(rowCount == 0)
{
this.statement.getMoreResults();
continue;
}
ResultSet rs = this.statement.getResultSet();
if(rs != null)
{
while (rs.next())
{
this.sortie.ecrireResultats(rs); // Write the results to a file
}
rs.close();
this.statement.getMoreResults();
continue;
}
break;
}
this.statement.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
It tried it out and my file has everything I want in it.
Thank you all for your help ! :)