Combine(Concatenate) two columns to third(PK) on insert query, sql server 2014 -


i using sql server 2014 windows form application,i have columns

id | accountno | location

column details:

id ---> int, not null, auto increment

accountno ---> varchar(50), not null, primary key

location ---> varchar(50), not null

i want run sql insert query enter user input this

| id | accountno | location|

| 10000 | pk10000 | pk |

i thinking query this

string strcommand ="insert customer(id, accountno, location) values (@id, concat(@location, @id [, @accountno]), @location)";

or possible create trigger combine 2 columns @ insert if not possible way accountno pk

suggestions please..

you shouldn't concatenate primary key values. example, duplicate values. it's better create identity column primary key. if need column display purposes, add computed column. example:

create table mytable (     id int not null identity(1, 1) primary key,     location varchar(20) not null,     accountno cast(id varchar) + location ) 

now need insert this:

insert mytable (location) values ('pk') 

this give row these values:

id  location accountno 1   pk       1pk 

you create constraint on computed column enforce unique values accountno:

alter table mytable     add constraint ux_mytable_accountno unique (accountno) 

Comments