具有多行的SQL Insert语句

时间:2016-02-10 13:40:39

标签: sql sql-server

这看起来非常微不足道,但对于我的生活,我无法理解为什么这不起作用。任何想法都会很棒!

USE [ACCTQA]
GO

INSERT INTO [dbo].[glacct]
           ([Company]
           ,[Account]
           ,[Description])
     VALUES
 ('E096','191802','INVESTMENT IN SUBS')
,('E096','320007','COMMON STOCK')
,('E096','330013','SURPLUS')
,('E096','340003','RETAINED EARNINGS')
,('E096','690876','LIAB & CASUALTY INS')
,('E096','710000','BONDS - AGENCY BILL')
,('E096','710001','COMM. P&C - AGENCY BILL')
,('E096','710002','EMPL BENEFITS-AGENCY BILL')

错误味精:

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

3 个答案:

答案 0 :(得分:1)

如果您使用的是SQL Server 2005或更低版本,那么您的查询将无法运行。在那些版本上你必须尝试

INSERT INTO glacct
  ([Company],[Account],[Description]) 
SELECT 'E096','191802','INVESTMENT IN SUBS'
UNION ALL
SELECT 'E096','320007','COMMON STOCK'
UNION ALL
SELECT 'E096','330013','SURPLUS' 

您的查询将适用于SQL Server 2008及更高版本,。

<强> SQL FIDDLE DEMO

答案 1 :(得分:1)

试试这个

USE [ACCTQA]
GO

INSERT INTO [dbo].[glacct]
           ([Company]
           ,[Account]
           ,[Description])
SELECT * FROM (   
SELECT 'E096','191802','INVESTMENT IN SUBS' UNION ALL
SELECT 'E096','320007','COMMON STOCK' UNION ALL
SELECT 'E096','330013','SURPLUS' UNION ALL
SELECT 'E096','340003','RETAINED EARNINGS'
.....)

我猜你的版本不接受这样的多个值,所以你必须使用一个select语句(INSERT FROM SELECT)才能使它工作。

答案 2 :(得分:0)

它成功了......

CREATE TABLE #glacct (Company VARCHAR(100),
                        Account INT,
                    Description VARCHAR(200))

INSERT INTO [dbo].[#glacct]
           ([Company],
            [Account],
            [Description])
     VALUES
('E096','191802','INVESTMENT IN SUBS'),
('E096','320007','COMMON STOCK'),
('E096','330013','SURPLUS'),
('E096','340003','RETAINED EARNINGS'),
('E096','690876','LIAB & CASUALTY INS'),
('E096','710000','BONDS - AGENCY BILL'),
('E096','710001','COMM. P&C - AGENCY BILL'),
('E096','710002','EMPL BENEFITS-AGENCY BILL')

PS:完成SQLServer 2014 ...