使用SQL语句存储大型文本常量

时间:2014-07-08 19:06:07

标签: sql sql-server

使用SQL语句时,存储非常长的随机字符文本字符串的最佳做法是什么?我正在做这样的事情:

insert into tblMyTable (MyID,MyText) values (1,'this is a very long text which could contain punctuation and odd characters');

我知道你应该将字符串中的单引号加倍,这样它们就不会被解释为分隔符,但是它们是否会在调整字符串常量时考虑其他因素?

MyText是varchar(max)

2 个答案:

答案 0 :(得分:0)

如果您使用参数化的sql(我强烈推荐),您不必担心双引号。

declare @MyID int = 1;
declare @MyText varchar(max) = 'this is a very long text which could contain punctuation and odd characters';

insert into tblMyTable (MyID,MyText) values (@MyID, @MyText);

答案 1 :(得分:0)

正如肖恩所说,参数化的sql是一个干净的解决方案。但你仍然需要担心两个单引号。

   declare @MyID int = 1;

-- single quote bad
--    declare @MyText varchar(max) = 'this is a very long text which could've contained punctuation and odd characters';

-- two single quotes good
      declare @MyText varchar(max) = 'this is a very long text which could''ve contained punctuation and odd characters';

select @MyID, @MyText