2011-03-09 88 views
1

我需要將50多行插入到SQL Server 2008中,並且出現一個奇怪的錯誤。請幫忙!SQL SERVER 2008:嘗試使用1個SQL語句插入多行

表設計:

  • 名稱:mod_Facilities
    • faclityID,主鍵/索引
    • facilityName,爲nvarchar(4000)
    • facilityDescription,爲nvarchar(4000)
    • statusComment,nvarchar(4000)
    • isPublic,位
    • isActive,位
    • 請將isDeleted,位

錯誤:

Msg 102, Level 15, State 1, Line 3 Incorrect syntax near ','.

這裏是我的SQL語句

INSERT INTO mod_Facilites (facilityName,facilityDescription,isActive,isDeleted) 
VALUES 
('Conference Room Lower','Conference Room Lower – 25, (AV ready for meetings and info sessions)','true','false'), 
('Conference Room Upper','Conference Room Upper – 21, (AV ready for meetings and info sessions)','true','false'), 
('Meeting Room A','Meeting Room A – (upper theatre set up capacity 40) ','true','false'), 
('Meeting Room B','Meeting Room B – (AV ready classroom set up capacity 25) ','true','false'), 
('Meeting Rooms A & B','Meeting Rooms A & B – (AV ready capacity 80)','true','false'), 
('OP Resource Room','OP Resource Room','true','false'), 
('Climbing Wall','Climbing Wall','true','false'), 
('Bouldering Wall','Bouldering Wall','true','false'), 
('Entire Climbing Area','Entire Climbing Area','true','false'), 
('CPR/First Aid classroom','CPR/First Aid classroom','true','false'), 
('Lobby Area','Lobby Area','true','false'), 
('Studio 1','Studio 1 ','true','false'), 
('Studio 2','Studio 2','true','false'), 
('Studio 3','Studio 3','true','false'), 
('Studio 4','Studio 4','true','false'), 
('Mat Studio','Mat Studio','true','false'); 
+3

對於那些不熟悉,這是在SQL Server中有效語法2008年。但對於以前版本的SQL Server,您必須使用不同的方法。 – DOK 2011-03-09 17:50:08

+1

因爲你傳遞了var的isActive和isDeleted? – 2011-03-09 17:51:54

+0

如果你通常嘗試'('Mat Studio','Mat Studio',true,false)'(圍繞你的BIT值沒有單引號) - 這會改變什麼嗎? – 2011-03-09 18:03:36

回答

4

你標記這個問題爲SQL Server 2008,但是如果你在2005年或更早的時候嘗試過這種語法,那麼它就是你所看到的錯誤。

作爲替代方案,嘗試:

INSERT INTO mod_Facilites 
    (facilityName,facilityDescription,isActive,isDeleted) 
    SELECT 'Conference Room Lower','Conference Room Lower – 25, (AV ready for meetings and info sessions)','true','false' UNION ALL 
    SELECT 'Conference Room Upper','Conference Room Upper – 21, (AV ready for meetings and info sessions)','true','false' UNION ALL 
    SELECT 'Meeting Room A','Meeting Room A – (upper theatre set up capacity 40) ','true','false' UNION ALL 
    SELECT 'Meeting Room B','Meeting Room B – (AV ready classroom set up capacity 25) ','true','false' UNION ALL 
    SELECT 'Meeting Rooms A & B','Meeting Rooms A & B – (AV ready capacity 80)','true','false' UNION ALL 
    SELECT 'OP Resource Room','OP Resource Room','true','false' UNION ALL 
    SELECT 'Climbing Wall','Climbing Wall','true','false' UNION ALL 
    SELECT 'Bouldering Wall','Bouldering Wall','true','false' UNION ALL 
    SELECT 'Entire Climbing Area','Entire Climbing Area','true','false' UNION ALL 
    SELECT 'CPR/First Aid classroom','CPR/First Aid classroom','true','false' UNION ALL 
    SELECT 'Lobby Area','Lobby Area','true','false' UNION ALL 
    SELECT 'Studio 1','Studio 1 ','true','false' UNION ALL 
    SELECT 'Studio 2','Studio 2','true','false' UNION ALL 
    SELECT 'Studio 3','Studio 3','true','false' UNION ALL 
    SELECT 'Studio 4','Studio 4','true','false' UNION ALL 
    SELECT 'Mat Studio','Mat Studio','true','false'; 
+0

雖然這可能很好地解決了OP中的問題。我會更關心爲什麼MSSQL 2008不像MSSQL 2008那樣行事。 – Enull 2011-03-09 20:42:23

4

你在SQL 2008兼容模式下運行?

這會返回100還是小於100?如果低於100,那麼你沒有在SQL 2008的兼容性級別運行

SELECT compatibility_level 
FROM sys.databases 
WHERE database_id = DB_ID() 
+0

我該如何檢查? – 2011-03-09 17:47:08

+0

查看附加代碼。 – SQLMenace 2011-03-09 17:48:05

0

有它必須是一個陳述任何理由?我從來沒有見過這種INSERT語句的使用。我會使用多個語句或BULK INSERT或SSIS。

+0

你有一個批量插入的例子嗎? – 2011-03-09 17:52:47

+0

鏈接MSDN增加 - 這取決於你的數據源,以它是否是最好的BULK INSERT或SSIS但基本上BULK INSERT將文件的內容插入表。 SSIS可以貼任何東西,任何地方:) – 2011-03-09 17:55:30

0

處理這個最簡單的方法是改變值到選擇和UNION他們:

INSERT INTO mod_Facilites (facilityName,facilityDescription,isActive,isDeleted) 
SELECT 
    'Conference Room Lower', 'Conference Room Lower – 25, (AV ready for meetings and info sessions)','true','false' UNION ALL 
SELECT 
    'Conference Room Upper','Conference Room Upper – 21, (AV ready for meetings and info sessions)','true','false' UNION ALL 

...等