![](/img/trans.png)
[英]Oracle stored function/procedure with VARRAY or user-defined type as IN parameter
[英]Passing a user-defined table type to a SQL Server stored procedure using JDBC
我们在 SQL Server 中有这个用户定义的表类型:
CREATE TYPE [dbo].[INITVALS_MSG] AS TABLE(
[SDate] [decimal](8, 0) NOT NULL,
[EDate] [decimal](8, 0) NOT NULL,
[PlantCode] [nvarchar](10) NOT NULL,
[LoadType] [nchar](8) NOT NULL,
[Asset] [bigint] NOT NULL
)
以及一个将该表作为输入的存储过程:
ALTER PROCEDURE [dbo].[RegisterInitAssets]
@initmsg INITVALS_MSG ReadOnly
AS
BEGIN
...
现在,我需要从 java 调用这个过程。 有可能做这样的事情吗? JDBC 支持吗?
--------编辑我在java中有一个对应的类用于该类型:
public class DBInitialAsset {
private Integer sDate;
private Integer eDate;
private String plantCode;
private String loadType;
private Integer asset;
public DBInitialAsset() {
}
}
是的,现在可以了。 Microsoft 用于 SQL Server 的 JDBC 驱动程序的 6.0 版增加了对表值参数的支持。
以下代码示例显示了如何
SQLServerDataTable
对象来保存要传递的表数据,以及SQLServerCallableStatement#setStructured
方法将该表传递给存储过程。SQLServerDataTable sourceDataTable = new SQLServerDataTable();
sourceDataTable.addColumnMetadata("SDate", java.sql.Types.DECIMAL);
sourceDataTable.addColumnMetadata("EDate", java.sql.Types.DECIMAL);
sourceDataTable.addColumnMetadata("PlantCode", java.sql.Types.NVARCHAR);
sourceDataTable.addColumnMetadata("LoadType", java.sql.Types.NCHAR);
sourceDataTable.addColumnMetadata("Asset", java.sql.Types.BIGINT);
// sample data
sourceDataTable.addRow(123, 234, "Plant1", "Type1", 123234);
sourceDataTable.addRow(456, 789, "Plant2", "Type2", 456789);
try (CallableStatement cs = conn.prepareCall("{CALL dbo.RegisterInitAssets (?)}")) {
((SQLServerCallableStatement) cs).setStructured(1, "dbo.INITVALS_MSG", sourceDataTable);
boolean resultSetReturned = cs.execute();
if (resultSetReturned) {
try (ResultSet rs = cs.getResultSet()) {
rs.next();
System.out.println(rs.getInt(1));
}
}
}
有关更多详细信息,请参阅以下 MSDN 文章:
试试这个方法:
connection = dataSource.getConnection();
CallableStatement statement = connection.prepareCall("{? = call dbo.RegisterInitAssets(?)}");
statement.registerOutParameter(1, OracleTypes.CURSOR);//you can skip this if procedure won't return anything
statement.setObject(2, new InitvalsMsg()); //I hope you some kind of representation of this table in your project
statement.execute();
ResultSet set = (ResultSet) statement.getObject(1);//skip it too if its not returning anything
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.