How to properly access a VARCHAR(MAX) parameter value of a FireDAC dataset when getting “data too large for variable” error?

前端 未结 1 361
南方客
南方客 2021-01-21 03:08

Our application updates and accesses data using SQL Server 2014.

I have a table of which the last column (\'Contents\') is created as VARCHAR(MAX).

相关标签:
1条回答
  • 2021-01-21 03:31

    Is it acceptable to access a VARCHAR(MAX) type field parameter value by the AsWideMemo property?

    Not especially. For VARCHAR(MAX) field parameter use AsMemo access. It is because you could be sending to your DBMS Unicode values to a non Unicode field. From the reference:

    The Unicode encoded parameter value is converted to a Unicode character set, which is supported by the DBMS, and sent to the DBMS. This does not depend on a client character set or on a Delphi version.

    If your field would be NVARCHAR(MAX), using AsWideMemo access to the parameter value would be the right choice.


    Why am I getting “data too large for variable” error when having assigned a more than 8k chars long string by the AsString property?

    Some background to why this happens. By accessing a certain parameter value by As<T> property, the engine also sets the parameter DataType, if you don't explicitly do that before. In this particular case you hinted the engine that it's fine for you if it sets the parameter data type to ftWideString or ftString just by accessing parameter value by the AsString property.

    And thanks to data type mapping such parameter is treated as the VARCHAR[n] or NVARCHAR[n] data type including its limits (hence you got a string length limit error here).

    Similar, just more specific data type hint is used when you access the parameter value by AsMemo property, it defaults to ftMemo data type which maps to VARCHAR(MAX). And as you may predict, AsWideMemo access defaults to ftWideMemo which maps to NVARCHAR(MAX) data type. If you don't want to explicitly set the parameter data types but use this hinting, consult the manual to see how each used access property sets the default parameter data type.

    0 讨论(0)
提交回复
热议问题