那位朋友讲解一下存储过程?分是一定给的,多谢了!

lci21 2001-08-28 01:53:05
那位朋友讲解一下:
什么是存储过程,有什么用?
怎样创建一个存储过程?
最好觉个例子,
如果你觉得分不够,还可以加!
多谢了!
...全文
113 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
xmao 2001-08-28
  • 打赏
  • 举报
回复
lci21 2001-08-28
  • 打赏
  • 举报
回复
上面的话是对leeyoong(莫西)老兄说的
lci21 2001-08-28
  • 打赏
  • 举报
回复
可惜看不懂啊
KingSunSha 2001-08-28
  • 打赏
  • 举报
回复
我来几段书上的吧(翻译自ORACLE PL/SQL HELP):

***子程序
子程序(过程和函数)能被单独编译并永久的储存在数据库中,随时准备运行. 一个用ORACLE工具预先生成的子程序被称为"存储子程序".
你可以交互式地在SQL*PLUS或SQL*DBA中使用CREATE PROCEDURE和CREATE FUNCTION指令. 举例,你可以如下生成一个叫"fire_employee"(开除员工)的过程:
CREATE PROCEDURE fire_employee (emp_id NUMBER) AS
BEGIN
DELETE FROM emp WHERE empno = emp_id;
END;
子程序通过编译后被加入数据库的数据字典, 成为该数据库的一个对象, 它可以被任意多个连接到数据库的对象引用.
定义在一个包中的子程序称为"打包"子程序;独立定义的则称为独立子程序.
你可以在触发器,另一个子程序,一个预编译过的ORACLE应用,一个OCI应用或从SQL*PLUS或SQL*DBA中交互式地调用存储子程序, 举例,你可以如下从SQL*PLUS中调用独立存储过程"fire_employee':
SQL> EXECUTE fire_employee(7499);
子程序已被预先编译过. 所以,当被调用的时候,它们会被立即传输给PL/SQL引擎. 同时,存储子程序可以提高ORACLE的共享内存中的能力. 当被多个用户调用时,在数据库内存中只需要调入一次子程序.

***过程
一个过程是一段子程序,用来执行一个特定的动作. 生成一个存储过程的命令格式如下:
CREATE PROCEDURE proc_name
(parameter list)
AS
variable declare;
...
BEGIN
sql statement;
...
END;


哇!好累!英文不好请见谅, 还有问题的话建议多看一些参考书
leeyoong 2001-08-28
  • 打赏
  • 举报
回复
你如果看懂了上面的程序,也就明白了答案!
leeyoong 2001-08-28
  • 打赏
  • 举报
回复
CREATE PROCEDURE up_i_scpdmx
(
@msdm char(2) ,
@pclx int , /***********************************************************************
pclx:
0 is to update the record of table_KCB which field_MSDM_HH
exist in table_PDMXB
1 is to update the record of table_KCB which field_MSDM_HH
exist in table_PDMXB, and to update field_KCSL with 0
of the record of table_KCB which field_MSDM_HH
does't exist in table_PDMXB
2 is to update the record of table_KCB which field_MSDM_HH
exist in table_PDMXB, and to update field_QCS with
field_KCSL of the record of table_KCB which
field_MSDM_HH does't exist in table_PDMXB
************************************************************************/
@kccllx int=0 /***********************************************************************
0 is not to delete the record of table_KCB which field_KCSL is 0
1 is to delete the record of table_KCB which field_KCSL is 0
************************************************************************/
)
AS
begin
declare @trancount int,
@getday char(16),
@getstr char(14),
@getnum int ,
@strlen int ,
@pdkey char(16),
@pdpch char(14),
@scs int,
@kcsl int
select @trancount = @@trancount
/*********************************************************************************************
Data_Flow Interrupt:
parameter check
if pclx=1
begin
generate the data that exists in table_KCB but does not exists in
table_PDMXB,fill field_SCS with 0
end
if pclx=2
begin
generate the data that exists in table_KCB but does not exists in
table_PDMXB,fill field_SCS with field_kcsl
end
insert data into table_MSBFB
update table_KCB
if kccllx=1
begin
delete the record of table_KCB which field_KCSL is 0
end
remark table_PDMXB

**********************************************************************************************/
/**************************************************
Save the point of the transaction
***************************************************/
if @trancount = 0
begin transaction tran_pdjz
else
save transaction tran_pdjz
/**************************************************
check the parameter is valid or not
***************************************************/
if @kccllx not in (0,1)
begin
raiserror('The parameter @KCCLLX is wrong ',11,1)
if @trancount = 0
rollback transaction tran_pdjz
return -1
end
if @pclx not in (0,1,2)
begin
raiserror('The parameter @PCLX is wrong ',11,1)
if @trancount = 0
rollback transaction tran_pdjz
return -1
end
if @msdm not in (select msdm
from msbdmb
)
begin
raiserror("The parameter @MSDM is invalid",11,1)
if @trancount = 0
rollback transaction tran_pdjz
return -1
end
/**************************************************
process data
***************************************************/
select pdkey,msdm,hh,scs,kcsl ,zzr
into #pdtmp
from pdmxb
where isnull(clbz,0) = 0 and msdm = @msdm
if @@error<>0
goto error
if (select count(*)
from #pdtmp
)=0
begin
if @trancount = 0
commit transaction tran_pdjz
return 0
end
if @pclx in (1,2)
begin
select kcb.msdm,kcb.hh,kcb.kcsl
into #kcb_sub_pdmxb
from kcb
where kcb.msdm=@msdm and
not exists(
select *
from #pdtmp
where #pdtmp.msdm=kcb.msdm and
#pdtmp.hh=kcb.hh
)
if @@error<>0
goto error
if @pclx=1
begin
insert #pdtmp (pdkey,msdm,hh,scs,kcsl ,zzr)
select "",msdm,hh,0,kcsl,""
from #kcb_sub_pdmxb
if @@error<>0
goto error
end
if @pclx=2
begin
insert #pdtmp (pdkey,msdm,hh,scs,kcsl ,zzr)
select "",msdm,hh,kcsl,kcsl,""
from #kcb_sub_pdmxb
if @@error<>0
goto error
end
end
/**********************************************************************
get maximum pdkey for update the field_PDPCH of table_PDMXB
***********************************************************************/
select @pdpch=max(pdkey)
from #pdtmp
/**********************************************************************
insert into table_MSBFB
***********************************************************************/
insert msbfb (msdm,hh,dj,sl,bz,zzr,gnbz)
select pd_data.msdm,
pd_data.hh,
smxxb.dj,
pd_data.kcsl - pd_data.scs,
@pdpch+":盘点",
"0000",
1
from smxxb,
(
select msdm,hh,sum(scs) "scs",min(kcsl) "kcsl"
from #pdtmp
group by msdm,hh
) pd_data
where smxxb.hh=pd_data.hh
if @@error<>0
goto error
/*********************************************************************
update the table_KCB
**********************************************************************/
update kcb
set kcb.kcsl=pd_data.scs,
kcb.ljshs=0,
kcb.ljxss=0,
kcb.ljsts=0,
kcb.ljfts=0,
kcb.ljyks=0,
kcb.ljbfs=0,
kcb.ljdcs=0,
kcb.ljsrs=0,
kcb.qcs=pd_data.scs
from kcb,
(
select msdm,hh,sum(scs) "scs",min(kcsl) "kcsl"
from #pdtmp
group by msdm,hh
) pd_data
where pd_data.msdm=kcb.msdm and
pd_data.hh=kcb.hh
if @@error<>0
goto error
if @kccllx=1
begin
/*****************************************************************
delete the record of table_KCB which field_KCSL is 0
******************************************************************/
delete kcb
where msdm=@msdm and kcsl=0
if @@error<>0
goto error
end
/**********************************************************
remark the field_CLBZ of table_PDMXB with 1 meant
that the records were processed
***********************************************************/
update pdmxb
set pdmxb.clbz=1,
pdmxb.pdpch=@pdpch
from pdmxb,#pdtmp
where pdmxb.pdkey=nullif(#pdtmp.pdkey,"")
if @@error<>0
goto error
if @trancount = 0
commit transaction tran_pdjz
return 0
error:
raiserror('SQL do not execute successfully',11,1)
if @trancount = 0
rollback transaction tran_pdjz
return -1
end

GO

shinebei 2001-08-28
  • 打赏
  • 举报
回复
有点像DOS时代的批处理一样,把若干命令放到一起,一次就执行完,用不到一条一条地去弄,大概就是这么个意思,专业的解释有点枯燥,一并附上:
存储过程是利用SQL Server所提供的Tranact-SQL语言所撰写的程序。Tranact-SQL语言是SQL SERVER提供专为设计数据库应用程序的语言,是应用程序和SQL SERVER数据库间的主要程序式设计界面。好比ORACLE数据库里的Pro-SQL和Informix里的Informix-4GL语言一样。这类语言有以下功能:
1、参数声明
2、ANSI相容的SQL命令(如select,update...)
3、一般流程控制命令(if..else, while...)
4、内建函数
存储过程最主要的特色是当你写完一个存储过程后即被翻译成可执行码保存在系统表内,当做是数据库的对象之一,一般用户只要执行存储过程,并且提供存储过程所需参数就可以得到他要的结果而不用接触到SQL命令。给你一个简单的例子

CREATE PROCEDURE abcd --此处是创建,创建一名为abcd的存储过程,AS后面跟存储过程具体内容
AS

begin tran --事务起点(事务概念,呵,自己找吧)

--判断A表中NAME是否为空
if (select NAME from A) is not null
rollback tran --中断事务
else
exec xyz --调用已存在的存储过程

insert into A select .....
update....
....

commit tran --提交事务,存储过程结束
GO
shift 2001-08-28
  • 打赏
  • 举报
回复
如在SQL Server中建立存储过程
Create Procedure GetEmployeeCount(存储名) @InJobID Int,@OutCount Int Out as Select @OutCount=Count(*) From Employee Where Job_id=@InJobID


shift 2001-08-28
  • 打赏
  • 举报
回复
存儲過程是一組預先定義並編譯好的 SQL 語句,可以接受參數,返回狀態值及參數值,並可以嵌套調用.還可作一種安全機製,使用戶通過它訪問未授權的表和視圖.

CREATE PROCEDURE
Create Procedure 過程名 (參數) As SQL語句
參數格式
@參數名稱 數據類型 [=缺省值] [OUTPUT]

數據類型不能為 Image類型.

Output 說明參數為一返回參數,其值可以返回給調用它的Execute語句中的變量.

通过存储过程存取数据库除了减少网络上的传输,更能提高执行速度.

34,588

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧