SQL Server Custom identity sequence in stored procedure

ruyhziif  于 2023-04-19  发布在  其他
关注(0)|答案(1)|浏览(93)

It's my first time incorporating a custom identity sequence in one of my projects. I have a stored procedure for inserting new records into the database. I have this but not too sure how to incorporate it into my existing stored procedure.

Here's the script that I want to add into my stored procedure:

PatientID AS '23-' + RIGHT('0000000' + CAST(PatientID AS NVARCHAR(3)), 3) PERSISTED

Trying to add into this section of my stored procedure:

@GroundServiceDate NVARCHAR(100),
    @GroundInvoice NVARCHAR(100),
    @GroundPaymentMethod NVARCHAR(50),
    @GroundVoucherId NVARCHAR(100)
AS
BEGIN
    SET NOCOUNT ON;

    PatientID AS '23-' + RIGHT('0000000' + CAST(PatientID AS NVARCHAR(3)),3) PERSISTED

    INSERT INTO tblReferrals (PatientID, ReferralNumber, AssignedReviewer, S
8oomwypt

8oomwypt1#

Why not just create a string variable inside your stored procedure to store that calculation into, and then use it in the INSERT?

DECLARE @patientID NVARCHAR(15);
SET @patientID = 
'23-' + RIGHT('0000000' + CAST(PatientID AS NVARCHAR(3)),3)

INSERT INTO tblReferrals (PatientID, ReferralNumber, AssignedReviewer,...) 
                  VALUES (@patientID,...);

相关问题