Page 1 of 1

How to pass unicode literal in parameter?

Posted: Thu 18 Jul 2019 10:24
by brace
How to tell SDAC to use a unicode literal?

I would like that when i do

Code: Select all

  MSQuery1.SQL.Text := 'UPDATE MYTABLE SET FIELD= :NEWVALUE';
  MSQuery1.ParamByName('NEWVALUE').AsString := '゚ワノラルユナ';
  MSQuery1.Execute;
SDAC passes N'゚ワノラルユナ' instead of 'ワノラルユナ', how is doable?

WIthout this nvarchar strings are casted to varchar.

Thanks.

Re: How to pass unicode literal in parameter?

Posted: Thu 18 Jul 2019 14:25
by Stellar
When you set the value of the string parameter using the AsString property, SDAC will set the data type for the parameter to ftWideString by default in Delphi 2009 or newer. In Delphi versions older than 2009, the data type for the string parameter will be set to ftString. To solve the issue, you can explicitly set the parameter type to ftString\ftWideString, or set the parameter value using the AsAnsiString\AsWideString property.

Re: How to pass unicode literal in parameter?

Posted: Thu 18 Jul 2019 14:58
by brace
By using asstring i got unicode chars inserted as "?????" in SQL Server, i had to do a workaround avoiding using parameters bu by creating the update statement like this:

MSQuery1.SQL.Text := 'UPDATE MYTABLE SET FIELD = N''' + unicodeText+ '''';

I use DElphi 10, so it seems what you are telling me is not right.

Re: How to pass unicode literal in parameter?

Posted: Fri 19 Jul 2019 07:28
by Stellar
Please check the data type of the "NEWVALUE" parameter after assigning a value to it. For example:

Code: Select all

var
  FieldType: TFieldType;
begin
  MSQuery1.SQL.Text := 'UPDATE MYTABLE SET FIELD = :NEWVALUE';
  MSQuery1.ParamByName('NEWVALUE').AsString := '゚ワノラルユナ';
  FieldType :=  MSQuery1.ParamByName('NEWVALUE').DataType; //ftWideString or ftString
  MSQuery1.Execute;
end;

Re: How to pass unicode literal in parameter?

Posted: Fri 19 Jul 2019 07:34
by brace
It is ftString, since the query SQL is created at runtime as in the code snippet, how can i force the parameter Datatype to Thanks.

Re: How to pass unicode literal in parameter?

Posted: Fri 19 Jul 2019 08:44
by Stellar
Please try to explicitly set the data type for the parameter after assinging a value to it, e.g.:

Code: Select all

MSQuery1.SQL.Text := 'UPDATE MYTABLE SET FIELD = :NEWVALUE';
MSQuery1.ParamByName('NEWVALUE').AsString := '゚ワノラルユナ';
MSQuery1.ParamByName('NEWVALUE').DataType := ftWideString;
MSQuery1.Execute;
Additionally, check the value of the global variable ParamStringAsAnsiString, which defaults to False. If you set it to True, all String parameters that are set using the AsString method will be of type ftString.
To execute the statement, please try to set it to False, e.g.:

Code: Select all

ParamStringAsAnsiString := False;
Note that the ParamStringAsAnsiString variable is located in unit DBAccess.

Re: How to pass unicode literal in parameter?

Posted: Fri 19 Jul 2019 09:02
by brace
Thanks a lot.

Re: How to pass unicode literal in parameter?

Posted: Mon 22 Jul 2019 08:26
by Stellar
Glad to see that the issue was resolved.
Feel free to contact us if you have any further questions about our products.