USE [TARMED]
GO

/****** Object:  StoredProcedure [dbo].[up_LMIN]    Script Date: 12.02.2026 08:58:28 ******/
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO





CREATE OR ALTER procedure [dbo].[up_LMIN]
(
	@ptber	int			-- Tarifbereich
)
AS
BEGIN
	DECLARE @fs1 int, @tber int , @knr varchar(14), @lnr varchar(14), @gvon date, @gbis date, @bezg varchar(255), @lmin int, @i int, @c varchar(1), @s varchar(10)  
	DECLARE @sql nvarchar(500), @par nvarchar(500);	
	BEGIN
		SET @par = N'@ptber tinyint, @pknr varchar(14), @plnr varchar(14), @pgvon date, @pgbis date, @plmin int'   
		SET @sql = N'UPDATE dbo.T_LEISTUNG SET LMIN=@plmin WHERE (BEREICH=@ptber AND KNR=@pknr AND LNR=@plnr AND GVON=@pgvon AND GBIS=@pgbis)'
		DECLARE LC CURSOR FOR 
			SELECT TOP (100) PERCENT BEREICH, KNR, LNR, GVON, GBIS, BEZG FROM dbo.T_LEISTUNG
				WHERE BEREICH=@ptber
		END
		OPEN LC
		FETCH FROM LC INTO @tber, @knr, @lnr, @gvon, @gbis, @bezg 
		SET @fs1 = @@FETCH_STATUS   
		WHILE @fs1 = 0 
			BEGIN
				SET @lmin=-1
				SET @s=''
				SET @i=LEN(@bezg)-1
				WHILE @i > 1 AND SUBSTRING(@bezg,@i,1) <> ',' AND dbo.uf_ISNUMBER(SUBSTRING(@bezg,@i,1)) = 0 
					SET @i=@i-1
				WHILE @i > 1 AND dbo.uf_ISNUMBER(SUBSTRING(@bezg,@i,1)) = 1
					BEGIN
						SET @c=SUBSTRING(@bezg,@i,1)
						SET @s = @c + @s
						SET @i=@i-1
					END
				IF ISNUMERIC(@s)=1 SET @lmin=CONVERT(int,@s)
				EXEC sp_executesql @sql, @par, @ptber=@tber, @pknr=@knr, @plnr=@lnr, @pgvon=@gvon, @pgbis=@gbis, @plmin=@lmin	
				FETCH NEXT FROM LC INTO @tber, @knr, @lnr, @gvon, @gbis, @bezg 
				SET @fs1 = @@FETCH_STATUS 
			END
	CLOSE LC
	DEALLOCATE LC
END

GO

