Table A has a computed field called Computed1. It is persisted and non-zero. In addition, it always evaluates an expression that is char (50). It is also unique and has a unique key limitation.
Table B has the field RefersToComputed1, which should refer to the actual value of Computed1.
Attempting to create a foreign key constraint on B RefersToComputed1 that references A 'Computed1 results in the following error:
Error SQL01268: .Net SqlClient Data Provider: Msg 1753, Level 16, State 0, Line 1 Column 'B.RefersToComputed1' is not the same length or scale as referencing column 'A.Computed1' in foreign key 'FK_B_A'. Columns participating in a foreign key relationship must be defined with the same length and scale.
Q: Why is this error created? Are there special measures for foreign keys for computed columns, and if so, what are they?
Summary:
- A specific problem arises from char computed fields that are varchar. Therefore, Computed1 is varchar (50), not char (50).
- It is best for the cast to surround the computed field expression in order to force it to be used for a particular type. Credit is sent to Cade Roux for this advice.
sql-server tsql sql-server-2008 foreign-keys calculated-columns
Asaf r
source share