1

I know this question has been asked so many times. But I googled a lot and also went through the answers over here but couldn't able to understand why my stored Procedure is giving this error repeatedly since little while ago the same code was perfectly working fine.

Please any one can help my identify that why this error is coming. Also Even if when this was working I was not getting my desired output as I want to generate the order numbers in sequence but I'm getting the same number as the number of count.

USE [Sost_Dev]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [sost].[GetExternalOrderNumbers]
    @Count int
AS
BEGIN
    SET NOCOUNT ON;
DECLARE @year VARCHAR(2)
DECLARE @ExternalOrderNumbers nvarchar(50)

SET @year = RIGHT(CAST(DATEPART(yy, GETUTCDATE()) AS VARCHAR(4)),2)

    -- Insert statements for procedure here
BEGIN TRAN
SAVE TRAN seq

            --Creating Temp Table for every isolated connection
            CREATE TABLE #NewOrders(ExtOrderNumber nvarchar(50));

            WHILE @Count > 0
                BEGIN
                   INSERT INTO [sost].ServiceOrderNumberSequence DEFAULT VALUES
                    SET @ExternalOrderNumbers = 'ESON' + @year + RIGHT(REPLICATE('0',10) + CAST(SCOPE_IDENTITY() AS  NVARCHAR(7)) ,7);
                    print 'External' + @ExternalOrderNumbers;
                    --Insert New Order Number to Temporary Tables
                   INSERT INTO #NewOrders  values(@ExternalOrderNumbers)
                   SET @Count -=1
                END

COMMIT

--Fetching Order Numbers from Temporary Table.

SELECT @ExternalOrderNumbers as ExternalOrderNumber from #NewOrders
END

Please see the below image for the error in my SQLServer enter image description here

enter image description here

Please find the result All the order numbers are same but I want in consecutive manner. enter image description here

5
  • 1
    print SCOPE_IDENTITY() result, it might be giving output greater than 7 digits. Commented Mar 5, 2020 at 9:27
  • yes correct.. It was giving output of add digits. In that case can you please tell me how to restrict it 7 only Commented Mar 5, 2020 at 9:41
  • USE right(SCOPE_IDENTITY(),7) or left(SCOPE_IDENTITY(),7) function according to your need. Commented Mar 5, 2020 at 9:43
  • I'm already using right function but still I got this error Commented Mar 5, 2020 at 9:52
  • But you are using right after casting, hence you got the error. Commented Mar 5, 2020 at 9:54

1 Answer 1

2

Just change this line

SET @ExternalOrderNumbers = 'ESON' + @year + RIGHT(REPLICATE('0',10) + CAST(SCOPE_IDENTITY() AS  NVARCHAR(100)) ,7);

Edit: AS per your requirement.

SET @ExternalOrderNumbers = 'ESON' + @year + RIGHT(REPLICATE('0',10) + CAST(RIGHT(SCOPE_IDENTITY(),7) AS  NVARCHAR(7)) ,7);
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks @Atk It solved my error. Can you please address my second problem also. It would be very much help for me.
run this query and share the output so that i can help you @KarthikSaxena
Please check now.
Still I'm getting the repeated values like I showed in my edited question
@KarthikSaxena Share schema of your table [sost].ServiceOrderNumberSequence

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.