-1

I'm using Microsoft SQL server 2008 and I need to create a constraint that will prevent a [Tx] from being created/updated if the [Driver] is not allowed to either the source [Shipper] or destination [Consignee] site.

Tables:

  • Drivers (holds typical driver info)
  • Shipper/Consignee (source/destination site - holds locations the driver goes from/to)
  • DriverForbiddenSite (holds sites the driver is not allowed to)
  • Schedule (hold driver, and when)
  • Tx (a transaction has source site{shipper}, destination site {consignee}, cargo, schedule)

Here is a query that will show any record that violates the rule:

select * from DriverForbiddenSite dfs
    join Schedule sch on dfs.driverId=sch.driverId
    join Tx tx on sch.scheduleId=tx.scheduleId
    left join Shipper shi on shi.shipperId=tx.shipperId
    left join Consignee con on con.consigneeId=tx.consigneeId
where dfs.transloadLocationId=shi.transloadLocationId or dfs.transloadLocationId=con.transloadLocationId

How would be best to prevent Tx's from being created if the Driver is not allowed on the Site?

Thanks

Alan B. Dee
  • 5,490
  • 4
  • 34
  • 29

1 Answers1

0

Full credit goes to Rahul. I was trying to solve this by using a constraint when instead I should use a trigger. The "instead of insert" and "instead of update" trigger will allow me to check if the driver is allowed on the Tx before it inserts/updates a Tx record.

Thanks Rahul, feel free to enter your own answer and I'll accept that instead.

Alan B. Dee
  • 5,490
  • 4
  • 34
  • 29