SQLTeam.com | Weblogs | Forums

T-sql 2012 trigger


#1

In a t-sql 2012 database, I would like to setup a trigger and/or a constraint on a field called customerdata where the column is defined as varchar(255).

The constraint and/or trigger needs to be setup when issueid ='9807'. This is due to the fact that data is obtained from this table by doing several self-joins.

Thus would you show me the sql on how to accomplish my goal?


#2

What needs to happen when issueid ='9807' ? If you just want to prevent that data ever being stored in your database then a CONSTRAINT will do. If you want some other action (such as "Alert the supervisor") then you need a Trigger, or a report than runs periodically looking for newly changed data which has that value now (and did not have it last time)


#3

What I am really looking for is to prevent second row in a table for the current school year for each student. Thus would you show me the sql on how to accomplish this goal?


#4

Sounds like you could create a Unique index on the combination of the two columns:

CREATE UNIQUE INDEX YourIndexName
ON YourTableName
(
	StudentUniqueIDColoumnName,
	YearIndicatorColoumnName
)