在Access自身的帮助中看到了Create Procedure语句,测试了老半天,终于发现了诀窍:必须使用OleDb连接才能使用Create Procedure语句。ODBC连接不支持该语句,提示Create Table语法错误。
创建了存储过程后,使用Office Access工具打开数据库,在“对象 - 查询”中能够看到你创建的存储过程。
创建存储过程的语法:
Create Procedure YourProc
(
@param1 varchar(254),
@param2 int
)
As
(
select * from Table1 where ID>@param2 and username=@param1
)
查询数据时只需要使用:
Rs.Open "YourProc admin,1", Conn
myproc.vbs
Set DB = GetObject("script:http://www.zope.org/Members/Rimifon/DbHelper.sct")
DB.ConnStr = "Provider=Microsoft.Jet.OleDb.4.0;Data Source=MyProc.mdb"
DB.NonQuery "create table Sheet1(id counter,name varchar(254),score int)"
DB.NonQuery "Create Procedure MyProc(@name varchar(254), @score int) as (insert into Sheet1(name,score) values(@name, @score))"
DB.NonQuery "Create Procedure Result as (select * from Sheet1)"
MsgBox "创建了表和存储过程"
DB.NonQuery "Execute MyProc Rimifon,90"
DB.NonQuery "Exec MyProc FengYun,93"
Set Ds = DB.DataSet("Result")
MsgBox "执行了存储过程"
DB.NonQuery "drop Procedure MyProc"
DB.NonQuery "drop Procedure Result"
DB.NonQuery "drop table Sheet1"
MsgBox "删除了表和存储过程"
Set DB = Nothing
Dim Result
Result="所有记录:" & Chr(13)
For Each Item In Ds
If IsObject(Item) Then
Result = Result & Item.ID & Chr(9) &_
Item.Name & Chr(9) & Chr(9) &_
Item.Score & Chr(13)
End If
Next
Set Ds = Nothing
MsgBox Result