Working in SQL Server, let's say I have a database with two tables: events
and competitors
.
events
has the following columns:
[event_id] [event_name] [winner_id] [loser_id]
competitors
has the following columns:
[comp_id] [comp_name]
I wanted to add a default constraint to [event_name]
to save me time when inserting a new event. Something similar to:
ALTER TABLE [events]
ADD CONSTRAINT DF_event_name
DEFAULT CAST([winner_id] AS varchar(10)) + ' vs ' + CAST([loser_id] AS varchar(10)) FOR [event_name]
Of course, this is a simplified version, it would also need to implement a join statement with competitors ON comp_id = winner_id and comp_id = loser_id
.
When I tried setting the above default constraint, I got the response you are probably expecting:
The name "winner_id" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.
So is there any way to work around what I'm trying to do?
Is there any way to automatically set the value of a cell to a string implementing data inserted from the same row? If that's possible, can it also be done using a join statement? My next idea would be a trigger.