機房重構——預存程序,重構預存程序

來源:互聯網
上載者:User

機房重構——預存程序,重構預存程序

    原來只是見過預存程序這幾個字,知道有這麼一個東西,但並未真正揭開它的面紗去真正瞭解它,但是在敲機房的時候就遇到了難題,要修改資料庫中多張表的資料,使用SQL語句的話會很複雜,究竟什麼可以讓我們的效率提高呢?預存程序可以做到,這就迫使我不得不去再次觸碰預存程序。

    定義:在大型資料庫中,一組完成特定功能SQL語句的集合。相當於就是將原本需要再D層多次寫的SQL語句,在資料庫中寫到了一塊。

    何時才會用到預存程序,我們從定義中就能看出預存程序是SQL語句的集合,當我們使用的SQL語句複雜時,使用預存程序能夠簡化我們的工作。對於複雜程度我們要有很好的估量,適時的使用預存程序,並不是所有的都用到預存程序就非常好。

    下面我就以註冊卡為例來說明一下如何使用預存程序:

USE /****** Object:  StoredProcedure [dbo].[PROC_Register]    Script Date: 03/15/2015 15:32:58 ******/SET ANSI_NULLS ONGOSET QUOTED_IDENTIFIER ONGO-- =============================================-- Author:<sisi>-- Create date: <2015-3-9 12:02:28>-- Description:<註冊>-- =============================================ALTER PROCEDURE [dbo].[PROC_Register]-- Add the parameters for the stored procedure here@cardID char(10),@studentID char(10),@account char(10),@cardType char(10),@usingState char(10),@remark varchar(50),@registrationDate char(10),@registrationTime char(10),@UserID char(10),@studentName char(10),@sex char(10),@department char(10),@grade char(10),@studentclass char(10),@RechargeDate char(10),@RechargeTime char(10),@RechargeMoney decimal(18,0)ASBEGIN--添加卡表insert into T_CardInfo (cardID ,studentID ,account,cardType,usingState ,remark,registrationDate ,registrationTime ,UserID ) values (@cardID,@studentID,@account,@cardType,@usingState ,@remark,@registrationDate ,@registrationTime ,@UserID )--新增學生表insert into T_StudentInfo (studentID,studentName ,sex,department ,grade,studentclass )values (@studentID,@studentName,@sex,@department ,@grade,@studentclass )--添加到儲值記錄表insert into T_RechargeRecord (cardID,RechargeDate ,RechargeTime ,RechargeMoney ,UserID )values (@cardID,@RechargeDate,@RechargeTime ,@RechargeMoney,@UserID )END
     看到預存程序,這另我想到我們經常用到的函數,函數也能夠實現某種功能,它們二者有何區別呢?

        

    通過上面的對比可能對預存程序有了更深的理解,函數我們已經學過並且經常使用,通過將預存程序與函數聯想到一起,這樣就不會再懼怕它了,我們並不是沒有學過,只是可能還沒有發現自己已經見過,要從已有的知識中發掘陌生事物的原型。


相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.