Hướng dẫn viết store procedure trong sql năm 2024

Hãy nâng cấp lên Microsoft Edge để tận dụng các tính năng mới nhất, bản cập nhật bảo mật và hỗ trợ kỹ thuật.

Create a stored procedure

  • Bài viết
  • 07/18/2023

Trong bài viết này

Applies to:

This article describes how to create a Transact-SQL stored procedure by using SQL Server Management Studio and by using the Transact-SQL CREATE PROCEDURE statement.

Permissions

Requires CREATE PROCEDURE permission in the database and ALTER permission on the schema in which the procedure is being created.

How to create a stored procedure

You can use one of the following:

Use SQL Server Management Studio

To create a procedure in Object Explorer

  1. In Object Explorer, connect to an instance of Database Engine and then expand that instance.
  2. Expand Databases, expand the AdventureWorks2022 database, and then expand Programmability.
  3. Right-click Stored Procedures, and then select New Stored Procedure.
  4. On the Query menu, select Specify Values for Template Parameters.
  5. In the Specify Values for Template Parameters dialog box, enter the following values for the parameters shown. Parameter Value Author Your name Create Date Today's date Description Returns employee data. Procedure_name HumanResources.uspGetEmployeesTest @Param1 @LastName @Datatype_For_Param1 nvarchar(50) Default_Value_For_Param1 NULL @Param2 @FirstName @Datatype_For_Param2 nvarchar(50) Default_Value_For_Param2 NULL
  6. Select OK.
  7. In the Query Editor, replace the SELECT statement with the following statement: SELECT FirstName, LastName, Department FROM HumanResources.vEmployeeDepartmentHistory WHERE FirstName = @FirstName AND LastName = @LastName
    AND EndDate IS NULL;  
    
  8. To test the syntax, on the Query menu, select Parse. If an error message is returned, compare the statements with the previous information and correct as needed.
  9. To create the procedure, from the Query menu, select Execute. The procedure is created as an object in the database.
  10. To see the procedure listed in Object Explorer, right-click Stored Procedures and select Refresh.
  11. To run the procedure, in Object Explorer, right-click the stored procedure name HumanResources.uspGetEmployeesTest and select Execute Stored Procedure.
  12. In the Execute Procedure window, enter Margheim as the value for the parameter @LastName and enter the value Diane as the value for the parameter @FirstName.

Warning

Validate all user input. Do not concatenate user input before you validate it. Never execute a command constructed from unvalidated user input.

Use Transact-SQL

To create a procedure in Query Editor

  1. In Object Explorer, connect to an instance of Database Engine.
  2. From the File menu, select New Query.
  3. Copy and paste the following example into the query window and select Execute. This example creates the same stored procedure as in the previous example, but using a different procedure name.

    USE AdventureWorks2022; GO CREATE PROCEDURE HumanResources.uspGetEmployeesTest2

    @LastName nvarchar(50),  
    @FirstName nvarchar(50)  
    
    AS
    SET NOCOUNT ON;  
    SELECT FirstName, LastName, Department  
    FROM HumanResources.vEmployeeDepartmentHistory  
    WHERE FirstName = @FirstName AND LastName = @LastName  
    AND EndDate IS NULL;  
    
    GO

To run the procedure, copy and paste the following example into a new query window and select Execute. Notice that different methods of specifying the parameter values are shown.

Một stored procedure là một mã SQL đã được chuẩn bị sẵn mà bạn có thể lưu, vì thế mã này có thể được sử dụng lại nhiều lần.

Nếu bạn có một truy vấn SQL được viết lặp lại nhiều lần, hãy lưu nó là một stored procedure và sau đó chỉ cần gọi nó để thực thi.

Bạn cũng có thể chuyển các tham số cho một stored procedure, để stored procedure có thể hoạt động dựa trên các giá trị tham số được truyền vào.

1. Cú pháp của Stored Procedure

CREATE PROCEDURE tên procedure

AS

Câu lệnh SQL

GO;

Câu lệnh thực thi một Stored Procedure:

EXEC tên procedure;

2. Ví dụ về Stored Procedure

Để thực hiện các ví dụ này, bạn cần nhập cơ sở dữ liệu mẫu Northwind vào SQL Server Management Studio. Hãy xem bài viết hướng dẫn ngay dưới đây:

Tham khảo: Hướng dẫn tải và cài đặt dữ liệu mẫu Northwind trong SQL Server Management Studio

Câu lệnh SQL dưới đây tạo ra một stored procedure có tên là “SelectAllCustomers” để chọn tất cả bản ghi từ bảng Customers:

CREATE PROCEDURE SelectAllCustomers

AS

SELECT * FROM Customers

GO;

Câu lệnh thực thi procedure ở trên là:

EXEC SelectAllCustomers;

Bằng cách sử dụng stored procedure các bạn có thể tiết kiệm số dòng lệnh cần phải viết khi có những dòng lệnh lặp lại nhiều lần. Đây là tính năng rất hữu ích khi phải làm việc với các cơ sở dữ liệu lớn và có nhiều dòng lệnh. Trên đây là những gì bạn cần biết về stored procedure và cách sử dụng, chúng ta sẽ ứng dụng trong các ví dụ thực tiễn khi phân tích trong các bài viết sau, đừng quên đón đọc tại BAC's Blog.

Nguồn tham khảo: https://www.w3schools.com/

Nhu cầu đào tạo doanh nghiệp

BAC là đơn vị đào tạo BA đầu tiên tại Việt Nam. Đối tác chính thức của IIBA quốc tế. Ngoài các khóa học public, BAC còn có các khóa học in house dành riêng cho từng doanh nghiệp. Chương trình được thiết kế riêng theo yêu cầu của doanh nghiệp, giúp doanh nghiệp giải quyết những khó khăn và tư vấn phát triển.

Tham khảo chương trình đào tạo:

BAC - Biên soạn và tổng hợp nội dung

Click để đọc tiếp

  1. Hướng dẫn viết store procedure trong sql năm 2024
    Hướng dẫn tạo Button Slicer mới cập nhật trong Power BI 2023
    Button Slicer mới là một trong những cập nhật của Power BI những tháng cuối năm 2023. Mang đến nhiều thay đổi so với Tile Slicer cũ không chỉ ở tính năng mà còn là thiết kế. Bài viết này sẽ hướng dẫn chi tiết về cách sử dụng Button Slicer.
  2. Hướng dẫn viết store procedure trong sql năm 2024
  3. Hướng dẫn viết store procedure trong sql năm 2024
    Trello và Slack khác biệt và hỗ trợ nhau như thế nào?
    Trello và Slack là hai ứng dụng được sử dụng nhiều nhất cho mục đích quản lý dự án. Cả hai đều cung cấp những tính năng tốt nhất với chi phí cạnh tranh. Vì thế, việc lựa chọn một trong hai công cụ này có thể là vấn đề phải cân nhắc.
    Hướng dẫn viết store procedure trong sql năm 2024
5 công cụ AI CRM tốt nhất cho năm 2024

Là sự kết hợp giữa công nghệ AI tiên tiến và CRM truyền thống, những phần mềm này sẽ mang đến sự hỗ trợ đắc lực cho doanh nghiệp. Bài viết này sẽ tổng hợp 5 công cụ AI CRM được đánh giá là tốt nhất trong năm 2024 để giúp các bạn lựa chọn.