Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Stored Procedures and Parameters in SQL Server, Summaries of Property Law

An overview of Stored Procedures in SQL Server, including their definition, creation, execution, and modification. It also covers Stored Procedure parameters, their declaration, and usage in calling Stored Procedures. Examples are given to illustrate the concepts.

What you will learn

  • What are Stored Procedure parameters in SQL Server?
  • How do you create a Stored Procedure in SQL Server?
  • How do you pass parameters to a Stored Procedure in SQL Server?
  • What is a Stored Procedure in SQL Server?
  • How do you execute a Stored Procedure in SQL Server?

Typology: Summaries

2020/2021

Uploaded on 04/02/2022

ha-nguyen-ngoc-2
ha-nguyen-ngoc-2 🇻🇳

6 documents

1 / 25

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Session 23
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19

Partial preview of the text

Download Stored Procedures and Parameters in SQL Server and more Summaries Property Law in PDF only on Docsity!

  • Session

Content

  • (^) Stored Procedure
  • (^) Stored Procedure Parameters
  • (^) OUTPUT Parameter

Example

Tạo Stored Procedure

CREATE PROCEDURE procedure_name

AS

BEGIN

-- SQL statements

-- SELECT, INSERT, UPDATE, or DELETE statement

END

Trong đó:

Procedure_name: là tên của Stored Procedure.

Trong BEGIN – END là nội dung của Stored Procedure

Tạo Stored Procedure

  • (^) Ví d ụ:

SELECT name , price, quantity

FROM PRODUCTS

ORDER BY price DESC;

CREATE PROCEDURE productList

AS
BEGIN

SELECT name , price, quanti

ty

FROM PRODUCTS

ORDER BY price DESC;

END ;

Thay đổi Stored Procedure

  • (^) Cú pháp:

Đ ể thay đ ổi stored thì b n s ạ ử d ụng l nh ALTER PROCEDURE và tên c ệ ủa stored

sẽ n m phía sau.ằ

ALTER PROCEDURE productList

AS
BEGIN

SELECT name , price, quantity, status

FROM PRODUCTS

ORDER BY quantity;

END ;

Xóa Stored Procedure

  • (^) Cú pháp:

Đ ể xóa stored procedure thì b n sạ ử d ụng l nh DROP PROCEDURE ho c DROP ệ ặ

PROC.

DROP PROC sp_name;

DROP PROCEDURE sp_name;

Ho ặc

Stored Procedure Parameter

  • (^) Stored Procedure 1 tham s ố:

CREATE PROCEDURE procedure_name

(@parameter_name AS data_type)

AS

BEGIN

-- SQL statements

-- SELECT, INSERT, UPDATE, or DELETE statement

END

Chú ý:

Tham s ố sẽ n m trong c p dằ ặ ấu ngo c (). ặ

M ỗi tham s ố ph i khai báo tên và kiả ểu d ữ li u tệ ương ứng thông qua t ừ khóa AS.

@parameter_name AS data_type chính là tham s ố có tên@parameter_name và có ki ểu

dữ li uệ data_type.

Đ ể s ử d ụng tham s ố ở bên trong ph ần thân c ủa procedure thì ta ch c ỉ ần gõ tên c ủa nó là

đượ c @parameter_name.

Stored Procedure Parameter

  • (^) Stored Procedure 1 tham s ố:

CREATE PROCEDURE findProdByPrice

(@minPrice AS DECIMAL)

AS
BEGIN

SELECT name , price, quantity

FROM PRODUCTS

W HERE price >= @minPrice

ORDER BY price DESC;

END ;

Ví d ụ: Vi ết th ủ t ục x ử lý vi c lệ ấy

các s n phả ẩm có giá l n h ớ ơn m tộ

giá tr ị nào đó. Giá tr ị này được

truy ền vào lúc th ực thi th ủ ụt c.

Stored Procedure Parameter

  • (^) Stored Procedure nhi ều tham s ố:

CREATE PROCEDURE procedure_name

(

@parameter_name_1 AS data_type,

@parameter_name_n AS data_type

)

AS

BEGIN

-- SQL statements

-- SELECT, INSERT, UPDATE, or DELETE statement

END

Chú ý:

Các tham s ố cách nhau b ởi d ấu “,”

Stored Procedure Parameter

  • (^) Stored Procedure nhi ều tham s ố:

CREATE PROCEDURE findProdByPrice

@minPrice AS DECIMAL,

@maxPrice AS DECIMAL

AS
BEGIN

SELECT name , price, quantity

FROM PRODUCTS
W HERE

price >= @minPrice

AND

price <= @maxPrice

ORDER BY price DESC;

END ;
Ví d ụ: Vi ết th ủ t ục x ử lý vi c lệ ấy các s n ả
ph ẩm có giá n ằm trong m ột kho ảng giá tr ị
nào đó. Giá tr ị max, min này được truy ền
vào lúc th ực thi th ủ ụt c.

Stored Procedure Parameter

  • (^) Stored Procedure nhi ều tham s ố:
Th ực thi Stored Procedure nhi ều tham s ố:

EXECUTE procedure_name

@parameter_name_1 = parameter_value_1,

@parameter_name_n = parameter_value_n;

Cách truy ền tham s ốtrên không

t ường minh, ph i nhả ớ thứ tự

các tham số

Cách truy ền tham s ốtrên không

t ường minh, ph i nhả ớ thứ tự

các tham số

Truy ền tên tham s ố ươt ng ứng

v ới giá tr luôn ị

Không c ần quan tâm đ ến th ứ ựt

c ủa chúng

Stored Procedure Parameter

  • (^) Stored Procedure v ới tham s ố m c đ nh :ặ ị

CREATE PROCEDURE procedure_name

(

@parameter_name_1 AS data_type,

@parameter_name_n AS data_type,

@parameter_name_default AS data_type = default_value

)

AS

BEGIN

-- SQL statements

-- SELECT, INSERT, UPDATE, or DELETE statement

END

Stored Procedure Parameter

  • (^) Stored Procedure nhi ều tham s ố:
Th ực thi Stored Procedure nhi ều tham s ố:

EXECUTE findProduct

@productName = ‘iphone’;

Chú ý:

@minPrice và @maxPrice sẽ l ấy giá tr m c đ nh là 0 và 9999 khi không đ ị ặ ị ược truy ền

vào.

OUTPUT

Parameter