DECLARE @MAXID VARCHAR(20) = '1'
DECLARE @SQL NVARCHAR(2000) = ''
BEGIN TRANSACTION
IF NOT EXISTS(SELECT * FROM sys.indexes WHERE name='IX_LOGS_TARIH_SAAT_LOGID' AND object_id = OBJECT_ID('DBO.LOG'))
BEGIN
        
    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOGARCHIVE' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   DROP TABLE DBO.LOGARCHIVE
    END

    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOGS' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   DROP TABLE DBO.LOGS
    END

    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOG' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   SET @MAXID = CONVERT(VARCHAR,ISNULL((SELECT MAX(LOGID) FROM LOG),0)+1)
    END

    SET @SQL = '
    CREATE TABLE [dbo].[LOGS](
	    [TARIH] [smalldatetime] NULL,
	    [SAAT] [nvarchar](11) NULL,
	    [USER] [nvarchar](50) NULL,
	    [ISLEM] [nvarchar](50) NULL,
	    [TABLE] [nvarchar](35) NULL,
	    [ICERIK1] [ntext] NULL,
	    [KAYITNO] [nvarchar](50) NULL,
	    [DEGISIKLIK] [nvarchar](100) NULL,
	    [LOGID] [int] IDENTITY('+@MAXID+',1) NOT NULL,
	CONSTRAINT [PK_LOGS] PRIMARY KEY CLUSTERED 
    (
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]'

    EXEC SP_EXECUTESQL @SQL

    CREATE NONCLUSTERED INDEX [IX_LOGS_ISLEM] ON [dbo].[LOGS]
    (
	    [ISLEM] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_TARIH_SAAT_LOGID] ON [dbo].[LOGS]
    (
	    [TARIH] ASC,
	    [SAAT] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_KAYITNO_LOGID] ON [dbo].[LOGS]
    (
	    [KAYITNO] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_TABLE_TARIH_SAAT_LOGID] ON [dbo].[LOGS]
    (
	    [TABLE] ASC,
	    [TARIH] ASC,
	    [SAAT] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_USER] ON [dbo].[LOGS]
    (
	    [USER] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    EXEC sp_rename 'LOG', 'LOGARCHIVE'

    EXEC sp_rename 'LOGS', 'LOG'

END
ELSE
BEGIN
     

    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOGS' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   DROP TABLE DBO.LOGS
    END

    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOGARCHIVE' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   SET @MAXID  = CONVERT(VARCHAR,ISNULL((SELECT MAX(LOGID) FROM LOGARCHIVE),0)+1)
    END

    SET @SQL = '
    CREATE TABLE [dbo].[LOG](
	    [TARIH] [smalldatetime] NULL,
	    [SAAT] [nvarchar](11) NULL,
	    [USER] [nvarchar](50) NULL,
	    [ISLEM] [nvarchar](50) NULL,
	    [TABLE] [nvarchar](35) NULL,
	    [ICERIK1] [ntext] NULL,
	    [KAYITNO] [nvarchar](50) NULL,
	    [DEGISIKLIK] [nvarchar](100) NULL,
	    [LOGID] [int] IDENTITY('+@MAXID+',1) NOT NULL,
	CONSTRAINT [PK_LOGS] PRIMARY KEY CLUSTERED 
    (
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]
    ) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]'

    IF EXISTS (SELECT * FROM INFORMATION_SCHEMA.TABLES WHERE TABLE_NAME = 'LOG' AND TABLE_SCHEMA = 'DBO') 
    BEGIN
	   DROP TABLE DBO.LOG
    END

    EXEC SP_EXECUTESQL @SQL

    CREATE NONCLUSTERED INDEX [IX_LOGS_ISLEM] ON [dbo].[LOG]
    (
	    [ISLEM] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_TARIH_SAAT_LOGID] ON [dbo].[LOG]
    (
	    [TARIH] ASC,
	    [SAAT] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_KAYITNO_LOGID] ON [dbo].[LOG]
    (
	    [KAYITNO] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_TABLE_TARIH_SAAT_LOGID] ON [dbo].[LOG]
    (
	    [TABLE] ASC,
	    [TARIH] ASC,
	    [SAAT] ASC,
	    [LOGID] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

    CREATE NONCLUSTERED INDEX [IX_LOGS_USER] ON [dbo].[LOG]
    (
	    [USER] ASC
    )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, SORT_IN_TEMPDB = OFF, DROP_EXISTING = OFF, ONLINE = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON, FILLFACTOR = 90) ON [PRIMARY]

END
IF @@ERROR <> 0
ROLLBACK
COMMIT