IF  EXISTS (SELECT * FROM dbo.sysobjects WHERE id = OBJECT_ID(N'[dbo].[SPWEB_LOGLA]') AND OBJECTPROPERTY(id,N'IsProcedure') = 1)
DROP PROCEDURE SPWEB_LOGLA
GO
--drop table MEDISOFTARSIV.dbo.WEBLOG ; drop table WEBLOG 
--SPWEB_LOGLA  'login', 5555, 'Mahmut Demir Giriş Yaptı','192.168.1.1'
CREATE PROCEDURE [dbo].[SPWEB_LOGLA]  @islem NVarChar(20), @islemid INT, @islemlog NVarChar(500), @islemip NVarChar(20)
AS
BEGIN
  DECLARE @ARSIVVERITABANI VARCHAR(20) = ''  
  SET @ARSIVVERITABANI=ISNULL((SELECT VALUESTR FROM CONFIG WITH(NOLOCK) WHERE PROGNAME='MEDISOFT' AND SECTIONSTR='Sistem' AND  KEYNAME='Arsiv veri tabani'),'')
  IF @ARSIVVERITABANI<>'' 
	SET @ARSIVVERITABANI = @ARSIVVERITABANI+'.DBO.'

  DECLARE @sqlQuery nVarchar(2000) = ''

  set @sqlQuery = '
	IF NOT EXISTS (SELECT * FROM  '+@ARSIVVERITABANI+'SYSOBJECTS WITH(NOLOCK) WHERE SYSOBJECTS.name = ''WEBLOG'')
		BEGIN
			CREATE TABLE '+@ARSIVVERITABANI+'[WEBLOG](
				[ID] [int] IDENTITY(1,1) NOT NULL,
				[ISLEM] [nvarchar](20) NULL,
				[ISLEMID] [int] NULL,
				[ISLEMLOG] [nvarchar](500) NULL,
				[ISLEMIP] [nvarchar](20) NULL,
				[ISLEMTARIH] [datetime] NULL,
			 CONSTRAINT [PK_WEBLOG] PRIMARY KEY CLUSTERED 
			(
				[ID] ASC
			)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
			) ON [PRIMARY]
			
		END

	IF NOT EXISTS (SELECT * FROM '+REPLACE(@ARSIVVERITABANI,'DBO.','')+'sys.indexes WITH(NOLOCK) WHERE name = N''IX_WEBLOG_ISLEMID_ISLEM'')
	BEGIN
		CREATE NONCLUSTERED INDEX [IX_WEBLOG_ISLEMID_ISLEM] ON '+@ARSIVVERITABANI+'[WEBLOG]
			(
				ISLEMID ASC,ISLEM ASC
			) WITH (FILLFACTOR = 70) ON [PRIMARY]
	END
	'
	EXEC (@sqlQuery)

	DECLARE @LOGLANSIN VARCHAR(1) = ISNULL((SELECT VALUESTR FROM CONFIG WITH(NOLOCK) WHERE PROGNAME='MEDISOFT' AND SECTIONSTR='Sistem' AND  KEYNAME='MediWeb islemleri loglansin'),'9')

	IF @LOGLANSIN = '9'
	BEGIN
		INSERT INTO [CONFIG] (PROGNAME,SECTIONSTR,KEYNAME,VALUESTR) VALUES ('MEDISOFT','Sistem','MediWeb islemleri loglansin','1')
		SET @LOGLANSIN = '1'
	END

	IF @LOGLANSIN='1'
	BEGIN
		set @sqlQuery = 'INSERT INTO '+@ARSIVVERITABANI+'[WEBLOG] (ISLEM,ISLEMID,ISLEMLOG,ISLEMIP,ISLEMTARIH) VALUES (@islem,@islemid,@islemlog,@islemip,GETDATE())'
		EXECUTE sp_executesql @sqlQuery, N'@islem NVarChar(20), @islemid int, @islemlog NVarChar(500), @islemip NVarChar(20)', @islem, @islemid, @islemlog, @islemip
	END
END
GO

IF EXISTS (SELECT [name] FROM [sys].[database_principals] WHERE [type] = N'S' AND [name] = N'mediwebuser')
BEGIN
	BEGIN TRY
		GRANT EXECUTE ON [dbo].[SPWEB_LOGLA] TO [mediwebuser]
	END TRY
	BEGIN CATCH
	END CATCH
END
GO




