我有一张带有BookingIDs的表格 我有一个FormatIDs的表格 我有一张包含货件的表格
这里有DLL来构建它们,如果有人想测试
CREATE TABLE PI_Booking (
ID int NULL,
UID nvarchar(50) NULL,
BookingID nvarchar(20) NULL,
DayUsed date NULL,
PI_Format nvarchar(10) NULL
)
INSERT INTO PI_Booking (ID,UID, BookingID,DayUsed,PI_Format)
VALUES(5,'test','123456','2018-06-06','2')
VALUES(6,'test','123457',NULL,NULL)
VALUES(7,'test','123458',NULL,NULL)
VALUES(8,'test','123459',NULL,NULL)
VALUES(9,'test','123460',NULL,NULL)
-
CREATE TABLE dbo.PI_Formats (
PI_Format nvarchar(1) NULL,
PI_Code nvarchar(1) NULL,
RegNo nvarchar(20) NULL
)
INSERT INTO PI_Formats (PI_Format, PI_Code, RegNo)
VALUES('1','S','DCXX001')
VALUES('2','M','DCXX002')
VALUES('3','L','DCXX003')
VALUES('4','E','DCXX004')
-
CREATE TABLE dbo.Shipments(
ID int NULL,
SStatus nvarchar(50) NULL,
PI_Format nvarchar(1) NULL,
BookingID nvarchar(20) NULL,
AutNo nvarchar(10) NULL
)
INSERT INTO Shipments (ID,SStatus, PI_Format)
VALUES(35,'Departure','1')
VALUES(36,'Departure','1')
VALUES(39,'Departure','2')
VALUES(40,'Hold','2')
VALUES(41,'Departure','2')
VALUES(43,'Hold','1')
VALUES(46,'Departure','1')
VALUES(48,'Departure','2')
VALUES(51,'Hold','3')
VALUES(52,'Departure','4')
VALUES(54,'Departure','1')
我必须在出发状态中将PI_Booking.BookingID
和PI_Formats.AutNo
分配给货件
但是BookingID
必须每天更改,并且每个FormatID
从发货中我们可以看到今天的出货与出发'状态有PI_Format 1,2和4
我的想法是将上述三个值1,2,4放入临时表中,并使用如下查询:
insert into @TempTable (PI_Format)
select distinct PI_Format
FROM Shipments
WHERE SStatus='Departure'
然后必须将日期和PI_Format分配给PI_Booking表:
我想知道的事情:
if not exists
(select 1 from PI_Booking
where DayUsed =cast(getUTCDATE() as date)
and PI_Format=@TempTable.PI_Format)
BEGIN
update PI_Booking.DayUsed=GETUTCDATE(), PI_Format=@TempTable.PI_Format
...to the first unused record, thus where ID=5
END
然后是所有(3)PI_Format值 因此PI_Booking Table at应该如下:
| ID | UID | BookingID | DayUsed | PI_Format |
| 5 | test | 123456 | 2018-06-06 | 2 |
| 6 | test | 123457 | 2018-06-07 | 1 |
| 7 | test | 123458 | 2018-06-07 | 2 |
| 8 | test | 123459 | 2018-06-07 | 4 |
| 9 | test | 123460 | NULL | NULL |
但我不知道如何编写这一步,我认为应该有更优雅的方法来解决这个问题。
然后分配PI_Booking.BookingID和PI_Formats.AutNo似乎很容易: 我可以使用PI_Format和DayUsed作为连接字段
加入3个表可以建议一个解决方案,或者至少是一条路径?
由于