今天寫了乙個呼叫儲存過程的方法

2021-08-30 07:24:59 字數 1844 閱讀 4849

1、介面

/**

* 呼叫儲存過程

* * @param procname 儲存過程名. 如:testprocparam、mypack.testprocparam

* @param inparams 輸入引數對映物件. 格式為:索引號->值

* @param outtypes 輸出引數型別對映物件. 格式為:索引號->型別

* @return map輸出結果對映物件. 格式為:索引號->值

*/public mapcallprocedure(string procname,

mapinparams, mapouttypes);

2、實現方法

public mapcallprocedure(final string procname,

final mapinparams, final mapouttypes)

if (maputils.isnotempty(outtypes))

// 設定問號字串, 以逗號隔開

for (int i = 0; i < paramcount; i++)

if (!"".equals(questionmark)) questionmark = questionmark.substring(0, questionmark.length() - 1);

// 獲取資料庫連線

connection con = session.connection();

// 建立呼叫儲存過程的callablestatement物件

string sql = "";

callablestatement cstmt = con.preparecall(sql);

// 設定輸入引數

if (maputils.isnotempty(inparams))

for (map.entryentry : inparams.entryset())

// 註冊輸出引數

if (maputils.isnotempty(outtypes))

for (map.entryentry : outtypes.entryset())

// 執行儲存過程

cstmt.execute();

// 獲取輸出引數結果

if (maputils.isnotempty(outtypes))

for (map.entryentry : outtypes.entryset())

return resultmap;

}});

}

3、使用

string procname = "pack_task.proc_softwore_update_analyze"; // 儲存過程名

mapinparams = new hashmap(); // 輸入引數. 格式為:索引號->值

mapouttypes = new hashmap(); // 輸出引數型別. 格式為:索引號->型別

// 設定輸入引數

inparams.put(1, taskid);

// 設定輸出型別

outtypes.put(2, types.integer);

outtypes.put(3, types.varchar);

// 呼叫儲存過程

mapresultmap = softwareresultrepository.callprocedure(procname, inparams, outtypes);

// 獲取輸出引數值

resultmap.get(2);

resultmap.get(3);

SQL用乙個儲存過程呼叫另乙個儲存過程

一 直接呼叫的例子 建被呼叫儲存過程b create procedure b sql nvarchar 500 null,outpara nvarchar 500 output asdeclare sqlstr nvarchar 500 begin set outpara sql end 建呼叫儲存...

SQL 儲存過程裡呼叫另乙個儲存過程

由於建立了乙個儲存過程,並且要在另乙個儲存過程裡呼叫這個儲存過程所以在網上找了一下相關的 現在總結一下,防止以後還會用到 由於這次我寫的儲存過程只需要返回乙個求和的結果,所以我使用了output 引數,即執行了儲存過程以後返回乙個值 具體 如下 create procedure dbo t1 tes...

寫了乙個簡單的對多游標控制的儲存過程

寫了乙個簡單的對多游標控制的儲存過程。此例是示範游標的具體使用方法。不過生產過程中不推薦使用游標。因為完全可以用into 變數語句和迴圈來代替游標。1 sp delimiter drop procedure if exists test2 sp cur demo create definer roo...