我又是新来的。我再次说,因为我的第一个SQL入门是在1992年的AS400上。
我意识到有一些类似的帖子。我试图从每个人的想法中解决我的问题,但很少没有成功。这是我想要做的。有3个表。两个源表和一个目标表。源表是Tickets和TicketEntities,目标表是DelTimes。
Tickets
有列:
(Id (primary), TicketNumber, LastUpdateTime and LastOrderDate, DepartmentId)
TicketEntities
有列:
(Ticket_Id, EntityTypeId, EntityName)
目标表DelTimes
具有:
(TicketId, TicketNo, TicketDate, LOTime, LOMinutes, DriverNam)
lastOrderDate
和LastUpdateTime
是日期和时间的组合,我必须从使用转化中提取信息。
到目前为止,这是错误的多部分标识符问题。
IF EXISTS (SELECT * FROM Tickets, TicketEntities Where Tickets.DepartmentId = 2 and EntityTypeId = 3)
UPDATE DelTimes
SET TicketId =Tickets.Id,
DelTimes.TicketNo = Tickets.TicketNumber,
DelTimes.TicketDate = CONVERT(Date,Tickets.LastOrderDate,5),
DelTimes.LOTime = Convert(datetime,Tickets.LastOrderDate,8),
DelTimes.LOMinutes = DATEDIFF(minute,Tickets.LastOrderDate,Tickets.LastUpdateTime),
DelTimes.DriverNam = TicketEntities.EntityName
ELSE
INSERT INTO DelTimes (TicketId, TicketNo, TicketDate, LOTime, LOMinutes, DriverNam)
VALUES (Id, TicketNumber, CONVERT(Date,LastOrderDate,5),
Convert(datetime,LastOrderDate,8),
DATEDIFF(minute,LastOrderDate,LastUpdateTime),
EntityName)
感谢您解决此问题的帮助!
答案 0 :(得分:0)
不是说这是答案,因为你没有变量,表之间的连接或WHERE子句。但是对于刚刚开始的人来说,它可能最终会看起来像:
UPDATE DelTimes
SET DelTimes.TicketNo = Tickets.TicketNumber,
DelTimes.TicketDate = CONVERT(Date,Tickets.LastOrderDate,5),
DelTimes.LOTime = Convert(datetime,Tickets.LastOrderDate,8),
DelTimes.LOMinutes = DATEDIFF(minute,Tickets.LastOrderDate,
Tickets.LastUpdateTime),
DelTimes.DriverNam = @DriverName
FROM DelTimes
JOIN Tickets ON DelTimes.TicketId = Tickets.TicketId
JOIN TicketEntities ON TicketEntities.Ticket_Id = Tickets.TicketId
WHERE DelTimes.TicketId = @TicketId
AND TicketEntities.EntityTypeId = 3
IF @@ROWCOUNT = 0
INSERT INTO DelTimes (TicketId, TicketNo, TicketDate, LOTime, LOMinutes, DriverNam)
VALUES (@TicketId, @TicketNumber, CONVERT(Date,@LastOrderDate,5),
Convert(datetime,@LastOrderDate,8),
DATEDIFF(minute,@LastOrderDate,@LastUpdateTime),
@DriverName)
我可能在这里弊大于利,而且我们不喜欢这个,但这会让你开始。 (到下一轮问题。)