Napisalem sobie taka rocedure
Chce ddodac rok,ale dla kazdego roku liczyc od nowa. 0000-0000-0001/1947 itd. Czy procedure mozna latwiej napisac? Poz JB.SQLserver 2014. Sprawdzone w petli na 300000 w vb.net 2012.
USE [FINANSE]
GO
/****** Object: StoredProcedure [dbo].[uspAAANrNaTrzyPolaEndNoKoniec] Script Date: 11.10.2023 16:37:03 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
-- =============================================
-- Author: Jerzy Bierniak/47
-- Create date:Dzisiaj
-- Description: poz
-- =============================================
ALTER PROCEDURE [dbo].[uspAAANrNaTrzyPolaEndNoKoniec]
--DECLARE
@sts1 as nvarchar(4)= NULL,
@sts2 as nvarchar(4) = NULL,
@sts3 as nvarchar(4) = NULL,
@Nr as nvarchar(14) = null,
@id as int = null
AS
BEGIN
SET NOCOUNT ON
SET @Nr = '9999-9999-9999'
SET @sts1 = (Select Right(@Nr,4))
SET @sts2 = (Select LEFT(Right(@Nr,9),4))
SET @sts3 = (Select LEFT(@Nr,4))
IF @sts1 = 9999 AND @sts2 = 9999 AND @sts3 = 9999
BEGIN
SET @Nr ='EndNo'
SELECT @Nr
END
Else
BEGIN
IF @sts1 = 9999 AND @sts2 = 9999
BEGIN
SET @sts3 = Format(@sts3+1,'0000')
SET @sts1 = '0000'
SET @sts2 ='0000'
END
ELSE
BEGIN
SET @sts2= (SELECT FORMAT( IIF(@sts1 = 9999,@sts2 +1, @sts2 ),'0000'))
SET @sts1=(SELECT FORMAT(IIF(@sts1 < 9999, @sts1+1,0000),'0000'))
END
SET @Nr = (SELECT @sts3+'/'+ @sts2+'/'+ @sts1)
SELECT @Nr
End
END