A few simple examples of SQL stored procedures

Source: Internet
Author: User

reading: SQL storage is an important part of the database operation process, for some beginners is also more abstract difficult to understand, this article I will use a few examples to parse the database of SQL stored procedures, so that the abstract things visualized, more easily understood. Example 1:create proc proc_stu @sname varchar ( -), @pwd varchar ( -)  as Select* fromrenwhere[email protected] and pwd=@pwd Go View results: Proc_stu'Admin','Admin'Example 2: The following stored procedure implements user-authenticated functionality, returns 0 if unsuccessful, and returns 1 if successful. CREATE PROCEDURE VALIDATE @USERNAME CHAR ( -), @PASSWORD CHAR ( -), @LEGAL BIT outputas IF EXISTS (SELECT* from REN WHERE SNAME = @USERNAME and PWD =@PASSWORD) SELECT @LEGAL=1ELSE SELECT @LEGAL=0calls the stored procedure in the program and determines whether the user is legitimate based on the value of the @legal parameter. Example 3: An efficient data paging stored procedure can easily cope with millions of data CREATE PROCEDURE pagetest--test for page flipping--The sort field needs to be placed in the first column (@FirstID nvarchar ( -)=NULL, --The value of the sort field for the first record in the current page @LastID nvarchar ( -)=NULL, --The value of the sort field for the last record in the current page @isNext bit=NULL, --true 1: Next page;false 0: prev @allCountintOutput,--returns the total number of records @pageSizeintOutput,--returns the number of records on a page @CurPageint--page number (first page)0: first page;-1 last page. ) asif@CurPage =0--represents the first page begin--total number of statistics recordsSelect@allCount =count (ProductId) fromproduct_testSet@pageSize =Ten--return data on the first pageSelectTopTenProductId, ProductName, Introduction fromproduct_test ORDER by ProductId endElse if@CurPage =-1--represents the last pageSelect* from (SelectTopTenProductId, ProductName, Introduction fromProduct_test ORDER BY ProductId Desc) asAA ORDER BY ProductIdElsebeginif@isNext =1--turn to the next pageSelectTopTenProductId, ProductName, Introduction fromProduct_testwhereProductId >@LastID ORDER BY ProductIdElse--turn to the previous pageSelect* from (SelectTopTenProductId, ProductName, Introduction fromProduct_testwhereProductId < @FirstID ORDER BY ProductId Desc) asBB ORDER by ProductId end

A few simple examples of SQL stored procedures

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.