zoukankan      html  css  js  c++  java
  • 数据库—存储过程。

    存储过程:

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

    存储过程的建立:

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

    SET ANSI_NULLS ON
    GO
    SET QUOTED_IDENTIFIER ON
    GO
    -- =============================================
    -- Author:		<Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:	<Description,,>
    -- =============================================
    CREATE PROCEDURE <Procedure_Name, sysname, ProcedureName> 
    	-- Add the parameters for the stored procedure here
    	<@Param1, sysname, @p1> <Datatype_For_Param1, , int> = <Default_Value_For_Param1, , 0>, 
    	<@Param2, sysname, @p2> <Datatype_For_Param2, , int> = <Default_Value_For_Param2, , 0>
    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:		<Author,,Name>
    -- Create date: <Create Date,,>
    -- Description:	<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.降低网络流量。

    存储过程位于server上,调用的时候仅仅须要传递存储过程的名称以及參数就能够了,因此降低了网络传输的数据量。

    4.安全性。參数化的存储过程能够防止SQL注入式攻击,并且能够将Grant、Deny以及Revoke权限应用于存储过程。
    详见百度百科

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



  • 相关阅读:
    将Nginx添加到windows服务中
    springboot使用redis管理session
    GIT常用命令
    阻止360、谷歌浏览器表单自动填充
    谈谈对Spring IOC的理解
    同一个Nginx服务器同一端口配置多个代理服务
    LeetCode 653. Two Sum IV
    109. Convert Sorted List to Binary Search Tree(根据有序链表构造平衡的二叉查找树)
    108. Convert Sorted Array to Binary Search Tree(从有序数组中构造平衡的BST)
    LeetCode 236. Lowest Common Ancestor of a Binary Tree(二叉树求两点LCA)
  • 原文地址:https://www.cnblogs.com/jzssuanfa/p/7223021.html
Copyright © 2011-2022 走看看