无法批量加载。文件“'+ @CSVPath +'”不存在

时间:2015-10-06 18:20:34

标签: java sql-server jdbc sql-server-2008-r2 bulkinsert

存储过程中的FROM行取自多个指南(有些在stackoverflow上),但它似乎无法工作(无论我从java传递给SP)。

服务器在我的机器上。 java文件的选择路径与FROM。

下的SP注释中的同一文件相同

批量插入在sql server中作为普通查询执行时,带有注释路径的工作(已测试)。

使用jdbc4,sql server 2008r2。

存储过程:

CREATE PROCEDURE importUsers
@CSVPath NVARCHAR(200) /*the path parameter im using.*/
AS
BEGIN
SET NOCOUNT ON
SET XACT_ABORT ON

CREATE TABLE #Inserts(
rowNo int IDENTITY(1,1),
UserNo int,
email varchar(255),
nickName varchar(20),
registrationDate Date
);

BEGIN TRAN fetchFromFile

BULK INSERT #Inserts
FROM ''' + @CSVPath + '''
/*FROM 'e:\csv\test.csv'    THIS PATH WORKS*/
    WITH
    (
        FIELDTERMINATOR = '|',
        ROWTERMINATOR = '\n'
    )

    IF @@ERROR <> 0
    BEGIN
        ROLLBACK TRAN fetchFromFile
        GOTO ON_ERROR
    END

    IF @@ROWCOUNT > 0
    BEGIN

/*copy duplicates with higher rowNumber in file, 
        -- or ones that already exist in tblUser to updates table.*/
        SELECT *
        INTO #Updates
        FROM #Inserts O
        WHERE userNo IN (SELECT UserNo
                         FROM tblUser) OR
              EXISTS (SELECT *
                      FROM #Inserts I
                      WHERE O.UserNo = I.UserNo AND I.rowNo < O.rowNo)

        IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN fetchFromFile
            GOTO ON_ERROR
        END

        /*keep only inserts in the inserts table.*/
        DELETE FROM #Inserts 
        WHERE rowNo IN (SELECT rowNo 
                        FROM #Updates)

        /*keep only the latest updates of a certain user.*/
        DELETE FROM #Updates
        WHERE EXISTS (SELECT * 
                          FROM #Updates I 
                          WHERE I.UserNo = #Updates.UserNo AND I.rowNo > #Updates.rowNo)

        IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN fetchFromFile
            GOTO ON_ERROR
        END

        /*try inserting the inserts.*/
        INSERT INTO tblUser(UserNo, email, nickName, registrationDate)
        SELECT I.UserNo, I.email, I.nickName, I.registrationDate
        FROM #Inserts I

        IF @@ERROR <> 0
        BEGIN
            ROLLBACK TRAN fetchFromFile
            GOTO ON_ERROR
        END

        /*try updating the updates.*/
        UPDATE tblUser
        SET email = S.email, nickName = S.nickName, registrationDate = S.registrationDate
        FROM #Updates S
        WHERE S.UserNo = tblUser.UserNo

    END

COMMIT TRAN fetchFromFile

ON_ERROR:
PRINT 'rollback occured!';  

END

java snippets:

{ ....   // environment where the file/path is picked...
         FileChooser fileChooser = new FileChooser();
     fileChooser.setTitle("open csv file");
     fileChooser.getExtensionFilters().add(new ExtensionFilter("CSV files", "*.csv", "*.txt"));
     File potentialFile = fileChooser.showOpenDialog(ViewLogic.getPrimaryStage());
     if (potentialFile != null){
         selectedFile = potentialFile;
         filePathField.setText(selectedFile.getAbsolutePath());
         btnImport.setDisable(false);
     }

ViewLogic.importData(selectedFile.getCanonicalPath(), E_ImportType.USER);
}


// environment that executes the SP.
public boolean importData(String path ....
......
cstmt = database.getConnection().prepareCall("{call dbo.importUsers(?)}";
cstmt.setNString("CSVPath", path);
cstmt.executeUpdate();

0 个答案:

没有答案