JDBC and calling a macro
Teradata Teradata Discussion Forums Teradata.com Discussion Forum
Visit Teradata.com
Home       Guidelines    Member List
Welcome Guest ( Login | Register )
        


This online forum is for user-to-user discussions of Teradata products, and is not an official customer support channel for Teradata. If you require direct assistance, please contact Teradata support.


JDBC and calling a macro Expand / Collapse
Author
Message
Posted 7/16/2007 1:01:36 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 7/19/2007 12:16:00 PM
Posts: 5, Visits: 1
Hello,

It's easy to call a teradata procedure with the teradata JDBC driver.

But is it possible to call a macro in JDBC too?

I found no documentation of this nice feature...

Greetings
Rainer Kopp
Post #8197
Posted 7/17/2007 12:01:49 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 6/25/2008 12:24:48 AM
Posts: 425, Visits: 389
I haven't tried, but I think this can be accomplished via

[Statement/PreparedStatement].[execute() / executeQuery()] ;
Post #8201
Posted 7/17/2007 1:11:02 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 7/19/2007 12:16:00 PM
Posts: 5, Visits: 1
yes, we have tried PrepareStatement.execute(); using "call" and "execute" - but with execute/exec the DB complains
about wrong syntax...

Perhaps calling macros is not supported ?
Post #8212
Posted 7/18/2007 1:21:02 AM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: Forum Members
Last Login: 6/25/2008 12:24:48 AM
Posts: 425, Visits: 389
Here's a sample code I could get to work ....


-- Macro definition

REPLACE MACRO SEL_EMP_MAC(EMPID INTEGER)
AS
(
SELECT EMPNAME FROM EMPLOYEE WHERE EMPID = :EMPID;
);



// java program ....

import java.sql.*;

public class MacroTest001
{

public static Connection establishConnection(String args[]) throws ClassNotFoundException, SQLException
{
Class.forName("com.ncr.teradata.TeraDriver");

String jdbcUrl = "jdbc:teradata://" + args[0];
String userId = args[1];
String password = args[2];

return DriverManager.getConnection(jdbcUrl, userId, password);
}

public static void main(String args[]) throws ClassNotFoundException, SQLException
{
Connection con = establishConnection(args);
PreparedStatement ps = con.prepareStatement("EXECUTE SEL_EMP_MAC(?);");

ps.setInt(1, 100);
ResultSet rs = ps.executeQuery();

while(rs.next())
System.out.println(rs.getString("EMPNAME"));

}

}

Post #8214
Posted 7/19/2007 12:16:32 PM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 7/19/2007 12:16:00 PM
Posts: 5, Visits: 1
thanks a lot - it s working fine!
Post #8236
Posted 3/28/2008 11:21:02 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 4/1/2008 11:26:03 AM
Posts: 2, Visits: 5
Hi,

macros which use select only statements are working fine that way.
Unfortunately I'm not successful executing macros that use
update and select, e.g.:

REPLACE MACRO m_get_next_id
(
in_name VARCHAR(100) NOT NULL
)
AS
(
update t_ref_max_id
set id = id + 1
where id_name = :in_name;

select id_name,
id
from t_ref_max_id
where id_name = :in_name;
);

Executing that macro using:

PreparedStatement stmt = con.prepareStatement("EXECUTE m_get_next_id(?);");
stmt.setString(1, "process_id");
ResultSet rs = stmt.executeQuery();

I get:
SQL Exception: [NCR] [Teradata JDBC Driver] : executeQuery() cannot be used when there is no result set expected; use executeUpdate() or execute()

Using Perl::DBI via ODBC it's working OK.
So, is there any way to get that working using JDBC?
Post #11084
Posted 3/28/2008 1:22:07 PM
Supreme Being

Supreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme BeingSupreme Being

Group: PAC and SFT Members
Last Login: 7/2/2008 5:38:40 PM
Posts: 280, Visits: 297
Your multistatement macro returns both an update count and a result set. So you need to use the generic "execute" method, e.g.

boolean isResultset = stmt.execute();
// Returns FALSE, indicating first result is not a ResultSet
int updateCount = stmt.getUpdateCount;
// Returns the value of the UpdateCount result - optional
isResultSet = stmt.getMoreResults();
// Positions to next result and returns TRUE, indicating it is a ResultSet
ResultSet rs = stmt.getResultSet();
// Returns the results

// NOTE: There are no more results when both
// getMoreResults (or execute) returns FALSE (saying current result is not a ResultSet), and
// getUpdateCount returns -1 (saying current result is not an UpdateCount)
Post #11086
Posted 4/1/2008 11:26:55 AM
Forum Newbie

Forum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum NewbieForum Newbie

Group: Forum Members
Last Login: 4/1/2008 11:26:03 AM
Posts: 2, Visits: 5
Thanks very much. Works great.
Post #11109
« Prev Topic | Next Topic »


Reading This Topic Expand / Collapse
Active Users: 0