存储过程

Jun 07, 2016 pm 03:58 PM
sql storage database program process connect

以前做《机房收费系统》时,和数据库连接的T—SQL程序都保存在本地,通过一段时间对数据库的学习后了解到数据库中的存储过程也可以完成相同的操作,且其创建的T—SQL程序保存在SQL Server中。它在服务器端对数据库中的数据进行处理,并将结果返回到客户端。

以前做《机房收费系统》时,和数据库连接的T—SQL程序都保存在本地,通过一段时间对数据库的学习后了解到数据库中的存储过程也可以完成相同的操作,且其创建的T—SQL程序保存在SQL Server中。它在服务器端对数据库中的数据进行处理,并将结果返回到客户端。这样就避免了从客户端多次连接并访问数据库的操作,从而提高客户端的工作效率。

1.存储过程的使用

存储过程是集中存储在SQLServer中的SQL语句和流程控制语句的预编译集合,用以实现某种任务(增删改查等)。任何一组Transact——SQL语句构成的代码块,都可以作为存储过程保存起来。

1)创建:

Create procedure存储过程名[;编号]

[{@参数数据类型}[varying][=默认值][output]][,…n]

With

{recompile|encryption|recompile,encryption}]

As

SQL语句[…n]

参数说明:

存储过程名对于数据库及其所有者必须唯一。创建局部临时存储过程时,可在存储过程名前加一个#号,创建全局临时存储过程则可加两个#,用以简单区分。

编号:可选整数,用来对同名的存储过程分组,以便用一条drop procedure语句即可将同组的存储过程一起删除

@参数:过程中的参数,每个过程的参数仅用于该过程本身。默认情况下,参数只能代替常量。

output:表明参数时返回值。使用output参数可将信息返回给调用过程。

recompile:表明不保存该存储过程的执行计划,该存储过程将在运行时重新编译。

encryption:对存储过程进行加密,即不能修改该存储过程中的SQL语句。

2)执行

使用excute语句执行存储过程,语法格式为:

[[exec[ute]]

{[@返回状态=]{存储过程名|@存储过程名变量}}

[[@参数名称=]{值|@变量[output]|[default]}]

[,…n]

[With recompile]

(Ps:SQL中的EXEC命令有两个用法:执行一个存储过程,或者执行一个动态批次。批次是一个内容为SQL语句的字符串)

3)查看和修改存储过程

比较简单一点的就是在企业管理器中对存储过程右击进行相应操作,再者就是用alter procedure语句修改和sp_rename进行重命名,删除时用dropprocedure语句即可

2.优缺点

在本文的开头已经提到存储过程的功能是可以由其它程序替代的,那么一个数据库是否有必要设计存储过程呢?

1)优点

a.安全性:可以防止SQL注入式攻击,可设定只有某些用户具有对存储过程的使用权

b.提高性能:存储过程只在创建时进行编译,一般SQL语句每执行一次就编译一次,所以存储过程允许程序更快地执行

c.减少网络流量:存储过程的代码直接存储在数据库中,可以减少SQL语句从客户端到服务器端的传输量

d.存储过程可以减少客户端代码的重复,且其可重复使用,从而减少数据库开发人员的工作量

2)缺点

a.移植问题:由于存储过程将应用程序绑定到了SQL Server,因此使用存储过程封装的业务逻辑将限制应用程序的可移植性。

b.重新编译问题:后端代码是运行前编译的,如果带有引用关系的对象发生改变,受影响的存储过程需要重新编译。

3.实例

以机房收费系统的数据库为例,在数据库表中查找指定的卡号并为该卡的金额增加10元。创建存储过程

	Create procedure addCash_proc		--创建一个存储过程,名为addCash_proc
		@CardNo integer			--声明一个参数
	with encryption				--对存储过程进行加密
	as
	begin
		--查找到相应卡号的金额并加上10
		update Student_Info set cash=cash+10 where CardNo =@CardNo 
	end
Copy after login

执行语句如下

	Exec addCash_proc @CardNo='2'
Copy after login

程序执行后卡号为2的金额增加10元。

小结:

存储过程非常便捷,一般熟悉了SQL语句就可以很容易的掌握存储过程的使用,但是如果大量使用可能会给系统维护带来麻烦。在实际应用中,有的人偏好于使用存储过程有的人则尽量避免,在此要考虑好是让数据库服务器负担更多的运算量还是交给客户端,权衡存储过程带来的利与弊再酌情使用。

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1670
14
PHP Tutorial
1274
29
C# Tutorial
1256
24
iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

Apple's latest releases of iOS18, iPadOS18 and macOS Sequoia systems have added an important feature to the Photos application, designed to help users easily recover photos and videos lost or damaged due to various reasons. The new feature introduces an album called "Recovered" in the Tools section of the Photos app that will automatically appear when a user has pictures or videos on their device that are not part of their photo library. The emergence of the "Recovered" album provides a solution for photos and videos lost due to database corruption, the camera application not saving to the photo library correctly, or a third-party application managing the photo library. Users only need a few simple steps

How to handle database connection errors in PHP How to handle database connection errors in PHP Jun 05, 2024 pm 02:16 PM

To handle database connection errors in PHP, you can use the following steps: Use mysqli_connect_errno() to obtain the error code. Use mysqli_connect_error() to get the error message. By capturing and logging these error messages, database connection issues can be easily identified and resolved, ensuring the smooth running of your application.

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

How to use MySQLi to establish a database connection in PHP: Include MySQLi extension (require_once) Create connection function (functionconnect_to_db) Call connection function ($conn=connect_to_db()) Execute query ($result=$conn->query()) Close connection ( $conn->close())

How to save JSON data to database in Golang? How to save JSON data to database in Golang? Jun 06, 2024 am 11:24 AM

JSON data can be saved into a MySQL database by using the gjson library or the json.Unmarshal function. The gjson library provides convenience methods to parse JSON fields, and the json.Unmarshal function requires a target type pointer to unmarshal JSON data. Both methods require preparing SQL statements and performing insert operations to persist the data into the database.

How to use database callback functions in Golang? How to use database callback functions in Golang? Jun 03, 2024 pm 02:20 PM

Using the database callback function in Golang can achieve: executing custom code after the specified database operation is completed. Add custom behavior through separate functions without writing additional code. Callback functions are available for insert, update, delete, and query operations. You must use the sql.Exec, sql.QueryRow, or sql.Query function to use the callback function.

MySQL: Simple Concepts for Easy Learning MySQL: Simple Concepts for Easy Learning Apr 10, 2025 am 09:29 AM

MySQL is an open source relational database management system. 1) Create database and tables: Use the CREATEDATABASE and CREATETABLE commands. 2) Basic operations: INSERT, UPDATE, DELETE and SELECT. 3) Advanced operations: JOIN, subquery and transaction processing. 4) Debugging skills: Check syntax, data type and permissions. 5) Optimization suggestions: Use indexes, avoid SELECT* and use transactions.

PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings PHP Database Connection Pitfalls: Avoid Common Mistakes and Misunderstandings Jun 05, 2024 pm 10:21 PM

To avoid PHP database connection errors, follow best practices: check for connection errors and match variable names with credentials. Use secure storage or environment variables to avoid hardcoding credentials. Close the connection after use to prevent SQL injection and use prepared statements or bound parameters.

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

See all articles