我正在使用Java代码在SQL Server 2008中插入数据。我收到以下错误。
BULK INSERT testres FROM 'D:\RTTM\NTB\Data\GDW\Files\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
查询
Incorrect syntax near ' '.
com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near ' '.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:197)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1493)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.doExecuteStatement(SQLServerStatement.java:775)
at com.microsoft.sqlserver.jdbc.SQLServerStatement$StmtExecCmd.doExecute(SQLServerStatement.java:676)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:4874)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:1400)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:179)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:154)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeUpdate(SQLServerStatement.java:633)
at QueryCreation_sqlserver.readCsvUsingLoad(QueryCreation_sqlserver.java:225)
at QueryCreation_sqlserver.readCsvcoloumnname(QueryCreation_sqlserver.java:268)
at QueryCreation_sqlserver.main(QueryCreation_sqlserver.java:39)
Incorrect syntax near ' '.
错误
Driver name: Microsoft SQL Server JDBC Driver 3.0
Driver version: 3.0.1119.0
Product name: Microsoft SQL Server
Product version: 10.50.1600
数据库详细信息
sqljdbc4-3.0.jar
JDBC jar详细信息
json_decode
答案 0 :(得分:0)
你能逃回D:\RTTM\NTB\Data\GDW\Files\ACCif.csv
中的反斜杠吗?
答案 1 :(得分:0)
loadQuery = "BULK INSERT testres FROM 'D:\RTTM\NTB\Data\GDW\Files\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
上面的语句有很多反斜杠,你可能会知道java字符串中的'\'表示转义序列的开始。 当在内部使用loadQuery变量时,它将导致一个破坏的sql被触发。 所以你可以在下面做
loadQuery = "BULK INSERT testres FROM 'D:\\RTTM\\NTB\\Data\\GDW\\Files\\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\\n')";
从堆栈跟踪中建议的语法错误中我怀疑查询最有可能在'\ n'字符处以SQL字符串的形式呈现给SQL解释器,而不是发送它的原始意图作为'\ n'
您还应该看一下这个问题How to show special escape characters like LineBreak in Java output?