sql server - TSQL - How to check a value against a value in a different column before inserting? -
Is there a way to enforce a check obligation that checks against another value in another column in the same table is? Specifically, I want to make sure that the "checkin" date value in the table is greater than the date value in the table before inserting the row.
I can remember some details, so please let me know that this is the case.
You can use it as a check:
ALTER TABLE dbo.myTable ADD CONSTRAINT CK_myTable_CheckoutGreaterThanCheckin check (dbo.CheckDates ()> 1) Then add a function like this:
Create function checkdates () Returns RARNS DECLARE @retval int SET @retval = (SELECT COUNT (*) myTable WHERE checkout & gt; Checkin) Return @ Retaleand; They can not work in writing (sorry currently no access to SQL engine.)
Or you may want to include in the query the query That's because you do not have to use the error handling in your code to fail when your check fails.
If you still want to fail, then you want to expand that section to something:
Enter Meritable (checkout) value (@ checkout ) WHERE @checkout & gt; @chekin and id column = @ id column If you want to return some type of indicators that there was a problem, then you can use anything like the above but SELECT @@ Add ROWCOUNT or you can just use the price check in advance:
DECLARE @var SET @var = (SELECT count (*) myTable WHERE @checkout> Checkin and ID Column = @ID column) When the value is crossed, it will be equal to 1.
Comments
Post a Comment