Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sql-server/21.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用SQL语句存储大型文本常量_Sql_Sql Server - Fatal编程技术网

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

使用SQL语句存储大型文本常量,sql,sql-server,Sql,Sql Server,在使用SQL语句时,存储非常长的随机字符文本字符串的最佳实践是什么?我正在做这样的事情: insert into tblMyTable (MyID,MyText) values (1,'this is a very long text which could contain punctuation and odd characters'); 我知道您应该将字符串中的单引号加倍,这样它们就不会被解释为分隔符,但是它们的其他考虑因素是否会影响字符串常量 如果您使用参数化sql(我强烈建议您不必担心

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

insert into tblMyTable (MyID,MyText) values (1,'this is a very long text which could contain punctuation and odd characters');
我知道您应该将字符串中的单引号加倍,这样它们就不会被解释为分隔符,但是它们的其他考虑因素是否会影响字符串常量


如果您使用参数化sql(我强烈建议您不必担心双引号),MyText是一个varcharmax

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);
   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 

正如Sean提到的,参数化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);
   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 

使用参数化查询我很困惑,存储是什么意思?你期望出现什么问题,你在考虑/研究什么解决方案?我想这是我的问题,从用户的体验中我还能期待什么。这是真的,但大多数时候你没有在sql中设置值,而是在应用程序中设置值。从应用程序中,您不希望将您的报价加倍,我们将为您的实际数据加上双引号。如果您是从应用程序调用proc,您是对的。但是,如果你只是SSM,你需要双单引号: