Constraint - sql server -
I want to add an obstacle to a column that depends on one column from a different table. For example, if I have string column str1 in table T1, and I have a string cluster strz on table T2, I should be str1 when leaving srt2 - except for "ok".
You can not use SELECT in an interrupt, but the user-defined function is allowed. So you can create a function to get strobe from T2. Here is an example setup: Make the table T1 (ID int, Straw 1 curve (max)) Make Tables T2 (Straw 2 Varcharey (10)) Make the Function Debo. Getts tr2 () returns as return curve (10) @ Selection of retala varchar (10) @retval = str2 T2 Returns @ Returns the table by going to end and change the t1 barrier CheckStr2Constraint check (str1 zero or dbo.GetStr2 () = ' Okay');
You can test like this:
Insert T2 values ('OK') in T1 values (1, 'test ') - Enter the Squeeds T1 values (1, empty) - Update updates T2 set in St 2 =' OK 'T1 values (1,' test ') - inserted into T1 values (1, empty) - Dormant
The third INSERT will fail, because str2 is not okay, and you are inserting a non-empty string. The error message will look like this:
The INSERT statement is contradictory with the Czech check "CheckStr2Constraint".
Comments
Post a Comment