关于这个话题,我没有看到任何类似的问题,我必须为我现在正在做的事情研究这个问题。我想我应该把答案贴出来以防其他人也有同样的问题。
当前回答
所有这些选项都可以工作,这取决于您的情况,但如果您使用SSMS(正如在一些评论中提到的SSMS隐藏CR/ lf),您可能看不到它们中的任何一个工作。
所以,与其开车去拐弯,不如检查一下这个设置
工具|选项
其他回答
我想说的
concat('This is line 1.', 0xd0a, 'This is line 2.')
or
concat(N'This is line 1.', 0xd000a, N'This is line 2.')
所有这些选项都可以工作,这取决于您的情况,但如果您使用SSMS(正如在一些评论中提到的SSMS隐藏CR/ lf),您可能看不到它们中的任何一个工作。
所以,与其开车去拐弯,不如检查一下这个设置
工具|选项
下面是一个c#函数,它在现有文本blob之前加上一个文本行,由crlf分隔,并返回适合于INSERT或UPDATE操作的T-SQL表达式。它有一些我们专有的错误处理,但一旦你把它去掉,它可能会有帮助,我希望如此。
/// <summary>
/// Generate a SQL string value expression suitable for INSERT/UPDATE operations that prepends
/// the specified line to an existing block of text, assumed to have \r\n delimiters, and
/// truncate at a maximum length.
/// </summary>
/// <param name="sNewLine">Single text line to be prepended to existing text</param>
/// <param name="sOrigLines">Current text value; assumed to be CRLF-delimited</param>
/// <param name="iMaxLen">Integer field length</param>
/// <returns>String: SQL string expression suitable for INSERT/UPDATE operations. Empty on error.</returns>
private string PrependCommentLine(string sNewLine, String sOrigLines, int iMaxLen)
{
String fn = MethodBase.GetCurrentMethod().Name;
try
{
String [] line_array = sOrigLines.Split("\r\n".ToCharArray());
List<string> orig_lines = new List<string>();
foreach(String orig_line in line_array)
{
if (!String.IsNullOrEmpty(orig_line))
{
orig_lines.Add(orig_line);
}
} // end foreach(original line)
String final_comments = "'" + sNewLine + "' + CHAR(13) + CHAR(10) ";
int cum_length = sNewLine.Length + 2;
foreach(String orig_line in orig_lines)
{
String curline = orig_line;
if (cum_length >= iMaxLen) break; // stop appending if we're already over
if ((cum_length+orig_line.Length+2)>=iMaxLen) // If this one will push us over, truncate and warn:
{
Util.HandleAppErr(this, fn, "Truncating comments: " + orig_line);
curline = orig_line.Substring(0, iMaxLen - (cum_length + 3));
}
final_comments += " + '" + curline + "' + CHAR(13) + CHAR(10) \r\n";
cum_length += orig_line.Length + 2;
} // end foreach(second pass on original lines)
return(final_comments);
} // end main try()
catch(Exception exc)
{
Util.HandleExc(this,fn,exc);
return("");
}
}
我来这里是因为我担心我在c#字符串中指定的cr-lfs没有在SQl Server Management Studio查询响应中显示。
事实证明,它们确实存在,但没有被展示出来。
要“查看”cr-lfs,使用如下的打印语句:
declare @tmp varchar(500)
select @tmp = msgbody from emailssentlog where id=6769;
print @tmp
这总是很酷的,因为当您从Oracle获得导出列表时,您将获得跨越几行的记录,这对于cvs文件来说可能很有趣,所以要小心。
无论如何,Rob的答案很好,但我建议使用其他的东西,而不是@,尝试更多的东西,比如§§@@§§或其他的东西,这样它就有一些独特性的机会。(但是仍然要记住你插入的varchar/nvarchar字段的长度..)
推荐文章
- 我如何循环通过一组记录在SQL Server?
- 数据库和模式的区别
- 如何在SQL Server中一次更改多个列
- 如何从命令行通过mysql运行一个查询?
- 外键约束可能导致循环或多条级联路径?
- 使用LIMIT/OFFSET运行查询,还可以获得总行数
- 当恢复sql时,psql无效命令\N
- 货币应该使用哪种数据类型?
- 如何选择每一行的列值不是独特的
- 如何改变字符集(和排序)在整个数据库?
- mySQL:: insert到表,数据从另一个表?
- nvarchar(max)非文本
- 如何在Postgres中获得两个字段的MIN() ?
- 在SQL Server 2008 R2中重命名数据库时出错
- 将数据复制到另一个表中