程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> SqlServer數據庫 >> 關於SqlServer >> 在SQL Server的存儲過程調用Com組件

在SQL Server的存儲過程調用Com組件

編輯:關於SqlServer
SQL Server 的 T-Sql 語言的功能是 非常的強大,但是有個時候 也確實是有些限制和不方便,為什麼不象 ASP 一樣 大量的借用組件呢?開始在 Sql online book 中查找,終於找到了 一個 Sql 的 系統存儲過程 sp_OACreate,下面大家就一起去 看看這個 存儲過程的神氣之處吧

s首先我們先用VB 作一個最簡單的組件 ,因為是介紹性的文章,所以這個組件是非常的的簡單,在具體的
工作中,可以寫個 比這個 業務復雜的多的 組件
Project Name: testSQLCOM
Class Name: TestMath
Public Function AddMe(a As Long, b As Long) As Long
AddMe = a + b
End Function
編譯生成後,我們就可以在 SQL Server 中對這個 Com 組件進行調用了
declare @i int
declare @intRet int
declare @intRetCode int
DECLARE @strErr varchar (255)
DECLARE @strErr1 varchar (255)
/* 首先創建Com 實例 */
exec @ret_code = sp_OACreate "testSQLCOM.TestMath", @i out
IF @intRetCode <> 0
BEGIN
/* 創建實例 失敗 */
EXEC sp_OAGetErrorInfo @i, @strErr OUT, @strErr1 OUT
PRINT "創建實例失敗,失敗的原因是:: " + @strErr + " " + @strErr1
RETURN
END

/* 創建成功,開始調用 */
EXEC @intRetCode = sp_OAMethod @i,'AddMe',@ret OUT,100,200
IF @intRetCode <> 0
BEGIN
/* 調用方法出錯 */
EXEC sp_OAGetErrorInfo @i, @strErr OUT, @strErr1 OUT
PRINT "調用方法失敗,失敗的原因是:: " + @strErr + " " + @strErr1
EXEC sp_OADestroy @i
RETURN
END
PRINT "返回的結果是" + Str(@intRet)
exec sp_OADestroy @i

以前是存儲過程的輸出

Step 4:
返回的結果是 300

<
  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved