Access中使用Create Procedure创建存储过程第1/2页
程序员文章站
2022-06-15 14:21:49
在access自身的帮助中看到了create procedure语句,测试了老半天,终于发现了诀窍:必须使用oledb连接才能使用create proc...
在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
创建了存储过程后,使用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
1