DECLARE
  @ESKIKEYNAME nvarchar(200),
  @PROGNAME nvarchar(200),
  @SECTIONSTR nvarchar(200),
  @KEYNAME nvarchar(200),
  @VALUESTR nvarchar(200),
  @SUBEID int

SET @PROGNAME = 'MEDISOFT'
SET @SECTIONSTR = N'EFaturaEnt'
SET @ESKIKEYNAME = N'EArsiv Aktif'

SELECT @VALUESTR = VALUESTR FROM CONFIG
WHERE PROGNAME = @PROGNAME AND SECTIONSTR=@SECTIONSTR AND KEYNAME = @ESKIKEYNAME  

IF @@ROWCOUNT = 1 AND @VALUESTR <> ''''
BEGIN
	DECLARE SUBE_CURSOR CURSOR FOR
	SELECT SUBEID FROM SUBELER

	OPEN SUBE_CURSOR
	FETCH NEXT FROM SUBE_CURSOR INTO @SUBEID
	PRINT CAST(@SUBEID AS NVARCHAR(20))
	WHILE @@FETCH_STATUS = 0
	BEGIN
		PRINT CAST(@SUBEID AS NVARCHAR(20))
		SET @KEYNAME = CAST(@SUBEID AS NVARCHAR(20))+N' Numaralı Şubede E-Arşiv Aktif'
		
		IF NOT EXISTS(SELECT CONFIGID 
					  FROM CONFIG 
					  WHERE PROGNAME = @PROGNAME AND SECTIONSTR = @SECTIONSTR AND KEYNAME = @KEYNAME)
			INSERT INTO [dbo].[CONFIG]([PROGNAME], [SECTIONSTR], [KEYNAME], [VALUESTR])  VALUES( @PROGNAME, @SECTIONSTR, @KEYNAME, @VALUESTR)


		SELECT * FROM CONFIG
		WHERE PROGNAME =	@PROGNAME AND SECTIONSTR=@SECTIONSTR AND KEYNAME = @KEYNAME  
		
		FETCH NEXT FROM SUBE_CURSOR INTO @SUBEID
	END
	CLOSE SUBE_CURSOR
	DEALLOCATE SUBE_CURSOR
END
GO

