* Java에서 TableType으로 Procedure 호출 *


- Procedure

CREATE PROCEDURE [dbo].[table_sel]
@tbl INT32Table READONLY
AS
BEGIN
SET NOCOUNT ON;
SELECT value FROM @tbl
END


-아래는 호출이 안되는 script

SqlConnection sqlconn;
System.Data.DataTable tbl = new System.Data.DataTable("INT32Table", "dbo");
tbl.Columns.Add("value", typeof(int));
tbl.Rows.Add(2);
tbl.Rows.Add(3);
tbl.Rows.Add(5);
tbl.Rows.Add(7);
tbl.Rows.Add(11);
using (SqlCommand command = new SqlCommand("table_sel"))
{
    command.Connection = sqlconn;
    command.Parameters.AddWithValue("@tbl", tbl);
    command.CommandType = System.Data.CommandType.StoredProcedure;
    SqlDataReader reader = command.ExecuteReader();

    //do stuff
}


-아래는 될거 같은 script 1

java.sql.connection connection = //driver, url, database, credentials ...

try
{
    PreparedStatement ps =
        connection.prepareStatement("insert into tbl values (?)");
    ps.setInt(1, your 1st int);
    ps.addBatch();
    ps.setInt(1, your 2nd int);
    ps.addBatch();
    ps.setInt(1, your 3rd int);
    ps.addBatch();
    ps.executeBatch();
}
catch (SQLException e)
{
    // err handling goes here
}
finally
{
    // close your resources
}



-아래는 될거 같은 script 2

java.sql.connection connection = //driver, url, database, credentials ...

try
{
    CallableStatement cs =
        connection.prepareCall("{ call table_sel(?) }");
    cs.setInt(1, your int);
    cs.execute();
}
catch (SQLException e)
{
    // err handling goes here
}
finally
{
    // close your ressources
}


'(DB) MS SQL > Procedure (단계별스터디)' 카테고리의 다른 글

MS SQL.PLSQL - 문법  (0) 2017.01.27
MS SQL.PLSQL - 단점  (0) 2017.01.27
MS SQL.PLSQL - 장점  (0) 2017.01.27
MS SQL.PLSQL - 특징  (0) 2017.01.27
Java에서 TableType으로 Procedure 호출 2  (0) 2017.01.06
Posted by 농부지기
,