-
October 24th, 2019, 11:53 PM
#1
Check Constraint Help
Can i create Custom check constraint in SQL Server?
I have a table 'City' having columns as 'ID' , 'Name' and 'Number'.
If Name is 'AA' then Number column shouldn't accept null values. If name is other than 'AA' Number column can be null
-
October 25th, 2019, 11:28 PM
#2
Re: Check Constraint Help
I believe a constraint can call a function, but I'm on my phone so I really can't look into it fully. However, doing a google search for, "sql constraint calls function" seemed to look promising. Hopefully, this will help.
-
October 26th, 2019, 02:16 AM
#3
Re: Check Constraint Help
Originally Posted by Sherin Mathew
Can i create Custom check constraint in SQL Server?
I have a table 'City' having columns as 'ID' , 'Name' and 'Number'.
If Name is 'AA' then Number column shouldn't accept null values. If name is other than 'AA' Number column can be null
You could implement an "Instead ff" Trigger like:
Code:
CREATE TRIGGER dbo.OnInsertCity
ON dbo.City
INSTEAD OF INSERT
AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.
SET NOCOUNT ON;
DECLARE @city varchar(50),@number int
SELECT @city=[name] ,@number=[number] from inserted
IF (@number IS NULL and @city ='AA')
BEGIN
RAISERROR ('You are not allowed to Add These Data.', 10, 11)
END
ELSE
INSERT INTO City ([name] ,[number]) values (@city,@number)
END
GO
Victor Nijegorodov
-
November 3rd, 2019, 11:56 PM
#4
Re: Check Constraint Help
Hello,
You can call a UDF in check constraint which will work as per your requirement. Hope this will work as per your requirement.
-
November 4th, 2019, 02:14 AM
#5
Re: Check Constraint Help
Originally Posted by ishan_shah
Hello,
You can call a UDF in check constraint which will work as per your requirement. Hope this will work as per your requirement.
Could you post an example for the OP's case
Victor Nijegorodov
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|