2014-07-21 26 views
0

我有一個場景,我需要通過檢查記錄是否存在插入多個記錄。每次啓動服務器時,它都會運行一個包含truncate table語句的sql script.sql文件),以便刪除以前的行。有不失以前的記錄運行腳本的同時,並沒有在腳本中使用truncate語句插入的方式插入多個記錄如果不存在

INSERT INTO employeedetails (EmpNo, FirstName, LastName,DateOfBirth, EmailId, PANNo, PFNumber, BankName, JoiningDate, 
    Designation, Location, LOPDays, EmployeeStatus) VALUES 
    (1,'A','S','1965-04-14','[email protected]','ADJP','KN/43708/01','XYZ','2007-10-15','Director','PQR','NIL',1), 
    (2,'B','N','1971-01-23','r[email protected]','AAGP','KN/43708/02','XYZ','2007-11-28','Director','PQR','NIL',1), 
    (3,'C','S','1982-10-05','[email protected]','AIBP','KN/43708/03','XYZ','2008-03-17','SA','PQR','NIL',1), 
    (7,'D','M','1978-02-17','[email protected]','AHRP','KN/43708/07','XYZ','2008-07-14','C T O','PQR','NIL',1), 
    (8,'E','D','1983-05-12','[email protected]','BUMP','KN/43708/08','XYZ','2008-05-05','TL','PQR','NIL',1); 

回答

1

你可以嘗試使用INSERT忽略

INSERT忽略INTO employeedetails (EmpNo,名字, 姓氏,DateOfBirth,EmailId,PANNo,PFNumber,BankName,JoiningDate, 名稱,位置,LOPDays,EmployeeStatus)值 (1,'A','S','1965-04-14', '[email protected]', 'ADJP', 'KN/43708/01', 'XYZ','2007-10-15 ','導演','PQR','NIL',1), (2,'B','N','1971-01-23','[email protected]','AAGP',' KN/43708/02','XYZ','2007-11-28','Director','PQR','NIL',1), (3,'C','S','1982-10 -05' , '[email protected]', 'AIBP', 'KN/43708/03', 'XYZ', '2008-03-17', 'SA', 'PQR', 'NIL',1) , (7,'D','M','1978-02-17','[email protected]','AHRP','KN/43708/07','XYZ','2008-07- ('8','E','D','1983-05-12','[email protected]','''''''''''') BUMP」, 'KN/43708/08', 'XYZ', '2008-05-05', 'TL', 'PQR', 'NIL',1);

編輯:它的工作更新了H2 database到最新版本時,和使用

對重複密鑰更新。示例代碼:

INSERT INTO employeedetails (EmpNo, FirstName, LastName,DateOfBirth, EmailId, PANNo, PFNumber, BankName, 
JoiningDate,Designation, Location, LOPDays, EmployeeStatus, on_duplicate_update_count) VALUES 
(1,'A','S','1965-04-14','[email protected]','ADJP','KN/43708/01','XYZ','2007-10-15','Director','PQR','NIL',1,0), 
(2,'B','N','1971-01-23','[email protected]','AAGP','KN/43708/02','XYZ','2007-11-28','Director','PQR','NIL',1,0), 
(3,'C','S','1982-10-05','[email protected]','AIBP','KN/43708/03','XYZ','2008-03-17','SA','PQR','NIL',1,0), 
(7,'D','M','1978-02-17','[email protected]','AHRP','KN/43708/07','XYZ','2008-07-14','C T O','PQR','NIL',1,0), 
(8,'E','D','1983-05-12','[email protected]','BUMP','KN/43708/08','XYZ','2008-05-05','TL','PQR','NIL',1,0) 
ON DUPLICATE KEY UPDATE on_duplicate_update_count=on_duplicate_update_count+1; 

http://dev.mysql.com/doc/refman/5.6/en/insert.html
OR
INSERT ...對重複密鑰更新
http://dev.mysql.com/doc/refman/5.1/en/insert-on-duplicate.html

+0

了異常'造成的:org.h2.jdbc.JdbcSQLException:語法錯誤SQL語句「INSERT IGNORE [*] INTO EMPLOYEEDETAILS' – SparkOn

+0

我猜忽略不適用於H2數據庫的情況 – SparkOn

+0

最新版本的H2支持'ON DUPLICATE KEY UPDATE'syn稅,但不是'IGNORE'語法。 –

相關問題