November 28, 2006 at 8:41 am
Guys,
Does SQL Server allow for creating conditional constraints on the table columns?
My table is:
CREATE TABLE [X]
(
[UID] [int] IDENTITY (1, 1) NOT NULL ,
[A] [varchar] (255) COLLATE SQL_Latin1_General_CP1_CS_AS NOT NULL ,
[bit] NOT NULL ,
CONSTRAINT [PK_X] PRIMARY KEY CLUSTERED
(
[UID]
) ON [PRIMARY]
) ON [PRIMARY]
I want to add the following constraints:
CONSTRAINT [IX_UNIQUE1] UNIQUE NONCLUSTERED
(
A, B = 1
) ON [PRIMARY]
CONSTRAINT [IX_UNIQUE2] UNIQUE NONCLUSTERED
(
UPPER(A), B = 0
) ON [PRIMARY]
That is, if the table contains "Hello" in column A, then an insert of ("hello", 1) will be rejected, but ("hello", 0) will be inserted.
Any ideas?
Thanks a lot
November 28, 2006 at 8:49 am
write a function and reference the function in the constraint.
eg constraint check (dbo.BobIsRight(colname1, col23) = 'True')
November 28, 2006 at 8:50 am
Or do it in a trigger. That way you can send a more detailed error message.
November 28, 2006 at 9:05 am
Thanks a lot guys.
Let me try both approaches.
November 28, 2006 at 11:41 am
Guys,
If I create an INSERT trigger and then peform an INSERT of items (A,B), is it possible, inside the trigger, to see the items which are being inserted by the INSERT statement?
That is, I'd like the trigger to behave differently based on the values which get passed to the INSERT statement.
Is that possible?
Thanks a lot
November 28, 2006 at 11:53 am
All this code is accessible only from inside a trigger
Select * FROM Inserted >> new data (inserted or updated)
Select * FROM Deleted >> old date (updated or deleted)
November 28, 2006 at 12:35 pm
This is great info.
Thanks a lot
November 28, 2006 at 1:36 pm
There appears to be a problem with using a trigger in my case.
Namely, I believe we only have an AFTER TRIGGER and an INSTEAD OF TRIGGER.
If I use INSTEAD OF TRIGGER, then my data would never get inserted.
If I use AFTER TRIGGER, then the data will get inserted before I can run any tests on it.
Is there any way to have an INSERT made only after the trigger finishes its code?
I know that I can perform a rollback, but that won't work for me.
The issue is that I need to check that a certain element is already in the table. But if it is INSERTed first, then it would always be in the table and I won't be able to perform the check correctly.
Is there any other way to go that anyone can see?
Thanks a lot
November 28, 2006 at 1:40 pm
Both trigger types could do this. I would probabely use the instead of insert (just a hunce).
Once you validated the new data in the instead of trigger, you have to insert into manually in the table from the trigger for the insert to take place.
November 28, 2006 at 1:46 pm
Ah ... initially I assumed that by performing an INSERT within the TRIGGER we might enter some infinite loop where the INSERT would fire a TRIGGER, which would in turn perform and INSERT which would fire a trigger ...
Now I tried it and it appears to work fine!
Thanks a lot.
November 28, 2006 at 1:51 pm
Quoting one member of this forum (sorry forgot your name) :
"When in doubt : test, test, test"
Viewing 11 posts - 1 through 10 (of 10 total)
You must be logged in to reply to this topic. Login to reply