I am using Java code to insert a data in SQL Server 2008. I am getting below error.
Statement st = null;
Connection con = null;
String loadQuery = "";
Class.forName(com.microsoft.sqlserver.jdbc.SQLServerDriver);
con = DriverManager.getConnection(jdbc:sqlserver://11.11.112.123:1433;DatabaseName=test123, test, test);
st = con.createStatement();
loadQuery = "BULK INSERT testres FROM 'D:\RTTM\NTB\Data\GDW\Files\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
System.out.println(loadQuery);
st.executeUpdate(loadQuery);
Query
BULK INSERT testres FROM 'D:\RTTM\NTB\Data\GDW\Files\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')
Error
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 ' '.
Database Details
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
JDBC jar Details
sqljdbc4-3.0.jar
您可以在D:\\RTTM\\NTB\\Data\\GDW\\Files\\ACCif.csv
吗?
loadQuery = "BULK INSERT testres FROM 'D:\RTTM\NTB\Data\GDW\Files\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n')";
The above statements has a lot of backslashes and as you would know the '\\' in a java string is used to indicate start of an escape sequence. When the loadQuery variable is used internally it will result in a broken sql being fired. So your can do below
loadQuery = "BULK INSERT testres FROM 'D:\\RTTM\\NTB\\Data\\GDW\\Files\\ACCif.csv' WITH ( FIRSTROW=2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\\n')";
From the syntax error suggested in stack trace i suspect that most likely the query breaks at the '\\n' character being presented to the SQL interpreter as a SQL string ' ' spread over two lines rather than your original intent of sending it as '\\n'
You should also have a look at this question How to show special escape characters like LineBreak in Java output?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.