2017-02-15 29 views
1

架構我試圖執行以下查詢,但它總是會報告錯誤

Tenants (HouseID, ApartmentNumber, LeaseTenantSSN, LeaseStartDate(not null), LeaseExpirationDate(can be null), 
Rent, LastRentPaidDate, RentOverdue) 
NewRentPayments (HouseID, ApartmentNumber, LeaseTenantSSN(not null), Rent(can be null), DatePaid) 

查詢

INSERT INTO Tenants (x.HouseID,x.ApartmentNumber,x.LeaseTenantSSN,CURRENT_DATE,NULL,x.Rent,x.Rent,FALSE) 
SELECT x.HouseID, x.ApartmentNumber,x.LeaseTenantSSN,CURRENT_DATE,NULL,x.Rent,x.Rent     
FROM NewRentPayments x 
WHERE x.HouseID NOT IN (select HouseID 
       FROM Tenants) 
     OR x.ApartmentNumber NOT IN (SELECT ApartmentNumber 
        FROM Tenants 
        WHERE Tenants.HouseID=x.HouseID); 

我總是在CURRENT_DATE錯誤,NULL.It說在sintax錯誤或在CURRENT_DATE附近

回答

2

您的語法關閉。以下INSERT INTO應該是您計劃插入數據的列名列表,使用以下SELECT作爲源。像這樣的東西應該工作:

INSERT INTO Tenants (HouseID, ApartmentNumber, LeaseTenantSSN, LeaseStartDate, 
        LeaseExpirationDate, Rent, LastRentPaidDate, RentOverdue) 
SELECT x.HouseID, x.ApartmentNumber, x.LeaseTenantSSN, CURRENT_DATE, NULL, x.Rent, 
     x.DatePaid, FALSE 
FROM NewRentPayments x 
WHERE x.HouseID NOT IN (SELECT HouseID FROM Tenants) OR 
     x.ApartmentNumber NOT IN (SELECT ApartmentNumber FROM Tenants 
           WHERE Tenants.HouseID=x.HouseID); 

我選擇FALSERentOverdue價值,這似乎與你的邏輯一致。

+0

謝謝,把x.DatePaid放在你的問號上。 – daniel

2

INSERT INTO要求在表名之後的給定數據必須插入的列的名稱。顯然,CURRENT_DATENULL不是列的名稱。

相關問題