我订购系统食品
我想这样做,当菜肴的所有状态都是" COMPLETE"时,订单会自动更改为完成
我有4张桌子
1订购
ID_ORDER:OR00001
状态:正在处理/自动更改"完成"
CREATION_DATE:17/07/2017
3 DISH
1 DISH PASTA
ID:PA00001
NAME:PASTA MIDELA
CHEF:JEFF
ID_ORDER:OR00001
状态:正在处理
2 DISH SALAD
ID:SA00001
姓名:饮食沙拉
CHEF:ASTON
ID_ORDER:OR00001
状态:正在处理
3 DISH MEAT
ID:ME00001
NAME:MEAT BBF
CHEF:JEFF
ID_ORDER:OR00001
状态:正在处理
怎么可能?谢谢
CREATE TABLE [dbo].[ORDER](
[ID_OR] [int] IDENTITY(1,1) NOT NULL,
[ID_ORDER] AS ('OR'+right('00000'+CONVERT([varchar],[ID_OR],(0)),(5))),
[STATUS] [datetime] NOT NULL,
[CREATION_DATE] [datetime] NOT NULL,
CONSTRAINT [PK_RR] PRIMARY KEY CLUSTERED
(
[ID_ORDER] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
CREATE TABLE [dbo].[1_DISH](
[ID_D1] [int] IDENTITY(1,1) NOT NULL,
[ID_PASTA] AS ('PA'+right('00000'+CONVERT([varchar],[ID_D1],(0)),(5))),
[NAME] [varchar](20),
[CHEF] [varchar](20),
[STATUS] [datetime] NOT NULL,
[ID_ORDER] [varchar](20)
CONSTRAINT [PK_RR] PRIMARY KEY CLUSTERED
(
[ID_PASTA] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
CREATE TABLE [dbo].[2_DISH](
[ID_D2] [int] IDENTITY(1,1) NOT NULL,
[ID_SALAD] AS ('SA'+right('00000'+CONVERT([varchar],[ID_D2],(0)),(5))),
[NAME] [varchar](20),
[CHEF] [varchar](20),
[STATUS] [datetime] NOT NULL,
[ID_ORDER] [varchar](20)
CONSTRAINT [PK_RR] PRIMARY KEY CLUSTERED
(
[ID_SALAD] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
CREATE TABLE [dbo].[3_DISH](
[ID_D3] [int] IDENTITY(1,1) NOT NULL,
[ID_MEAT] AS ('ME'+right('00000'+CONVERT([varchar],[ID_D3],(0)),(5))),
[NAME] [varchar](20),
[CHEF] [varchar](20),
[STATUS] [datetime] NOT NULL,
[ID_ORDER] [varchar](20)
CONSTRAINT [PK_RR] PRIMARY KEY CLUSTERED
(
[ID_MEAT] ASC
)WITH (IGNORE_DUP_KEY = OFF) ON [PRIMARY]
) ON [PRIMARY]
END
GO
答案 0 :(得分:1)
这里有一些可能对您有帮助的示例代码。
创建了两个表格:一个用于订单,另一个用于构成订单的菜单。
我在Dish表中添加了 DishType 列。
我将状态更改为varchar
数据类型。
declare @ORDERTABLE table
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[STATUS] [varchar](20) NOT NULL,
[CREATION_DATE] [datetime] NOT NULL
)
declare @DISHTABLE table
(
[ID] [int] IDENTITY(1,1) NOT NULL,
[DISHTYPE] varchar(20),
[NAME] [varchar](20),
[CHEF] [varchar](20),
[STATUS] [varchar](20) NOT NULL,
[ID_ORDER] int
)
插入一些示例数据。
insert @ORDERTABLE (STATUS, CREATION_DATE) values
('IN PROCESS', getdate()),
('IN PROCESS', getdate());
insert @DISHTABLE (DISHTYPE, NAME, CHEF, STATUS, ID_ORDER) values
('PASTA','PASTA MIDELA','JEFF','IN PROCESS',1),
('SALAD','DIET SALAD','KIA','IN PROCESS',1),
('MEAT','MEAT BBF','BART','IN PROCESS',1),
('SALAD','CAESAR SALAD','KIA','IN PROCESS',2),
('MEAT','ROAST CHICKEN','BART','IN PROCESS',2);
update @DISHTABLE set STATUS = 'COMPLETE' where ID = 1;
update @DISHTABLE set STATUS = 'COMPLETE' where ID = 2;
update @DISHTABLE set STATUS = 'COMPLETE' where ID = 3;
select * from @DISHTABLE;
自动更改订单状态的简单方法:每当您更改菜肴的状态时,请检查订单中的菜肴数量与完成的菜肴数量。如果所有菜肴都已完成,那么也要更改订单状态。
declare @ORDERID int = 1;
update @ORDERTABLE
set STATUS = 'COMPLETE'
where
ID = @ORDERID
and (select count(*) from @DISHTABLE where ID_ORDER = @ORDERID) = (select count(*) from @DISHTABLE where ID_ORDER = @ORDERID and STATUS = 'COMPLETE');
select
o.ID,
DISHES = (select count(*) from @DISHTABLE where ID_ORDER = o.ID),
COMPLETED = (select count(*) from @DISHTABLE where ID_ORDER = o.ID and STATUS = 'COMPLETE'),
o.STATUS
from @ORDERTABLE o;