0

In Below Table, I want full_name column to take Default value as concatenation of values in first_name, middle_name & last_name columns.

SQL Table

CREATE TABLE ms_employee
(
    emp_no int unique,
    first_name varchar(100) not null,
    middle_name varchar(100) not null,
    last_name varchar(100) not null,
    full_name varchar(100)  
)

Code

code below gives error

ALTER TABLE ms_employee ADD CONSTRAINT [cons_1] DEFAULT ([first_name]+' '+[middle_name]+' '+[last_name]) FOR [emp_name]

Error Message

The name "first_name" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

So, it's not working using Constraint!

How to achieve it, using Constraint? OR any other way will also be appreciated !

1 Answer 1

2

You can change it to a Computed column.

ALTER TABLE ms_employee DROP COLUMN [full_name];

GO

ALTER TABLE ms_employee ADD [full_name]  
       AS ([first_name] + ' ' + [middle_name] + ' ' + [last_name])
1
  • Great!!! Got exact problem, It takes concatenation of other columns & also Explicit Insert in it is NOT ALLOWED. Thank You!
    – Vikrant
    Dec 22, 2014 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.