2014-01-16 128 views
0

我使用EXCEL宏/ vba将xls表中的数据插入到SQL数据库中。插入A db时语法不正确

任何人都可以解释为什么我收到此错误信息......

附近有语法错误“OPEN”

我运行的代码是一个INSERT语句

Dim insert As String 

insert = "INSERT INTO dbo.HEADER 
    (plant, 
    taskno, 
    tskstatus, 
    category, 
    opened, 
    openedby, 
    title) 
VALUES 
    ('UK', 
    (select Max(taskno) AS count from HEADER)+ 1), 
    'open', 
    'RFSA', 
    CONVERT(datetime,'" & strdate & "',103), 
    '" & UserId & "', 
    '" & strtitle & "')" 

的代码插入进入一个SQL数据库。

'OPEN' 只是我试图插入一个collumn值..

回答

1

你想用insert . . . select语法:

INSERT INTO dbo.HEADER(plant, taskno, tskstatus, category, opened, openedby, title) 
    SELECT 'UK', Max(taskno) + 1, 'open', 'RFSA', 
      CONVERT(datetime,'" & strdate & "',103), 
      '" & UserId & "', 
      '" & strtitle & "' 
    from Header; 

但是,因为你正在使用SQL Server,你想taskno成为identity列。这会在每个插页上自动递增。

2

括号不匹配。

insert = "INSERT INTO dbo.HEADER 
    (plant, 
    taskno, 
    tskstatus, 
    category, 
    opened, 
    openedby, 
    title) 
VALUES 
    ('UK', 
    (select Max(taskno) AS count from HEADER)+ 1, 
    'open', 
    'RFSA', 
    CONVERT(datetime,'" & strdate & "',103), 
    '" & UserId & "', 
    '" & strtitle & "')"