如何通过脚本插入

时间:2017-01-30 15:32:34

标签: sql-server powershell

我有一个powerhshell脚本,它将值插入到文本列中。 在我的查询命令中是这样的:

private JsonSomeResponse resolveResponse(JsonSomeRequest jsonRequest, String accessToken)
{
    Response response = someClient.doThisOrThat(jsonRequest, "Bearer " + accessToken);
    JsonSomeResponse jsonResponse = null;
    String resultBody = "";
    try {
        try (BufferedReader buffer = new BufferedReader(new InputStreamReader(response.body().asInputStream()))) {
            resultBody = buffer.lines().collect(Collectors.joining("\n"));
        }
        jsonResponse = objectMapper.readValue(resultBody, JsonSomeResponse.class);
    } catch (IOException ex) {
        throw new RuntimeException("Failed to process response body.", ex);
    }
    // just an example of using exception
    if (response.status() >= 400) thrown new OmgWeAllAreDeadException();
    return jsonResponse;
}

当我从我的数据库中选择数据时(在上面的更新之后),我期望这样的事情:

query = "UPDATE mytable 
SET mytable.mycolumn = 'FILE1
FILE2
FILE3
FILE4
FILE...
FROM mytable 
WHERE someconditions..."

但在数据库中我有

SELECT mycolumn FROM mytable

mycolumn
FILE1
FILE2
FILE3
FILE4
FILE...

将我的查询修改为:

mycolumn
FILE1            FILE2                 FILE3          FILE4         FILE...

结果是

query = "UPDATE mytable SET mytable.mycolumn = 'FILE1`r`n FILE2`r`nFILE3...

1 个答案:

答案 0 :(得分:0)

尝试使用:

query = "UPDATE mytable" + 
"SET mytable.mycolumn = 'FILE1' + CHAR(13) + CHAR(10) + " +
"'FILE2' + CHAR(13) + CHAR(10) + " +
"'FILE3'  + CHAR(13) + CHAR(10) + " +
"'FILE4'  + CHAR(13) + CHAR(10) + " +
"'FILE'...  + CHAR(13) + CHAR(10) + " +
"FROM mytable " + 
"WHERE someconditions..."