This isn't a duplicate of Check constraint on date but I might have missed another similar question.
On MS SQL, you can create the following constraint:
ALTER TABLE [X] WITH CHECK ADD CONSTRAINT [CCCHK03_TBX] CHECK
(
[TBX_YEAR] = DATEPART( year, GetDate() )
)
You are able to insert records just fine but I am unable to fully test the implications of this though, what will happen when the server date ticks over to 2017? I am of the impression that it will allow inserts for 2017 but it'll theoretically invalidate all records for 2016.
This table is an insert only table so records are never meant to be updatable so that's not an issue. My main concern is whether there could potentially be a server stability issue caused by this?
I can't seem to find anything relating to this but MS must have a reason for allowing such a constraint.
Normally I'd advise creating an insert trigger and checking against that but this got me curious.
Edit: Expanded test from destination-data's answer:
IF ( OBJECT_ID( 'tempdb..#CheckTest' ) IS NOT NULL )
DROP TABLE #CheckTest
GO
CREATE TABLE #CheckTest ( MN INT )
ALTER TABLE #CheckTest WITH CHECK ADD CONSTRAINT CHK_MN CHECK ( MN = DATEPART( SECOND, GETDATE() ) )
ALTER TABLE #CheckTest CHECK CONSTRAINT CHK_MN
GO
-- Control Test. This will fail with:
--Msg 547, Level 16, State 0, Line 12
--The INSERT statement conflicted with the CHECK constraint "CHK_MN". The conflict occurred in database "tempdb", table "dbo.#CheckTest", column 'MN'.
--The statement has been terminated.
INSERT INTO #CheckTest ( MN )
VALUES ( DATEPART( SECOND, DATEADD( SECOND, 5, GETDATE() ) ) )
-- Add 5 different seconds.
DECLARE @Counter int = 0;
WHILE @Counter < 5
BEGIN
INSERT INTO #CheckTest ( MN )
VALUES ( DATEPART( SECOND, GETDATE() ) )
SET @Counter += 1;
-- Delay for a second.
WAITFOR DELAY '00:00:01';
END
GO
-- Add a different second.
-- Disabling and Enabling a check will work just fine so long as the check already exists.
ALTER TABLE #CheckTest NOCHECK CONSTRAINT CHK_MN;
INSERT INTO #CheckTest ( MN )
VALUES ( DATEPART( SECOND, DATEADD( SECOND, 5, GETDATE() ) ) )
ALTER TABLE #CheckTest CHECK CONSTRAINT CHK_MN;
-- Control Test. This will fail with:
--Msg 547, Level 16, State 0, Line 12
--The INSERT statement conflicted with the CHECK constraint "CHK_MN". The conflict occurred in database "tempdb", table "dbo.#CheckTest", column 'MN'.
--The statement has been terminated.
INSERT INTO #CheckTest ( MN )
VALUES ( DATEPART( SECOND, DATEADD( SECOND, 5, GETDATE() ) ) )
GO
-- Check table contents.
SELECT * FROM #CheckTest;
GO
-- Dropping and recreating the check constraint will result in an error:
--Msg 547, Level 16, State 0, Line 37
--The ALTER TABLE statement conflicted with the CHECK constraint "CHK_MN". The conflict occurred in database "tempdb", table "dbo.#CheckTest", column 'MN'.
ALTER TABLE #CheckTest DROP CONSTRAINT CHK_MN;
ALTER TABLE #CheckTest WITH CHECK ADD CONSTRAINT CHK_MN CHECK ( MN = DATEPART( SECOND, GETDATE() ) )
GO
DROP TABLE #CheckTest
GO
Edit 2: Summary
Based on the testing and feedback, while it sure was a fun exercise and definitely appears to be 100% perfectly valid, I'd definitely advise against it purely from a "future proof" point of view as the check will never be able to be altered. Personally I think the trigger based constraint would be the most maintainable.