温馨提示×

sql存储过程几个简单例子

sql
小云
94
2023-09-22 06:50:21
栏目: 云计算

  1. 创建一个简单的存储过程,返回所有顾客的数量:
CREATE PROCEDURE GetCustomerCount
AS
BEGIN
SELECT COUNT(*) AS CustomerCount FROM Customers;
END
  1. 创建一个存储过程,根据给定的国家参数返回该国家的所有顾客:
CREATE PROCEDURE GetCustomersByCountry
@Country NVARCHAR(50)
AS
BEGIN
SELECT * FROM Customers WHERE Country = @Country;
END
  1. 创建一个存储过程,将给定的订单号参数作为输入,并返回该订单的详细信息:
CREATE PROCEDURE GetOrderDetails
@OrderID INT
AS
BEGIN
SELECT * FROM Orders WHERE OrderID = @OrderID;
END
  1. 创建一个存储过程,将给定的产品类别参数作为输入,并返回该类别中所有产品的数量和平均价格:
CREATE PROCEDURE GetProductStats
@CategoryID INT
AS
BEGIN
SELECT COUNT(*) AS ProductCount, AVG(Price) AS AveragePrice
FROM Products
WHERE CategoryID = @CategoryID;
END

0