Introduction and improvement of stored procedures

Source: Internet
Author: User
To use the usual or very complex work, which is written in a SQL statement and stored with a specified name, you can automatically complete the command by simply calling execute when you want the database to provide the same functionality as the defined stored procedure.

What is a stored procedure?

Defined:

To use the usual or very complex work, which is written in a SQL statement and stored with a specified name, you can automatically complete the command by simply calling execute when you want the database to provide the same functionality as the defined stored procedure.

Here, someone might ask: so the stored procedure is just a bunch of SQL statements?

Why do Microsoft companies add this technology?

So what's the difference between a stored procedure and a normal SQL statement?

Advantages of stored procedures:

1. Stored procedures are compiled only when created, and each time a stored procedure is executed without recompiling, and the general SQL statements are compiled every time, so using stored procedures can improve database execution speed.

2. When complex operations are performed on the database (for example, when multiple tables are update,insert,query,delete), this complex operation can be encapsulated in a stored procedure and used in conjunction with transaction processing provided by the database.

3. Stored procedures can be reused to reduce the workload of database developers

4. High security, can be set only a certain user to have the right to use the specified stored procedures

Types of stored procedures:

1. System stored procedures: to sp_ the beginning of the system for the various settings. Get information. Related Management work,

such as sp_help is to obtain the relevant information of the specified object

2. Extended stored procedures begin with XP_ to invoke the functionality provided by the operating system

EXEC master.. xp_cmdshell ' Ping 10.8.16.1 '

3. User-defined stored procedures, which we refer to as stored procedures

Common format

Create procedure Procedue_name

[@parameter Data_type] [Output]

[With] {Recompileencryption}

As

Sql_statement

Explain:

Output: Indicates that this parameter is a can be returned

with {recompileencryption}

Recompile: means recompiling every time this stored procedure is executed

Encryption: The contents of the stored procedure created are encrypted

Such as:

The contents of the table book are as follows

Numbered title price

001 C Language Introduction $

002 PowerBuilder Report Development $52

Instance 1: Stored procedures for querying the contents of a table book

Create proc Query_book

As

SELECT * FROM book

Go

EXEC Query_book

Example 2: Add a record to the table book and query the total amount of all books in this table

Create proc Insert_book

@param1 Char, @param2 varchar, @param3 money, @param4 money output

With encryption---------encryption

As

Insert Book (number, title, price) Values (@param1, @param2, @param3)
Select @param4 =sum (price) from book
Go

Example of execution:
DECLARE @total_price Money
EXEC insert_book ' 003 ', ' Delphi Control Development Guide ', $, @total_price
print ' Total amount is ' +convert (varchar, @total_price)
Go
3 Kinds of return values for stored procedures:
1. Returns an integer with return
2. Returns parameters in output format
3.Recordset
The difference between return values:
Both output and return can be received in a batch program and the recordset is returned to the client of the execution batch

Example 3: With two tables for product,order, the table reads as follows:
Product
Product number Product name Customer order number
001 Pen 30
002 Brush 50
003 Pencil 100
Order
Product number Customer Name Customer deposit
001 Nanshan District, $
002 Luohu District $
003 Baoan District $
Implement a numbered join condition that joins two tables into a temporary table containing only numbers. Product name. Customer Name. Deposit. Total amount,
Total Amount = deposit * number, temporary table placed in stored procedure

The code is as follows:
Create proc Temp_sale
As
Select a. Product number, a. Product name, B. Customer's name, B. Customer's deposit, A. Customer order * B. Customer deposit as total amount
into #temptable from product a INNER join order B on a. Product number =b. Product number
If @ @error =0
print ' good '
Else
print ' Fail '
Go



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.