1.建一张客户表,包含客户名称,联系人,电话号码,公司地址.
2.建一张订单表,包含订单编号,客户ID,供应商ID,订单金额,客户支付状态,供应商支付状态,订单日期.
3.建一张供应商表,包含供应商名称,联系人,电话号,供应商地址
View Code
1 create table customer 2 ( 3 customerID int primary key identity(1,1) , 4 customerName varchar(50), 5 cuslinkMan varchar(50), 6 customerPhone varchar(12), 7 companyAdress varchar(100) 8 ) 9 10 create table supplier 11 ( 12 supplierID INT primary key identity(1,1) , 13 supplierName varchar(50), 14 supLinkeMan varchar(50), 15 supAdress varchar(100) 16 ) 17 18 create table orderForm 19 ( 20 orderID int identity(1,1), 21 orderNum varchar(50), 22 customerID int, 23 supplierID int, 24 orderMoney decimal , 25 custState int, 26 orderState int , 27 orderTime datetime, 28 constraint pk_order primary key(orderID) , 29 constraint fk_supplier foreign key(supplierID) references supplier(supplierID), 30 constraint fk_customer foreign key(customerID) references customer(customerID) 31 ) 32 33 insert into customer(customerName,cuslinkMan,customerPhone,companyAdress) values('太阳','刘敏','18762676815','无锡西区') 34 35 insert into dbo.supplier(supplierName,supLinkeMan,supAdress) 36 values('安泰','李丹','大连') 37 38 39 insert into dbo.orderForm(orderNum,customerID,supplierID,orderMoney,custState,orderState,orderTime) 40 values('0001','1','1','10000','0','0','2012.12.21')
练习一:写存储过程,根据订单日期,客户,供应商来查询订单,包含客户,供应商名称,订单详细信息,订单日期可以为空,客户跟供应商可以全选.
View Code
1 SET ANSI_NULLS ON 2 GO 3 SET QUOTED_IDENTIFIER ON 4 GO 5 6 create PROCEDURE 订单查询 7 8 @订单日期 datetime, 9 @客户 varchar(50), 10 @供应商 varchar(50) 11 AS 12 BEGIN 13 SET NOCOUNT ON; 14 15 16 SELECT orderNum, C.customerName,S.supplierName ,orderMoney,custState ,orderState ,orderTime 17 from dbo.orderForm O,dbo.customer C,dbo.supplier S 18 where 19 C.customerID=O.customerID 20 and S.supplierID=O.supplierID 21 --判断O.orderTime 22 and 23 ( 24 25 @订单日期 ='' 26 or @订单日期 is null or O.orderTime = @订单日期 27 ) 28 and --判断客户 29 ( 30 @客户='-1' or C.customerName=@客户 31 ) 32 and--判断供应商 33 ( 34 @供应商='-1' or S.supplierName=@供应商 35 ) 36 37 END 38 GO 39 40 EXEC 订单查询 '2012-12-21','星星','上海'
练习二:写存储过程,查询订单数最多的前3个客户的订单信息。
方法一:
ALTER PROCEDURE [dbo].[订单总数前三]
AS
BEGIN
SELECT O.*
from dbo.orderForm O
where
O.customerID in
(
SELECT top 3 O.customerID -- ,COUNT(O.orderNum) as 'aaa'
from dbo.orderForm O
group by O.customerID
order by COUNT(O.orderNum) DESC
)
END
ALTER PROCEDURE [dbo].[订单总数前三]
AS
BEGIN
SELECT O.*
from dbo.orderForm O
where
O.customerID in
(
SELECT top 3 O.customerID -- ,COUNT(O.orderNum) as 'aaa'
from dbo.orderForm O
group by O.customerID
order by COUNT(O.orderNum) DESC
)
END
方法二:
CREATE PROCEDURE 订单前三
AS
BEGIN
select O.* from orderForm O
join
(
select
top 3 customerID
from(
select customerID ,COUNT(orderNum) AS EE FROM dbo.orderForm GROUP BY customerID
) as TT order by EE desc
)as KK
on O.customerID = KK.customerID
END
GO
CREATE PROCEDURE 订单前三
AS
BEGIN
select O.* from orderForm O
join
(
select
top 3 customerID
from(
select customerID ,COUNT(orderNum) AS EE FROM dbo.orderForm GROUP BY customerID
) as TT order by EE desc
)as KK
on O.customerID = KK.customerID
END
GO
练习三:写存储过程,查询订单金额最多的前3个客户的订单信息。
View Code
USE [test] GO /****** Object: StoredProcedure [dbo].[订单金额前三] Script Date: 12/26/2012 09:30:19 ******/ SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GOALTER PROCEDURE [dbo].[订单金额前三]AS BEGINSET NOCOUNT ON;-- Insert statements for procedure hereSELECT O.orderID, C.customerID ,customerName,S.supplierName ,orderMoney,custState ,orderState ,orderTime from dbo.orderForm O,dbo.customer C,dbo.supplier S where C.customerID=O.customerID and S.supplierID=O.supplierID and O.customerID in (SELECT top 3 O.customerID --, sum(O.orderMoney)AS 订单总金额 from dbo.orderForm O group by O.customerIDorder by sum(O.orderMoney) desc) END
练习四:写sql,删除3条供应商数据,并且相应订单的供应商ID也置为null(这里应该是更新操作)
View Code
SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GOCREATE PROCEDURE 删除供应商信息 -- Add the parameters for the stored procedure here@供应商1ID int,@供应商2ID int,@供应商3ID int AS BEGIN-- SET NOCOUNT ON added to prevent extra result sets from-- interfering with SELECT statements.SET NOCOUNT ON;-- Insert statements for procedure hereupdate dbo.orderFormset supplierID=null where supplierID=@供应商1IDor supplierID=@供应商2IDor supplierID=@供应商3IDdelete from dbo.supplier where supplierID=@供应商1IDor supplierID=@供应商2IDor supplierID=@供应商3IDEND GOexec 删除供应商信息 '2','3','4'
练习五:查询不存在供应ID的订单信息,用(not exits)
CREATE PROCEDURE 查询无供应商的订单信息AS BEGINSET NOCOUNT ON;-- Insert statements for procedure hereSELECT * from orderForm A where not EXISTS (select supplierID from dbo.supplier B where B.supplierID=A.supplierID) END GOexec 查询无供应商的订单信息