== Why is this an issue?

Trying to assign a large character value to a smaller variable or column will raise an error.

include::../../../shared_content/plsql/data_dictionary.adoc[]

== How to fix it

Ensure that the size of the variable or column is large enough to hold the value.

=== Code examples

==== Noncompliant code example

[source,sql,diff-id=1,diff-type=noncompliant]
----
CREATE TABLE Persons
(
  Id   NUMBER,
  Name VARCHAR2(4)
);

INSERT INTO Persons (Id, Name) VALUES (1, 'Alice'); -- Noncompliant, raises ORA-12899

CREATE OR REPLACE PROCEDURE sp1
IS
  foo VARCHAR2(2);
BEGIN
  SELECT Name INTO foo FROM Persons WHERE Id = 1; -- Noncompliant, may raise ORA-06502
END;
----

==== Compliant solution

[source,sql,diff-id=1,diff-type=compliant]
----
CREATE TABLE Persons
(
  Id   NUMBER,
  Name VARCHAR2(8)
);

INSERT INTO Persons (Id, Name) VALUES (1, 'Alice');

CREATE OR REPLACE PROCEDURE sp1
IS
  foo VARCHAR2(8);
BEGIN
  SELECT Name INTO foo FROM Persons WHERE Id = 1;
END;
----

== Resources

=== Documentation

* CWE - https://cwe.mitre.org/data/definitions/704[CWE-704 - Incorrect Type Conversion or Cast]
* Oracle Database - https://docs.oracle.com/en/error-help/db/ora-12899[ORA-12899]
* Oracle Database - https://docs.oracle.com/en/error-help/db/ora-06502[ORA-06502]


ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]

include::../parameters.adoc[]

include::../highlighting.adoc[]

'''
== Comments And Links
(visible only on this page)

include::../comments-and-links.adoc[]

endif::env-github,rspecator-view[]
