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

CREATE PROCEDURE [dbo].KDS_VERI_GUNCELLE
 @tar1 as NVARCHAR(30),
@tar2 as NVARCHAR(30),
@subeid as nvarchar(10)

AS
SET NOCOUNT ON
SET ANSI_WARNINGS OFF

DECLARE @sqlstr as nvarchar(1000), @KOD as NVARCHAR(30)

--set @tar1 = '2019-03-01 00:00:00.000';
--set @tar2 = '2019-03-31 00:00:00.000';

DECLARE  TKLINIKKALITE_Cursor CURSOR FOR SELECT KLINIKKALITEKOD FROM TKLINIKKALITE
OPEN TKLINIKKALITE_Cursor

FETCH NEXT FROM TKLINIKKALITE_Cursor INTO @KOD
WHILE @@FETCH_STATUS = 0
BEGIN
   set @sqlstr='UPDATE UK SET UK.KLINIKKALITESAYI = KDSREVIZE.KLINIKKALITESAYI FROM USSGUNSONUGONDERIM US
	  	INNER JOIN USSGUNSONUGONDERIMKLINIKKALITE UK ON US.USSGUNSONUGONDERIMID=UK.USSGUNSONUGONDERIMID
		INNER JOIN KDSREVIZE ON US.TARIH=KDSREVIZE.TARIH
		WHERE US.SUBEID='+@subeid+' AND US.TARIH>='''+@tar1+''' AND US.TARIH<='''+@tar2+''' AND UK.KLINIKKALITEKOD='+@KOD+' AND KDSREVIZE.KLINIKKALITEKOD='+@KOD+'' ;
		--set @sqlstr = 'PRINT '+@KLINIKKALITEKOD

   EXECUTE(@sqlstr) 
   PRINT @KOD
   PRINT @sqlstr;

	FETCH NEXT FROM TKLINIKKALITE_Cursor INTO @KOD
END

CLOSE TKLINIKKALITE_Cursor
DEALLOCATE TKLINIKKALITE_Cursor


