

















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
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
Typology: Summaries
1 / 25
This page cannot be seen from the preview
Don't miss anything!
Trong đó:
Procedure_name: là tên của Stored Procedure.
Trong BEGIN – END là nội dung của Stored Procedure
SELECT name , price, quantity
ORDER BY price DESC;
CREATE PROCEDURE productList
SELECT name , price, quanti
ty
ORDER BY price DESC;
ALTER PROCEDURE productList
SELECT name , price, quantity, status
ORDER BY quantity;
DROP PROC sp_name;
DROP PROCEDURE sp_name;
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.
CREATE PROCEDURE findProdByPrice
(@minPrice AS DECIMAL)
SELECT name , price, quantity
W HERE price >= @minPrice
ORDER BY price DESC;
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 “,”
CREATE PROCEDURE findProdByPrice
@minPrice AS DECIMAL,
@maxPrice AS DECIMAL
SELECT name , price, quantity
price >= @minPrice
price <= @maxPrice
ORDER BY price DESC;
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
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
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.