sql存儲(chǔ)過(guò)程幾個(gè)簡(jiǎn)單例子

sql
小云
94
2023-09-22 06:50:21
欄目: 云計(jì)算

  1. 創(chuàng)建一個(gè)簡(jiǎn)單的存儲(chǔ)過(guò)程,返回所有顧客的數(shù)量:
CREATE PROCEDURE GetCustomerCount
AS
BEGIN
SELECT COUNT(*) AS CustomerCount FROM Customers;
END
  1. 創(chuàng)建一個(gè)存儲(chǔ)過(guò)程,根據(jù)給定的國(guó)家參數(shù)返回該國(guó)家的所有顧客:
CREATE PROCEDURE GetCustomersByCountry
@Country NVARCHAR(50)
AS
BEGIN
SELECT * FROM Customers WHERE Country = @Country;
END
  1. 創(chuàng)建一個(gè)存儲(chǔ)過(guò)程,將給定的訂單號(hào)參數(shù)作為輸入,并返回該訂單的詳細(xì)信息:
CREATE PROCEDURE GetOrderDetails
@OrderID INT
AS
BEGIN
SELECT * FROM Orders WHERE OrderID = @OrderID;
END
  1. 創(chuàng)建一個(gè)存儲(chǔ)過(guò)程,將給定的產(chǎn)品類別參數(shù)作為輸入,并返回該類別中所有產(chǎn)品的數(shù)量和平均價(jià)格:
CREATE PROCEDURE GetProductStats
@CategoryID INT
AS
BEGIN
SELECT COUNT(*) AS ProductCount, AVG(Price) AS AveragePrice
FROM Products
WHERE CategoryID = @CategoryID;
END

0