带有INSERT语句的WHERE子句,WHERE处的“不正确的语法”,以及IF NOT NOT EXISTS运行,无论是否存在记录

时间:2015-05-10 17:48:20

标签: sql sql-server-2008 where sql-insert

我有这个INSERT查询:

INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
VALUES (1, 11, 1, DATEADD(day, 1, GETDATE()))
WHERE NOT EXISTS 
    (SELECT * FROM Appointments 
     WHERE StaffID = 1 AND CustomerID = 11 AND TimeSlot = 1 AND AppDate = DATEADD(day, 1, GETDATE()));

然而它说“关键字'WHERE'附近的语法不正确”

这是非常基本的错误,但没有任何线索。

我也尝试过使用IF NOT EXISTS,它没有语法错误,但也不起作用,即使带有值的记录已经存在,也要执行插入操作:

IF NOT EXISTS 
    (
        SELECT * FROM Appointments 
        WHERE StaffID = 1 AND CustomerID = 11 AND TimeSlot = 1 AND AppDate = DATEADD(day, 1, GETDATE())
    )
    BEGIN
        INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
        VALUES (1, 11, 1, DATEADD(day, 1, GETDATE()))
END;

值为1,11,1和明天的记录已经存在100%,但再次插入。

2 个答案:

答案 0 :(得分:3)

您不能使用WHERE语句指定任何INSERT .. VALUES子句。但您可以使用INSERT .. SELECT语句执行此操作,如下所示。

也...

  

值为1,11,1和明天的记录已经存在100%,但又重新插入。

GETDATE()返回一个小数秒精度的时间戳,这在您的表中不太可能已经存在。您可能希望使用DATE数据类型,例如使用

DATEADD(day, 1, CAST(GETDATE() AS DATE))

INSERT .. SELECT

INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
SELECT 1, 11, 1, DATEADD(day, 1, CAST(GETDATE() AS DATE))
WHERE NOT EXISTS (
  SELECT * 
  FROM Appointments 
  WHERE StaffID = 1 
    AND CustomerID = 11 
    AND TimeSlot = 1 
    AND AppDate = DATEADD(day, 1, CAST(GETDATE() AS DATE))
);

INSERT .. SELECT与公用表表达式以避免重复

或许,避免多次计算GETDATE()

WITH ins (StaffID, CustomerID, TimeSlot, AppDate)
  AS (SELECT 1, 11, 1, DATEADD(day, 1, CAST(GETDATE() AS DATE)))
INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
SELECT ins.StaffID, ins.CustomerID, ins.TimeSlot, ins.AppDate
FROM ins
WHERE NOT EXISTS (
  SELECT * 
  FROM Appointments 
  WHERE StaffID = ins.StaffID 
    AND CustomerID = ins.CustomerID
    AND TimeSlot = ins.TimeSlot 
    AND AppDate = ins.AppDate
);

INSERT .. SELECT with set operations

或者,再次,更简洁地使用集合操作:

INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate)
SELECT 1, 11, 1, DATEADD(day, 1, CAST(GETDATE() AS DATE))
EXCEPT
SELECT StaffID, CustomerID, TimeSlot, AppDate
FROM Appointments

MERGE

实际上,整个“插入即使不存在”的概念也可以使用MERGE建模:

MERGE INTO Appointments a
USING (
  SELECT 
    1 StaffID, 
    11 CustomerID,
    1 TimeSlot,
    DATEADD(day, 1, CAST(GETDATE() AS DATE)) AppDate
) ins
ON (
  a.StaffID = ins.StaffID AND 
  a.CustomerID = ins.CustomerID AND 
  a.TimeSlot = ins.TimeSlot AND 
  a.AppDate = ins.AppDate
)
WHEN NOT MATCHED THEN INSERT (StaffID, CustomerID, TimeSlot, AppDate)
  VALUES (ins.StaffID, ins.CustomerID, ins.TimeSlot, ins.AppDate)

答案 1 :(得分:1)

在插入和写入查询时,您不应该涉及时间:

INSERT INTO Appointments (StaffID, CustomerID, TimeSlot, AppDate) 
select 1, 11, 1, DATEADD(day, 1, cast(GETDATE() as date))
WHERE NOT EXISTS 
(SELECT * 
 FROM Appointments 
 WHERE StaffID = 1 AND CustomerID = 11 AND TimeSlot = 1 
 AND AppDate = DATEADD(day, 1, cast(GETDATE() as date))
 )