IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPUSS_258_COCUK_DIYABET_IZLEM_KAYIT]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPUSS_258_COCUK_DIYABET_IZLEM_KAYIT
GO

CREATE PROCEDURE [dbo].[SPUSS_258_COCUK_DIYABET_IZLEM_KAYIT] 
	@HDOSYAID INT=0,
	@HMUAYENEID INT=0,
	@TUMUNUTEKRAR BIT = 0
	 	 
AS
BEGIN
	--SPUSS_258_COCUK_DIYABET_IZLEM_KAYIT 165788,115370,1
	DECLARE @HATA NVARCHAR(2000) = ''
	DECLARE @MESAJ NVARCHAR(2000) = ''
	DECLARE @SAYIHATA BIT = 0
	DECLARE @XMLBASLIK NVARCHAR(2000) = ''
	DECLARE @SUBEID INT = 1
	DECLARE @goNDERIMTABLOSU TABLE 
		(
			goNDERILECEKXML XML,
			HATA NVARCHAR(2000),
			MESAJ NVARCHAR(2000),	
			XMLBASLIK NVARCHAR(3500),
			TABLOID INT,
			MINID INT,
			MAXID INT
		)
	
	IF OBJECT_ID('tempdb..#TMPUSSDIGER') IS NOT NULL 
	DROP TABLE #TMPUSSDIGER


SELECT 
 USS258.ID USSDIGER_ID
,USS258.HDOSYAID
,USS258.HMUAYENEID
,USS258.HKIMLIKID
,USS258.HYATISID,

USS258.TANNER_EVRE AS TANNER_EVRE_ID,
TTANNER_EVRE.AD AS TANNER_EVRE_ADI,

USS258.DIYABET_KOMPLIKASYONLARI AS DIYABET_KOMPLIKASYONLARI_ID,
TDIYABET_KOMPLIKASYONLARI.AD AS DIYABET_KOMPLIKASYONLARI_ADI,

USS258.TIROID_MUAYENESI AS TIROID_MUAYENESI_ID,
TTIROID_MUAYENESI.AD AS TIROID_MUAYENESI_ADI,

USS258.DIYABET_TIPI AS DIYABET_TIPI_ID,
TDIYABET_TIPI.AD AS DIYABET_TIPI_ADI,

(SELECT TOP 1 ICDKOD FROM HDOSYAICD WHERE HDOSYAICD.HDOSYAID=HDOSYA.HDOSYAID ORDER BY ANATANI DESC) AS BIRLIKTE_GORULEN_EK_HASTALIKLAR_ID,
(SELECT TOP 1 (SELECT TICD.ICDADI FROM TICD WHERE TICD.ICDKOD=HDOSYAICD.ICDKOD) FROM HDOSYAICD WHERE HDOSYAICD.HDOSYAID=HDOSYA.HDOSYAID ORDER BY ANATANI DESC) AS BIRLIKTE_GORULEN_EK_HASTALIKLAR_ADI,

USS258.ESLIK_EDEN_BASKA_HASTALIK AS ESLIK_EDEN_BASKA_HASTALIK_ID,
TESLIK_EDEN_BASKA_HASTALIK.AD AS ESLIK_EDEN_BASKA_HASTALIK_ADI,

USS258.DIYABET_ILAC_TEDAVI_SEKLI AS DIYABET_ILAC_TEDAVI_SEKLI_ID,
TDIYABET_ILAC_TEDAVI_SEKLI.AD AS DIYABET_ILAC_TEDAVI_SEKLI_ADI,

USS258.DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI AS DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI_ID,
TDIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI.AD AS DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI_ADI,

BOY AS BOY,
KILO AS KILO,

HDOSYA.SUBEID HDOSYA_SUBEID
	  ,SUBELER.SUBEADI SUBELER_SUBEADI
	  ,HDOSYA.SNOTAKIPNO HDOSYA_SNOTAKIPNO
	  ,		 ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,HDOSYA.SUBEID)+' numarali subede Yenidoğanların Kimliksiz fatura edilebilir gün sayısı'),'') 	AS SAGLIKNETKIMLIKSIZYENIDOGANGUN,

		CASE WHEN (SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND /* VALUESTR='1' AND*/ KEYNAME LIKE CONVERT(VARCHAR,HDOSYA.SUBEID)+' numaral% %ubede Sa%l%k Net Online test modu aktif')='1' THEN
			'10000038' 
		ELSE
			ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,HDOSYA.SUBEID)+' numaralı şubenin kurum numarası'),'') 
		END AS SAGLIKNETKURUMKODU,

			CASE WHEN (SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND /* VALUESTR='1' AND*/ KEYNAME LIKE CONVERT(VARCHAR,HDOSYA.SUBEID)+' numaral% %ubede Sa%l%k Net Online test modu aktif')='1' THEN
			'3bc26a7d-4a19-4edf-ab9f-1413ac85e7d0' /*TEST KTS FİRMA KODU*/
		ELSE
			'C740D0288ED4C45FE0407C0A04162BDD'  /*KTS TALYA*/ 
		END AS SNOKTSFIRMAKODU,

		ISNULL((SELECT TOP 1 VALUESTR FROM CONFIG WITH(NOLOCK) WHERE SECTIONSTR='Saglik-Net' AND PROGNAME='MEDISOFT' AND KEYNAME= CONVERT(VARCHAR,HDOSYA.SUBEID)+' numarali subede Hasta kayıt gönderiminde referans no ön Karakteri'),'') AS REFNOONEK

INTO #TMPUSSDIGER 

FROM USS258_COCUK_DIYABET_IZLEM AS USS258
INNER JOIN HDOSYA ON HDOSYA.HDOSYAID = USS258.HDOSYAID
LEFT JOIN HKIMLIK ON HKIMLIK.HKIMLIKID=HDOSYA.HKIMLIKID
LEFT JOIN	SUBELER ON SUBELER.SUBEID=HDOSYA.SUBEID

LEFT JOIN 
(
SELECT 'EVRE-1' AS AD, 1 AS ID
UNION ALL
SELECT 'EVRE-2', 2
UNION ALL
SELECT 'EVRE-3', 3
UNION ALL
SELECT 'EVRE-4', 4
UNION ALL
SELECT 'EVRE-5', 5
) TTANNER_EVRE ON TTANNER_EVRE.ID=USS258.TANNER_EVRE

LEFT JOIN 
(
SELECT 'ACANTHOSİS NİGRİCANS' AS AD, 1 AS ID
UNION ALL
SELECT 'DİYABETİK AYAK (AMPUTASYON)', 2
UNION ALL
SELECT 'DİYABETİK AYAK (KANGREN)', 3
UNION ALL
SELECT 'DİYABETİK AYAK (NASIR)', 4
UNION ALL
SELECT 'DİYABETİK AYAK (OSTEOMYELİT)', 5
UNION ALL
SELECT 'DİYABETİK AYAK (TIRNAK BATMASI)', 6
UNION ALL
SELECT 'DİYABETİK AYAK (YÜZEYSEL ÜLSER)', 7
UNION ALL
SELECT 'DİYABETİK DERMOPATİ', 8
UNION ALL
SELECT 'GLOKOM', 9
UNION ALL
SELECT 'KATARAKT', 10
UNION ALL
SELECT 'KORONER ARTER HASTALIĞI', 11
UNION ALL
SELECT 'LİPOATROFİ', 12
UNION ALL
SELECT 'LİPOHİPERTROFİ', 13
UNION ALL
SELECT 'MAKÜLA ÖDEMİ', 14
UNION ALL
SELECT 'NECROBİASİS LİPOİDİCO DİABETİCORUM', 15
UNION ALL
SELECT 'NON-PROLİFERATİF RETİNOPATİ', 16
UNION ALL
SELECT 'OTONOM NÖROPATİ', 17
UNION ALL
SELECT 'PERİFERİK ARTER HASTALIĞI', 18
UNION ALL
SELECT 'PERİFERİK POLİNÖROPATİ', 19
UNION ALL
SELECT 'PRE-PROLİFERATİF RETİNOPATİ', 20
UNION ALL
SELECT 'PROLİFERATİF RETİNOPATİ', 21
UNION ALL
SELECT 'SEREBROVASKÜLER HASTALIK', 22
UNION ALL
SELECT 'PERSISTAN MIKROALBUMINÜRI', 23
UNION ALL
SELECT 'DIYABETIK NEFROPATI', 24
UNION ALL
SELECT 'FOKAL NÖROPATI (MONONÖROPOATI-KARPAL TÜNEL GIBI)', 25
UNION ALL
SELECT 'MIKROANEVRIZMAHEMORAJI', 26
UNION ALL
SELECT 'SERT VE YUMUŞAK EKSUDA', 27
UNION ALL
SELECT 'İNTRA RETINAL MIKROVASKÜLER ANORMALLIKLER', 28
UNION ALL
SELECT 'YOK', 97
UNION ALL
SELECT 'DİĞER', 98
UNION ALL
SELECT 'BELİRTİLMEDİ', 99
) TDIYABET_KOMPLIKASYONLARI ON TDIYABET_KOMPLIKASYONLARI.ID=USS258.DIYABET_KOMPLIKASYONLARI

LEFT JOIN
(
SELECT 'TİROİD ELE GELMİYOR' AS AD, 1 AS ID
UNION ALL
SELECT 'TİROİD NODÜLER ELE GELİYOR', 2
UNION ALL
SELECT 'TİROİD DİFFÜZ ELE GELİYOR', 3
UNION ALL
SELECT 'BELİRTİLMEDİ', 99
) TTIROID_MUAYENESI ON TTIROID_MUAYENESI.ID = USS258.TIROID_MUAYENESI

LEFT JOIN
(
SELECT 'TIP-1' AS AD, 1 AS ID
UNION ALL
SELECT 'TIP-2', 2
UNION ALL
SELECT 'MODY-5', 3
UNION ALL
SELECT 'DIĞER', 4
) TDIYABET_TIPI ON TDIYABET_TIPI.ID = USS258.DIYABET_TIPI

LEFT JOIN
(
SELECT 'ÇÖLYAK' AS AD, 1 AS ID
UNION ALL
SELECT 'HASHIMATO TIROIDITI', 2
UNION ALL
SELECT 'GRAVES HASTALIĞI', 3
UNION ALL
SELECT 'YOK', 9
) TESLIK_EDEN_BASKA_HASTALIK ON TESLIK_EDEN_BASKA_HASTALIK.ID = USS258.ESLIK_EDEN_BASKA_HASTALIK

LEFT JOIN
(
SELECT 'İNSÜLİN' AS AD, 1 AS ID
UNION ALL
SELECT 'ORAL ANTİDİYABETİK', 2
UNION ALL
SELECT 'İLAÇLI TEDAVİ VERİLMEDİ', 3
UNION ALL
SELECT 'KONVANSIYONEL', 4
UNION ALL
SELECT 'POMPA', 5
) TDIYABET_ILAC_TEDAVI_SEKLI ON TDIYABET_ILAC_TEDAVI_SEKLI.ID = USS258.DIYABET_ILAC_TEDAVI_SEKLI


LEFT JOIN
(
SELECT 'HASTA DİYETİNE UYMUYOR' AS AD, 1 AS ID
UNION ALL
SELECT 'HASTA DİYETİNE UYUYOR', 2
UNION ALL
SELECT 'DEĞIŞIM LISTESI', 3
UNION ALL
SELECT 'KARBONHIRDAT SAYIMI', 4
UNION ALL
SELECT 'YOK', 97
) TDIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI ON TDIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI.ID = USS258.DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI

WHERE USS258.HMUAYENEID= @HMUAYENEID AND USS258.HDOSYAID = @HDOSYAID 


	IF (SELECT COUNT(*) FROM #TMPUSSDIGER) = 0 
	BEGIN
		SET @SAYIHATA = 1
		SET @MESAJ = @MESAJ + 'Gönderilecek kayıt bulunamadı! '
	END

	IF (SELECT COUNT(*) FROM #TMPUSSDIGER WHERE HDOSYA_SUBEID IS NULL) > 0 
		SET @HATA = @HATA + 'dosyasının şube kaydı bulunamadı! '

	IF (SELECT COUNT(*) FROM #TMPUSSDIGER WHERE ISNULL(HDOSYA_SNOTAKIPNO,'')='') > 0 
		SET @HATA = @HATA + 'Sağlık Net hasta takip numarası alınmadan USS sistemine kayIT yapılamaz. '

   	IF @HATA='' AND @SAYIHATA=0
	BEGIN		
		SELECT @SUBEID = HDOSYA_SUBEID FROM #TMPUSSDIGER
		DECLARE @TBLXMLBASLIK TABLE (XMLBASLIK NVARCHAR(2000), HATA NVARCHAR(2000),MESAJ NVARCHAR(2000))
		INSERT INTO @TBLXMLBASLIK (XMLBASLIK, HATA, MESAJ) EXEC SPUSS_000_XML_BASLIK @SUBEID		
		SELECT @HATA=HATA, @MESAJ=MESAJ, @XMLBASLIK=XMLBASLIK FROM @TBLXMLBASLIK		
	END



	IF @HATA='' AND @SAYIHATA=0
	BEGIN


		--HER TETKIK KARTI AYRI AYRI goNDER DONGUSU
   		DECLARE @CURUSSDIGERID INT
		DECLARE CURUSSDIGER CURSOR FOR SELECT USSDIGER_ID FROM #TMPUSSDIGER
		OPEN CURUSSDIGER
		FETCH NEXT FROM CURUSSDIGER INTO @CURUSSDIGERID
		WHILE @@FETCH_STATUS =0
		BEGIN
		

	
					SET @HATA = ''
					SET @MESAJ = ''
					SET @SAYIHATA = 0

		
					 
					IF EXISTS(SELECT USSDIGER_ID FROM #TMPUSSDIGER WHERE  ISNULL(USSDIGER_ID,0)=0)
						SET @HATA = @HATA + ' ' + 'Kayıt seçilmemiş. '


					IF (SELECT COUNT(*) FROM #TMPUSSDIGER WHERE USSDIGER_ID=@CURUSSDIGERID) > 0
					BEGIN
						IF @HATA = '' AND @SAYIHATA=0
						BEGIN
							BEGIN TRY 
								INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
								SELECT							 
									(
									
										SELECT 
										   NEWID() AS 'messageGuid/@value',
										   '1' AS 'messageType/@version',
										   '0a9ba485-e7e0-4abb-9c86-0a14fd364bb8' as 'messageType/@codeSystemGuid',
										   '258' AS 'messageType/@code',
										   'Çocuk Diyabet İzlem' AS 'messageType/@value',
										   LEFT(REPLACE(REPLACE(REPLACE(CONVERT(VARCHAR(19),GETDATE(),120),' ',''),'-',''),':',''),12) as 'documentGenerationTime/@value',
										   (
											  SELECT	
												 '1' AS 'healthcareProvider/@version',
												 'c3eade04-4f91-5dab-e043-14031b0ac9f9' as 'healthcareProvider/@codeSystemGuid',
												 SAGLIKNETKURUMKODU as 'healthcareProvider/@code',
												 SUBELER_SUBEADI as 'healthcareProvider/@value'  
												 FOR XML PATH('author'), TYPE),
												 --'C740D0288ED4C45FE0407C0A04162BDD' as 'firmaKodu/@value' ,
												 SNOKTSFIRMAKODU as 'firmaKodu/@value' ,
												 (
													SELECT 
														(
														   SELECT 
															  (
																 SELECT 
																   
																		-- <TANNER_EVRE version="1" codeSystemGuid="8aff5e1b-81ac-4354-9fdf-ce206a7c4f13" code="Code" value="TANNER_EVRE" />
																		(
																				SELECT
																					'1' AS '@version',
																					'8aff5e1b-81ac-4354-9fdf-ce206a7c4f13' AS '@codeSystemGuid',
																					TANNER_EVRE_ID AS '@code',
																					TANNER_EVRE_ADI AS '@value'
																				WHERE TANNER_EVRE_ID IS NOT NULL
																				FOR XML PATH('TANNER_EVRE'), TYPE
																			),
																		
																		
																		-- <DIYABET_KOMPLIKASYONLARI version="1" codeSystemGuid="0d4c699c-06ca-4983-b3b5-617dd5def8e6" code="Code" value="DIYABET_KOMPLIKASYONLARI" />
																		(
																			SELECT
																				'1' AS '@version',
																				'0d4c699c-06ca-4983-b3b5-617dd5def8e6' AS '@codeSystemGuid',
																				DIYABET_KOMPLIKASYONLARI_ID  AS '@code',
																				DIYABET_KOMPLIKASYONLARI_ADI AS '@value'
																			WHERE DIYABET_KOMPLIKASYONLARI_ID IS NOT NULL
																			FOR XML PATH('DIYABET_KOMPLIKASYONLARI'), TYPE
																		),

																		-- <TIROID_MUAYENESI version="1" codeSystemGuid="1683b32e-0668-4c28-97b7-8d3245ae2f4d" code="Code" value="TIROID_MUAYENESI" />
																		(
																			SELECT
																				'1' AS '@version',
																				'1683b32e-0668-4c28-97b7-8d3245ae2f4d' AS '@codeSystemGuid',
																				TIROID_MUAYENESI_ID  AS '@code',
																				TIROID_MUAYENESI_ADI AS '@value'
																			WHERE TIROID_MUAYENESI_ID IS NOT NULL
																			FOR XML PATH('TIROID_MUAYENESI'), TYPE
																		),

																		-- <DIYABET_TIPI version="1" codeSystemGuid="35ae0624-4f4d-4ac0-886e-2687f017135b" code="Code" value="DIYABET_TIPI" />
																		(
																			SELECT
																				'1' AS '@version',
																				'35ae0624-4f4d-4ac0-886e-2687f017135b' AS '@codeSystemGuid',
																				DIYABET_TIPI_ID  AS '@code',
																				DIYABET_TIPI_ADI AS '@value'
																			WHERE DIYABET_TIPI_ID IS NOT NULL
																			FOR XML PATH('DIYABET_TIPI'), TYPE
																		),

																		-- <ESLIK_EDEN_BASKA_HASTALIK version="1" codeSystemGuid="94f19f91-44ed-4bf7-beed-de14090172d9" code="Code" value="ESLIK_EDEN_BASKA_HASTALIK" />
																		(
																			SELECT
																				'1' AS '@version',
																				'94f19f91-44ed-4bf7-beed-de14090172d9' AS '@codeSystemGuid',
																				ESLIK_EDEN_BASKA_HASTALIK_ID  AS '@code',
																				ESLIK_EDEN_BASKA_HASTALIK_ADI AS '@value'
																			WHERE ESLIK_EDEN_BASKA_HASTALIK_ID IS NOT NULL
																			FOR XML PATH('ESLIK_EDEN_BASKA_HASTALIK'), TYPE
																		),

																		-- <DIYABET_ILAC_TEDAVI_SEKLI version="1" codeSystemGuid="72f8fe9d-cdfe-4d8d-b154-66e0e706f06a" code="Code" value="DIYABET_ILAC_TEDAVI_SEKLI" />
																		(
																			SELECT
																				'1' AS '@version',
																				'72f8fe9d-cdfe-4d8d-b154-66e0e706f06a' AS '@codeSystemGuid',
																				DIYABET_ILAC_TEDAVI_SEKLI_ID  AS '@code',
																				DIYABET_ILAC_TEDAVI_SEKLI_ADI AS '@value'
																			WHERE DIYABET_ILAC_TEDAVI_SEKLI_ID IS NOT NULL
																			FOR XML PATH('DIYABET_ILAC_TEDAVI_SEKLI'), TYPE
																		),

																		-- <DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI version="1" codeSystemGuid="8c0e3f06-e381-4415-9446-a6251d655f91" code="Code" value="DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI" />
																		(
																			SELECT
																				'1' AS '@version',
																				'8c0e3f06-e381-4415-9446-a6251d655f91' AS '@codeSystemGuid',
																				DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI_ID  AS '@code',
																				DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI_ADI AS '@value'
																			WHERE DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI_ID IS NOT NULL
																			FOR XML PATH('DIYET_TEDAVISI_TIBBI_BESLENME_TEDAVISI'), TYPE
																		),


																		 --   <BOY_KILO_BILGILERI>
																		 --       <BOY value="BOY" />
																		 --       <KILO value="KILO" />
																		 --   </BOY_KILO_BILGILERI>
																		(SELECT 
																		
																		ISNULL(BOY,'')  AS 'BOY/@value',
																		ISNULL(KILO,'') AS 'KILO/@value'																	
																		
																		FOR XML PATH('BOY_KILO_BILGILERI'),TYPE)																	
																		
 		
																FROM #TMPUSSDIGER
																
																FOR XML PATH(''),TYPE
															)
														FOR XML PATH('COCUK_DIYABET_IZLEM'),TYPE
													),					
													   (
														SELECT ISNULL(HDOSYA_SNOTAKIPNO, '' ) AS 'SYSTakipNo/@value'
														FOR XML PATH('HASTA_TAKIP_BILGISI'), TYPE 
													)							
												 FOR XML PATH('recordData'), TYPE
											  )
										   FROM #TMPUSSDIGER WITH(NOLOCK)
										   WHERE USSDIGER_ID = @CURUSSDIGERID
										FOR XML PATH (''), ROOT('SYSMessage')


									) AS goNDERILECEKXML,
									@HATA AS HATA, 
									@MESAJ AS MESAJ,
									@XMLBASLIK AS XMLBASLIK,
									@CURUSSDIGERID AS TABLOID,
									0 AS MINID,
									0 AS MAXID

							END TRY
							BEGIN CATCH
								SET @HATA = ERROR_MESSAGE()
								IF @HATA = ''
									SET @HATA = 'Bilinmeyen bir hata oluştu'

								INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
								SELECT	'' AS goNDERILECEKXML,
										@HATA AS HATA, 
										@MESAJ AS MESAJ,
										'' AS XMLBASLIK,
										@CURUSSDIGERID AS TABLOID,
										0 AS MINID,
										0 AS MAXID
			

							END CATCH
						END
						ELSE
						BEGIN

							INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
							SELECT	'' AS goNDERILECEKXML,
									LEFT(@HATA,2000) AS HATA, 
									@MESAJ AS MESAJ,
									'' AS XMLBASLIK,
									@CURUSSDIGERID AS TABLOID,
									0 AS MINID,
									0 AS MAXID

							SET @HATA = ''		 
						END
					END




		FETCH NEXT FROM CURUSSDIGER INTO @CURUSSDIGERID 
		END --son HER TETKIK KARTI AYRI AYRI goNDER DONGUSU
	

		CLOSE CURUSSDIGER
		DEALLOCATE CURUSSDIGER

		IF (SELECT COUNT(*) FROM @GONDERIMTABLOSU)=0
			INSERT INTO @GONDERIMTABLOSU (GONDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
			SELECT	'' AS GONDERILECEKXML,
					'' AS HATA, 
					'Tüm KAYITLAR zaten gönderilmiş' AS MESAJ,
					'' AS XMLBASLIK,
					0 AS TABLOID,
					0 AS MINID,
					0 AS MAXID


	END
	ELSE
	BEGIN

		INSERT INTO @goNDERIMTABLOSU (goNDERILECEKXML,HATA,MESAJ,XMLBASLIK,TABLOID,MINID,MAXID)
		SELECT	'' AS goNDERILECEKXML,
				LEFT(@HATA,2000) AS HATA, 
				@MESAJ AS MESAJ,
				'' AS XMLBASLIK,
				@CURUSSDIGERID AS TABLOID,
				0 AS MINID,
				0 AS MAXID

		SET @HATA = ''		 
	END


	
	IF OBJECT_ID('tempdb..#TMPUSSDIGER') IS NOT NULL 
	DROP TABLE #TMPUSSDIGER

	

	SELECT * FROM @goNDERIMTABLOSU


END
