设为首页 加入收藏

TOP

数据库―存储过程。
2014-11-24 07:55:55 来源: 作者: 【 】 浏览:2
Tags:数据库 存储 过程

存储过程:

存储过程(Stored Procedure)是在大型数据库系统中,一组为了完成特定功能的SQL 语句集,经编译后存储在数据库中,用户通过指定存储过程的名字并给出参数(如果该存储过程带有参数)来执行它。存储过程是数据库中的一个重要对象,任何一个设计良好的数据库应用程序都应该用到存储过程。

存储过程的建立:

\

选中存储过程,右击——新建存储过程,则出现下面的代码。

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		
-- Create date: 
-- Description:	
-- =============================================
CREATE PROCEDURE  
	-- Add the parameters for the stored procedure here
	<@Param1, sysname, @p1>  = , 
	<@Param2, sysname, @p2>  = 
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	SELECT <@Param1, sysname, @p1>, <@Param2, sysname, @p2>
END
GO
建好的存储过程:
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author:		
-- Create date: 
-- Description:	
-- =============================================
ALTER PROCEDURE [dbo].[PROC_SettleAccount]
	-- Add the parameters for the stored procedure here
	@Recharge numeric(18,2) ,@ReturnM numeric(18,2),
	@Income numeric(18,2),@UserName char(10),
	@SetDate char(10),@SetTime char(10)
AS
BEGIN
	-- SET NOCOUNT ON added to prevent extra result sets from
	-- interfering with SELECT statements.
	SET NOCOUNT ON;

    -- Insert statements for procedure here
	insert into Bill (Recharge ,ReturnM ,Income ,UserName ,SetDate ,SetTime )
	values (@Recharge ,@ReturnM ,@Income ,@UserName ,@SetDate ,@SetTime )
	update RechargeRecords set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账'
	update ReturnMoneyRecords  set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账'
	update Cards  set SettleAccountState ='已结账' where UserName =@UserName and SettleAccountState ='未结账'
	
END
存储过程的使用:
  Public Function SettleAccount(bill As Entity.Bill) As Boolean Implements IDAL.IBill.SettleAccount
        Dim cmdtext As String
        cmdtext = "PROC_SettleAccount" '用存储过程的名称来替换SQL语句
        bill.SetDate = Format(Now, "yyyy-MM-dd") '获得当前日期
        bill.SetTime = Format(Now, "HH:mm:ss") '获得当前时间
        '添加参数
        Dim sqlparameter As SqlParameter() = {New SqlParameter("@Recharge", bill.Recharge),
                                             New SqlParameter("@ReturnM", bill.ReturnM),
                                             New SqlParameter("@Income", bill.Income),
                                             New SqlParameter("@UserName", bill.UserName),
                                             New SqlParameter("@SetDate", bill.SetDate),
                                             New SqlParameter("@SetTime", bill.SetTime)}
        Dim helper As New SqlHelper
        Dim flag As Boolean
        '中间的参数变为存储过程特用的参数
        flag = helper.ExecAddDelUpdate(cmdtext, CommandType.StoredProcedure, sqlparameter)
        Return flag
    End Function
只要将存储过程的名字替换SQL的语句。在执行的时候,也要换成存储过程特用的参数。

存储过程的优点:

1.重复使用。存储过程可以重复使用,从而可以减少数据库开发人员的工作量。 2.提高性能。存储过程在创建的时候在进行了编译,将来使用的时候不再重新翻译。一般的SQL语句每执行一次就需要编译一次,所以使用存储过程提高了效率。 3.减少网络流量。存储过程位于服务器上,调用的时候只需要传递存储过程的名称以及参数就可以了,因此降低了网络传输的数据量。 4.安全性。参数化的存储过程可以防止SQL注入式攻击,而且可以将Grant、Deny以及Revoke权限应用于存储过程。详见百度百科

总结:使用存储过程,在一定程度上减少了代码量,又尝试使用不曾用过得东西,会有成就感。

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇android内置数据库的简单实现 下一篇机房收费系统之―组合查询

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Python爬虫教程(从 (2025-12-26 16:49:14)
·【全269集】B站最详 (2025-12-26 16:49:11)
·Python爬虫详解:原 (2025-12-26 16:49:09)
·Spring Boot Java: (2025-12-26 16:20:19)
·Spring BootでHello (2025-12-26 16:20:15)