How can I generate the next one caught in SQLSERVER when I insert a data?

0

I want to record data of requests in a table, however they asked me to generate a unique code for the request and that it will take the structure that I show in the following image:

I have absolutely no idea how to do it, if someone can help me I would really appreciate it. I'm working with SQLSERVER2014

    
asked by Sergio Cordova 23.11.2018 в 05:32
source

1 answer

0

You must do it in 2 steps.

First add the record, but without adding it to the IDID field.

INSERT INTO X (...) VALUES (...)

Then update the field for that record

UPDATE X SET IDSolicitud=CONCAT('SAC-',SCOPE_IDENTITY(),'-',YEAR(getdate()) WHERE id=SCOPE_IDENTITY()
    
answered by 23.11.2018 в 10:36