字符串数据,SQLSRV PHP 驱动程序中的右截断(“甜甜圈洞”)
我正在使用 Microsoft 的 sqlsrv PHP 驱动程序(在 Windows 2003 服务器上)将大量文本写入 text
列。
有一个奇怪的“甜甜圈洞”错误,如果文本的长度相当小,则保存成功,如果文本足够大,则也保存成功。在这之间,我们收到以下错误:
SQLSTATE: 22001
code: 0
message: [Microsoft][SQL Server Native Client 10.0]String data, right truncation
下限(触发错误)似乎约为 4011 个字符,上限为 8024。数据库中还有其他一些列可能会使其超过 -两个边界(4096 和 8192)。
无论正在运行什么 SQL,都会发生这种情况。作为测试用例,我们运行了以下内容:
$connectionInfo = array("UID" => "REDACTED", "PWD" => "REDACTED", "Database"=>"REDACTED", "CharacterSet" => "UTF-8");
$conn = sqlsrv_connect("SQLSERVER", $connectionInfo);
$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(&$large_string));
if (!sqlsrv_execute($stmt))
print_r(sqlsrv_errors());
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
我们发现另一个表现出相同行为的表(使用 varchar(max)
列)。
编辑:在对 varchar 列使用 UTF-8 编码时会发生这种情况,但在使用标准编码时不会发生这种情况。
I'm writing large-ish amounts of text to a text
column using Microsoft's sqlsrv PHP driver (on a Windows 2003 server).
There's an odd "donut hole" error, where if the length of the text is fairly small, it is saved successfully, and if the text is sufficiently large, it is also save successfully. In between, we get the following error:
SQLSTATE: 22001
code: 0
message: [Microsoft][SQL Server Native Client 10.0]String data, right truncation
The lower limit (to trigger the error) appears to be around 4011 characters, and the upper limit 8024. There are a few other columns in the database that might push it over the power-of-two boundary (4096 and 8192).
This happens regardless of the sql being run. As a test case, we ran the following:
$connectionInfo = array("UID" => "REDACTED", "PWD" => "REDACTED", "Database"=>"REDACTED", "CharacterSet" => "UTF-8");
$conn = sqlsrv_connect("SQLSERVER", $connectionInfo);
$stmt = sqlsrv_prepare($conn, "SELECT LEN(?)", array(&$large_string));
if (!sqlsrv_execute($stmt))
print_r(sqlsrv_errors());
sqlsrv_free_stmt($stmt);
sqlsrv_close($conn);
We've found another table (using varchar(max)
columns) that exhibits the same behavior.
Edit: This occurs when using UTF-8 encoding with a varchar column, but not when using the standard encoding.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您在该参数中提供其他参数,该错误就会消失:
这可能与它将 UTF-8 值传递到 varchar(相对于 nvarchar)列这一事实有关。
If you feed additional parameters in with the parameter, the bug disappears:
This may be related to the fact that it is passing a UTF-8 value into a varchar (versus nvarchar) column.