Can you use an inserted variable in a default constraint 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.
No, default constraints cannot depend on other columns.  Further, neither default constraints nor computed columns can reference other tables (unless you define a scalar udf - not recommended!).  As mentioned, you can achieve this constraint using triggers.

I'd recommend you create and use a view over these two tables.  Select from the tables joined together and compute the data you want.