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 (20),
@pwd varchar (20)
As
select * from ren where [email protected] and [email protected]
Go

View results: Proc_stu ' admin ', ' admin '

Example 2:

The following stored procedure implements user-authenticated functionality, and if unsuccessful, returns 0, and succeeds returns 1.

CREATE PROCEDURE VALIDATE @USERNAME Char, @PASSWORD char, @LEGAL BIT OUTPUT
As

IF EXISTS (SELECT * from REN WHERE SNAME = @USERNAME and PWD = @PASSWORD)
SELECT @LEGAL = 1
ELSE
SELECT @LEGAL = 0

Calls 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 that can easily cope with millions of data

CREATE PROCEDURE pagetest--Test for page flipping
--you need to put the sort field 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
@allCount int output,--Returns the total number of records
@pageSize int output,--Returns the number of records on a page
@CurPage INT--page number (p. 0): first page;-1 last page.
)

As

If @CurPage =0--represents the first page
Begin
--Total records of statistics
Select @allCount =count (ProductId) from Product_test

Set @pageSize =10
--Return the data on the first page
Select Top 10
ProductId,
ProductName,
Introduction
From Product_test ORDER by ProductId
End

else if @CurPage =-1--represents the last page

SELECT * FROM
(select Top Ten ProductId,
ProductName,
Introduction

From Product_test ORDER BY ProductId Desc) as AA
ORDER BY ProductId
Else

Begin
If @isNext =1
--turn to the next page
Select Top Ten ProductId,
ProductName,
Introduction
From Product_test where ProductId > @LastID ORDER by ProductId
Else
--turn to the previous page
SELECT * FROM
(select Top Ten ProductId,
ProductName,
Introduction
From Product_test where ProductId < @FirstID ORDER BY ProductId desc) as BB 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.