Oracle参数:期望为固定字符长度提供填充值。

huangapple go评论56阅读模式
英文:

Oracle Parameters: expects padded value for fixed char length

问题

如何有效地使用 OracleCommand 和参数从 Oracle 数据库中获取数据,当你有一个固定长度的 char 列,而且不知道列有多大时?

假设我们想要获取一个名为 IID 的 ID,它是一个固定长度的 char 列(5字节)。

OracleCommand myCommand1;
myCommand1.CommandText = "SELECT * FROM IDS WHERE IID = :IID";
myCommand1.Parameters.AddWithValue("IID", "1234");

上述代码不会返回任何结果,但是

myCommand1.Parameters.AddWithValue("IID", "1234 ");

会返回结果,因为它与数据库中的5个字节匹配。

你也可以指定空格:

myCommand1.Parameters.Add("IID", OracleDbType.Char, 5).Value = "1234";

但在我的情况下,假装程序员不总是知道数据库中 char 列的确切大小(无论是否合理)。你如何在不填充的情况下传递参数 "1234"?

提前感谢。

英文:

How do you efficiently use OracleCommand and Parameters to get data from an Oracle DB when you have a fixed length char colum and don`t know how big the column is?

Let`s assume we want to get an ID named IID which is a char (5 bytes) fixed.

 OracleCommand myCommand1;
 myCommand1.CommandText = "SELECT * FROM IDS WHERE IID = :IID";
 myCommand1.Parameters.AddWithValue("IID", "1234");

Would NOT return an item but

 myCommand1.Parameters.AddWithValue("IID", "1234 ");

Would because it matches the 5 bytes in the database

You could also specify the space

myCommand1.Parameters.Add("IID", OracleDbType.Char, 5).Value = "1234";

But in my case just pretend that the programmer does not always know the exact amount of the char size defined in the database (if it makes sense or not). I use devart but I think this is more of a general issue. How could you pass the "1234" parameter without padding?

Thanks in advance

答案1

得分: 1

数据类型比较规则文档中提取的内容:

空格填充和非填充比较语义

使用空格填充语义时,如果两个值的长度不同,Oracle首先在较短的值的末尾添加空格,使它们的长度相等。然后,Oracle逐个字符地比较这些值,直到第一个不同的字符。在第一个不同位置的字符更大的值被视为更大。如果两个值没有不同的字符,则它们被视为相等。这个规则意味着如果两个值只在尾随空格的数量上不同,那么它们是相等的。Oracle仅在比较中的两个值都是数据类型CHARNCHAR、文本字面值或由USER函数返回的值时使用空格填充的比较语义。

使用非填充语义时,Oracle逐个字符地比较两个值,直到第一个不同的字符。在该位置上的字符更大的值被视为更大。如果两个不同长度的值在较短值的末尾相同,那么较长的值被视为更大。如果两个等长的值没有不同的字符,则这些值被视为相等。只要比较中的一个或两个值具有数据类型VARCHAR2NVARCHAR2,Oracle就使用非填充的比较语义。

如果有两个CHAR值,那么Oracle应该使用空格填充的比较语义,并将空格添加到字符串中,直到它们的长度相等。

如果有一个或多个VARCHAR2值,那么Oracle将使用非填充的比较语义。

这意味着您应该能够传递任意长度的CHAR,它将以适当的长度进行比较:

string iid = "1234";
myCommand1.Parameters.Add("IID", OracleDbType.Char, iid.Length).Value = iid;

注意:如果您遇到这个问题,那么建议您不要将字符串存储为CHAR,而应该使用VARCHAR2

英文:

From the Data Type Comparison Rules documentation:

> ### Blank-Padded and Nonpadded Comparison Semantics
>
> With blank-padded semantics, if the two values have different lengths, then Oracle first adds blanks to the end of the shorter one so their lengths are equal. Oracle then compares the values character by character up to the first character that differs. The value with the greater character in the first differing position is considered greater. If two values have no differing characters, then they are considered equal. This rule means that two values are equal if they differ only in the number of trailing blanks. Oracle uses blank-padded comparison semantics only when both values in the comparison are either expressions of data type CHAR, NCHAR, text literals, or values returned by the USER function.
>
> With nonpadded semantics, Oracle compares two values character by character up to the first character that differs. The value with the greater character in that position is considered greater. If two values of different length are identical up to the end of the shorter one, then the longer value is considered greater. If two values of equal length have no differing characters, then the values are considered equal. Oracle uses nonpadded comparison semantics whenever one or both values in the comparison have the data type VARCHAR2 or NVARCHAR2.

If you have two CHAR values then Oracle should use Blank-Padded Comparison Semantics and will add blanks to the strings until they are equal length.

If you have one-or-more VARCHAR2 values then Oracle will use Non-Padded Comparison Semantics.

This means you should be able to pass a CHAR of any length and it will be compared at the appropriate length:

string iid = "1234";
myCommand1.Parameters.Add("IID", OracleDbType.Char, iid.Length).Value = iid;

Note: If you are having this problem then it suggests that you should not be storing strings as a CHAR and should be using VARCHAR2 instead.

huangapple
  • 本文由 发表于 2023年6月26日 20:54:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/76556888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定