IF  EXISTS (SELECT * FROM sys.procedures WHERE object_id = OBJECT_ID(N'[dbo].[P_MATCHING_ITEXT_Q1B]'))
	DROP PROCEDURE [dbo].[P_MATCHING_ITEXT_Q1B]
GO
CREATE PROCEDURE dbo.P_MATCHING_ITEXT_Q1B  
   @language_i_text varchar(max)
AS 
   BEGIN

      IF @language_i_text IS NOT NULL
         BEGIN

            DECLARE
               @rec$I_NR float(53), 
               @rec$rowtype_field$1 varchar(max), 
               @rec$rowtype_field$2 varchar(4000), 
               @rec$DT varchar(max), 
               @rec$DI varchar(max), 
               @rec$DS_ID varchar(max), 
               @rec$rowtype_field$3 datetime, 
               @rec$rowtype_field$4 varchar(max)

            DECLARE
                I_Texte CURSOR LOCAL FORWARD_ONLY FOR 
                  SELECT 
                     DT037.DI025487 AS I_NR, 
                     @language_i_text, 
                     NULL, 
                     DT037.DI025502 AS DT, 
                     DT037.DI025503 AS DI, 
                     DT037.DI025504 AS DS_ID, 
                     getdate(), 
                     '-'
                  FROM dbo.DT037
                  WHERE DT037.DI025488 = 'DE' AND DT037.DI025487 NOT IN 
                     (
                        SELECT DT037.DI025487
                        FROM dbo.DT037
                        WHERE DT037.DI025488 = @language_i_text
                     )

            OPEN I_Texte

            WHILE 1 = 1
            
               BEGIN

                  FETCH I_Texte
                      INTO 
                        @rec$I_NR, 
                        @rec$rowtype_field$1, 
                        @rec$rowtype_field$2, 
                        @rec$DT, 
                        @rec$DI, 
                        @rec$DS_ID, 
                        @rec$rowtype_field$3, 
                        @rec$rowtype_field$4

                  IF @@FETCH_STATUS = -1
                     BREAK

                  INSERT dbo.DT037(
                     DI025487/*AS I_NR,*/, 
                     DI025488/*AS LANG,*/, 
                     DI025501/*AS I_TEXT,*/, 
                     DI025502/*AS DT,*/, 
                     DI025503/*AS DI,*/, 
                     DI025504/*AS DS_ID*/, 
                     DI025491/*angelegt am*/, 
                     DI025494/*angelegt von*/)
                     VALUES (
                        @rec$I_NR, 
                        @language_i_text, 
                        NULL, 
                        @rec$DT, 
                        @rec$DI, 
                        @rec$DS_ID, 
                        getdate(), 
                        '-')

                  PRINT 'I-Text fuer Sprache ' + ISNULL(@language_i_text, '') + ' angelegt. I-Nr: ' + ISNULL(CAST(@rec$I_NR AS nvarchar(max)), '')

               END

            CLOSE I_Texte

            DEALLOCATE I_Texte

         END

      IF @@TRANCOUNT > 0
         COMMIT WORK 

   END
GO
