April 20, 2009 at 7:49 pm
Hi
If i define a RULE and Check constraint on the same column then which gets executed first.
CREATE TABLE [dbo].[ACCOUNT](
[ACCOUNT_ID] [int] NULL,
[ACCOUNT_LOCATION] [varchar](10) NULL,
[CUSTOMER_ID] [int] NULL,
[CHECK_IN_AMT] [int] NULL,
[SAVINGS_AMT] [int] NULL
)
CREATE RULE Amount_rule
AS
(@CHECK_IN_AMT >= 1000)
EXEC sp_bindrule 'Amount_rule', 'account.CHECK_IN_AMT'
ALTER TABLE ACCOUNT
ADD CONSTRAINT cc_MinBal
CHECK (CHECK_IN_AMT > 800);
I am newbie in SQL Server and trying to learn the concepts.
April 20, 2009 at 8:56 pm
they don't really get executed first or second.... it's more like ALL constraints, including whether a column is nullable or not, all get evaluated prior to the the insert or update...if any constraint gets violated, an error is raised.
while it's true that in your example, if i put 100 dollars in the column, and there are two different constraints, one checking > 800 and the other for > 1000, only one of them is going to be returned, but i don't believe you can decide which occurs first.
it could be on my server, the check constraint returns the error, and on your server, the bind rule constraint returns as the error.
I've never seen any info on the order of precedence.
FYI, when i try to insert, the rule raises an error on my machine:
insert into account([CHECK_IN_AMT]) values (100)
Msg 513, Level 16, State 0, Line 1
A column insert or update conflicts with a rule imposed by a previous CREATE RULE statement. The statement was terminated. The conflict occurred in database 'tempdb', table 'dbo.ACCOUNT', column 'CHECK_IN_AMT'.
The statement has been terminated.
Lowell
Viewing 2 posts - 1 through 1 (of 1 total)
You must be logged in to reply to this topic. Login to reply