ALTER TABLE with programmatically determined constant DEFAULT value
问题 I am trying to add a column (MSSQL 2005) to a table (Employee) with a default constraint of a primary key of another table (Department). Then I am going to make this column a FK to that table. Essentially this will assign new employees to a base department based off the department name if no DepartmentID is provided. This does not work: DECLARE @ErrorVar INT DECLARE @DepartmentID INT SELECT @DepartmentID = DepartmentID FROM Department WHERE RealName = 'RocketScience' ALTER TABLE [Employee]