Hướng dẫn Toàn Diện về Thủ Tục Lưu Trữ trong SQL Server

Hướng dẫn Toàn Diện về Thủ Tục Lưu Trữ trong SQL Server

Khám phá hướng dẫn chi tiết về thủ tục lưu trữ (stored procedure) trong SQL Server, bao gồm cách tạo, sử dụng, sửa đổi và xóa các thủ tục lưu trữ hiệu quả.
15/03/2024
13,281 Lượt xem

Hướng dẫn về thủ tục lưu trữ (Stored Procedure) trong SQL Server

Trong thế giới phát triển ứng dụng cơ sở dữ liệu, thủ tục lưu trữ (stored procedure) đóng một vai trò quan trọng trong việc tối ưu hóa hiệu suất, đảm bảo tính nhất quán và quản lý logic nghiệp vụ một cách hiệu quả. Với #sql server, bạn có thể tận dụng sức mạnh của các thủ tục lưu trữ để tạo ra các ứng dụng cơ sở dữ liệu mạnh mẽ và hiệu quả. Trong bài viết này, chúng ta sẽ khám phá sâu hơn về khái niệm thủ tục lưu trữ trong SQL Server và cách sử dụng chúng một cách hiệu quả.

Giới thiệu về thủ tục lưu trữ (Stored Procedure)

Thủ tục lưu trữ là một nhóm các câu lệnh SQL được lưu trữ và đặt tên trong cơ sở dữ liệu. Chúng có thể chứa bất kỳ câu lệnh SQL hợp lệ nào, bao gồm các câu lệnh SELECT, INSERT, UPDATE, DELETE, và nhiều hơn nữa. Khi thực thi, thủ tục lưu trữ có thể nhận đầu vào từ các tham số và trả về kết quả hoặc giá trị.

Lợi ích của việc sử dụng thủ tục lưu trữ

  • Tối ưu hóa hiệu suất: Thủ tục lưu trữ được biên dịch và lưu trữ trong cơ sở dữ liệu, giúp giảm thời gian truy xuất và tăng tốc độ xử lý.
  • Đảm bảo tính nhất quán: Bằng cách tập trung logic nghiệp vụ vào các thủ tục lưu trữ, bạn có thể đảm bảo tính nhất quán của dữ liệu và quy trình xử lý.
  • Quản lý logic nghiệp vụ: Thủ tục lưu trữ cho phép bạn tách biệt logic nghiệp vụ khỏi ứng dụng, tăng tính modular và dễ bảo trì hơn.
  • Bảo mật: Bạn có thể kiểm soát quyền truy cập vào các thủ tục lưu trữ, đảm bảo rằng chỉ có người dùng được phép mới có thể thực thi các thao tác nhất định.

Tạo thủ tục lưu trữ trong SQL Server

Để tạo một thủ tục lưu trữ trong SQL Server, bạn sử dụng câu lệnh CREATE PROCEDURE. Cú pháp cơ bản như sau:

CREATE PROCEDURE [schema_name.][procedure_name] [@parameter_name data_type = default_value, ...] AS BEGIN -- Procedure body END 

Ví dụ, để tạo một thủ tục lưu trữ đơn giản in ra "Hello, World!":

CREATE PROCEDURE hello AS BEGIN PRINT 'Hello, World!' END 

Bạn có thể thực thi thủ tục lưu trữ này bằng cách sử dụng câu lệnh EXEC hoặc EXECUTE:

EXEC hello 

Sử dụng tham số trong thủ tục lưu trữ

Thủ tục lưu trữ có thể nhận đầu vào từ các tham số, cho phép bạn truyền dữ liệu động vào thủ tục. Ví dụ, chúng ta có thể tạo một thủ tục lưu trữ để tính tổng của hai số:

CREATE PROCEDURE sum_numbers @num1 INT, @num2 INT AS BEGIN DECLARE @result INT SET @result = @num1 + @num2 PRINT CONCAT('Tổng của ', @num1, ' và ', @num2, ' là: ', @result) END 

Để gọi thủ tục lưu trữ này, bạn cần truyền giá trị cho các tham số:

EXEC sum_numbers 5, 10 

Kết quả sẽ là: "Tổng của 5 và 10 là: 15"

Sửa đổi và xóa thủ tục lưu trữ

Sửa đổi thủ tục lưu trữ

Để sửa đổi một thủ tục lưu trữ đã tồn tại, bạn sử dụng câu lệnh ALTER PROCEDURE. Cú pháp tương tự như CREATE PROCEDURE, nhưng bạn cần đặt tên thủ tục lưu trữ hiện có:

ALTER PROCEDURE [schema_name.][procedure_name] [@parameter_name data_type = default_value, ...] AS BEGIN -- Procedure body END 

Xóa thủ tục lưu trữ

Để xóa một thủ tục lưu trữ, bạn sử dụng câu lệnh DROP PROCEDURE:

DROP PROCEDURE [schema_name.][procedure_name] 

Lưu ý rằng sau khi xóa, bạn sẽ không thể khôi phục lại thủ tục lưu trữ đó nếu không lưu trữ nó ở nơi khác.

Kết luận

Thủ tục lưu trữ là một công cụ hữu ích trong SQL Server, cho phép bạn tối ưu hóa hiệu suất, đảm bảo tính nhất quán và quản lý logic nghiệp vụ một cách hiệu quả. Bằng cách tận dụng sức mạnh của #stored procedure, bạn có thể tạo ra các ứng dụng cơ sở dữ liệu mạnh mẽ và hiệu quả. Hãy thực hành với các ví dụ trong bài viết này và khám phá thêm các tính năng nâng cao của thủ tục lưu trữ trong SQL Server để tối ưu hóa ứng dụng của bạn.

Các bạn có thể tham khảo thêm nguồn khác:

SQL Server Stored Procedures Tutorial

This tutorial series introduces you to the stored procedures and shows you how to develop flexible stored procedures to optimize database access. Section 1. Getting started with SQL Server Stored Proc>

Create a Stored Procedure - SQL Server | Microsoft Learn

Nov 18, 2022To run the procedure, in Object Explorer, right-click the stored procedure name HumanResources.uspGetEmployeesTest and select Execute Stored Procedure. In the Execute Procedure window, ent>

SQL Stored Procedures for SQL Server - W3Schools

What is a Stored Procedure? A stored procedure is a prepared SQL code that you can save, so the code can be reused over and over again. So if you have an SQL query that you write over and over again,>

Stored Procedures (Database Engine) - SQL Server | Microsoft Learn

Nov 18, 2022Stored procedures Create Modify Delete Execute Specify parameters Grant permissions Parameters Properties Return Data Recompile Rename View definition View dependence OLE Automation Object>

Execute a Stored Procedure - SQL Server | Microsoft Learn

Nov 18, 2022Execute a stored procedure In Object Explorer, connect to an instance of the SQL Server Database Engine, expand that instance, and then expand Databases. Expand the database that you want,>

SQL Server stored procedures for beginners - SQL Shack

In this article, we will learn how to create stored procedures in SQL Server with different examples. SQL Server stored procedure is a batch of statements grouped as a logical unit and stored in the d>

SQL Server Stored Procedures: Create, Alter, Rename, Execute

In SQL Server, a stored procedure is a set of T-SQL statements which is compiled and stored in the database. The stored procedure accepts input and output parameters, executes the SQL statements, and>

Stored Procedure in SQL Server - Dot Net Tutorials

Let's see the different ways to execute the above SQL Server Stored Procedure with the output parameter. Step1: First declare a variable of the same data type as that of the output parameter. Here we>

SQL Server Stored Procedures: The Ultimate Guide for Beginners

Feb 21, 2021What is a Stored Procedure? A stored procedure is a database object used to store complex SQL code that can be ran easily and repeatedly using simple commands. They can take parameters as>

Tutorial: Signing Stored Procedures with a Certificate - SQL Server ...

Nov 18, 2022SQL Tutorials Database Engine Tutorial: Signing Stored Procedures with a Certificate Article 11/18/2022 6 minutes to read 11 contributors Feedback In this article Prerequisites 1. Configur>

SQL Server Stored Procedure Tutorial - mssqltips.com

Apr 11, 2022SQL Server Stored Procedure Tutorial By: Greg Robidoux Overview A stored procedure is nothing more than prepared SQL code that you save so you can reuse the code over and over again.>

SQL Server Programming Part 1 - Stored Procedure Basics

By Andrew Gould https://www.wiseowl.co.uk - If you ever find yourself writing the same SQL query over and over again, a Stored Procedure could be just the time-saving tool you're looking for....>

SQL Server Stored Procedures Introduction - Tutorial Gateway

To execute Stored Procedure using the Management Studio (SSMS), Please navigate to the Programmability -> SP. Next, select the one you want to execute (SelectingAllRecordsInEmployee), and right-click>

SQL Stored Procedures (With Examples) - Programiz

SQL Stored Procedures In this tutorial, we'll learn about the stored procedures in SQL and how to use them with examples. In SQL, stored procedure is a set of statement (s) that perform some defined a>

T-SQL - Stored Procedures - tutorialspoint.com

The MS SQL Server Stored procedure is used to save time to write code again and again by storing the same in database and also get the required output by passing parameters. Syntax Following is the ba>

SQL Server Stored Procedure with Parameters - mssqltips.com

Create or Alter SQL Server Stored Procedure. In SQL Server 2016 and later there is the ability to either CREATE a new stored procedure if it does not already exist or ALTER the procedure if it does ex>

31. STORED PROCEDURES in SQL Server | Parameters | Create & Delete Demo ...

T-SQL (Structured Query Language) has been the primary language for accessing, storing, retrieving, and manipulating data in databases governed by Microsoft ...>

Advanced Stored Procedure Examples in SQL Server (17 Examples)

Jun 11, 2021Advanced SQL Server Stored Procedure Examples. Here is a list of advanced SQL server stored procedure examples.. 1. Stored Procedure to insert data. In this section, we will understand how>

SQL Stored Procedure Tutorial - c-sharpcorner.com

SQL stored procedure creates a Common Language Runtime (CLR) stored procedure in SQL Server, Azure SQL database. Stored procedures are similar to procedures in other programming languages in that they>

SQL Server stored procedure parameters (Complete tutorial)

Jul 28, 2021In SQL Server stored procedures, you can also pass table data to the input parameters and perform various operations on it. You will learn in this section how you can pass the table data t>

R tutorial: Run predictions in SQL stored procedures

Nov 18, 2022In this article. Applies to: SQL Server 2016 (13.x) and later Azure SQL Managed Instance In part five of this five-part tutorial series, you'll learn to operationalize the model that you t>

Stored Procedure in SQL Server - javatpoint

If we are using the SSMS, use the following steps for creating the stored procedure: Step 1: Select the Database -> Programmability -> Stored Procedures. Step 2: Right-click on the Stored Procedures f>

SQL Server Tutorial - The Practical SQL Server Tutorial

Our SQL Server tutorials are practical and include numerous hands-on activities. After completing the entire tutorials, you will be able to: Query data efficiently from tables in the SQL Server databa>

Get Free SQL Tips - SQL Server Stored Procedure Tutorial

Excellent tutorial for beginners... Really good tutorial. I will recommend it to others, for a quick start with stored procedures. Harish, take a look at this tip to load data into SQL Server. I am lo>

MySQL Stored Procedure Tutorial

Listing stored procedures - provide you with some useful commands to list stored procedures from databases. Section 2. Conditional Statements IF statement - show you how to use the IF THEN statement i>

Creating a simple SQL Server stored procedure - mssqltips.com

So the simple T-SQL code excuting in the AdventureWorks database would be as follows which will return all rows from this table. To create a stored procedure to do this the code would look like this:>

SQL Stored Procedures - W3Schools

The following SQL statement creates a stored procedure that selects Customers from a particular City from the "Customers" table: Example. CREATE PROCEDURE SelectAllCustomers @City nvarchar (30) AS. SE>


Tags:
SHARED HOSTING
70% OFF
$2.99/mo $0.90/mo
SHOP NOW
RESELLER HOSTING
25% OFF
$12.99/mo $9.74/mo
SHOP NOW