Wednesday, March 28, 2018

Custom Role

USE DBName;
GO
CREATE ROLE usrDBOSchema;
GO
GRANT ALTER, DELETE, EXECUTE, INSERT, REFERENCES, SELECT, UPDATE, VIEW DEFINITION ON SCHEMA ::dbo TO usrDBOSchema;
GO
GRANT CREATE TABLE, CREATE PROCEDURE, CREATE FUNCTION, CREATE VIEW TO usrdboschema;
GO

Monday, November 6, 2017

Job Failed Query

SELECT Job.server ServerName,
       SysJobs.name JobName,
       SysJobSteps.step_name StepName,
       Job.run_status RunStatus,
       Job.message ErrorMessage,
       Job.ExecDate RunDate,
       Job.run_duration RunTime
FROM
(
    SELECT Instance.instance_id,
           DBSysJobHistory.job_id,
           DBSysJobHistory.step_id,
           DBSysJobHistory.sql_message_id,
           DBSysJobHistory.sql_severity,
           DBSysJobHistory.message,
           (CASE DBSysJobHistory.run_status
                WHEN 0
                THEN 'Failed'
                WHEN 1
                THEN 'Succeeded'
                WHEN 2
                THEN 'Retry'
                WHEN 3
                THEN 'Canceled'
                WHEN 4
                THEN 'In progress'
            END) AS run_status,
           ((SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 5, 2)+'/'+SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 7, 2)+'/'+SUBSTRING(CAST(DBSysJobHistory.run_date AS VARCHAR(8)), 1, 4)+' '+SUBSTRING((REPLICATE('0', 6-LEN(CAST(DBSysJobHistory.run_time AS VARCHAR)))+CAST(DBSysJobHistory.run_time AS VARCHAR)), 1, 2)+':'+SUBSTRING((REPLICATE('0', 6-LEN(CAST(DBSysJobHistory.run_time AS VARCHAR)))+CAST(DBSysJobHistory.run_time AS VARCHAR)), 3, 2)+':'+SUBSTRING((REPLICATE('0', 6-LEN(CAST(DBSysJobHistory.run_time AS VARCHAR)))+CAST(DBSysJobHistory.run_time AS VARCHAR)), 5, 2))) AS ExecDate,
           DBSysJobHistory.run_duration,
           DBSysJobHistory.retries_attempted,
           DBSysJobHistory.server Server
    FROM msdb.dbo.sysjobhistory DBSysJobHistory
         JOIN
    (
        SELECT DBSysJobHistory.job_id,
               DBSysJobHistory.step_id,
               MAX(DBSysJobHistory.instance_id) AS instance_id
        FROM msdb.dbo.sysjobhistory DBSysJobHistory
        GROUP BY DBSysJobHistory.job_id,
                 DBSysJobHistory.step_id
    ) AS Instance ON DBSysJobHistory.instance_id = Instance.instance_id
    WHERE DBSysJobHistory.run_status <> 1
) AS Job
JOIN msdb.dbo.sysjobs SysJobs ON(Job.job_id = SysJobs.job_id)
JOIN msdb.dbo.sysjobsteps SysJobSteps ON(Job.job_id = SysJobSteps.job_id
                                         AND Job.step_id = SysJobSteps.step_id)
WHERE Job.ExecDate > DATEADD(DD, -1, GETDATE())
      AND Job.run_status LIKE 'failed';

Wednesday, September 13, 2017

Add Startup parameter with powershell

http://www.sqlservercentral.com/blogs/erichumphrey/2011/03/31/change-sql-startup-parameters-with-powershell/

##############################################################################
##
## Add-SqlServerStartupParameter
##
## by Eric Humphrey (http://www.erichumphrey.com/category/powershell/)
##
##############################################################################

<#

.SYNOPSIS

Adds an entry to the startup parameters list for all instances of SQL Server
on a computer

.EXAMPLE

PS >Add-SqlServerStartupParameter '-T3226'

#>

param(
    ## The parameter you wish to add
    [Parameter(Mandatory = $true)]
    $StartupParameter
)

$hklmRootNode = "HKLM:\SOFTWARE\Microsoft\Microsoft SQL Server"

$props = Get-ItemProperty "$hklmRootNode\Instance Names\SQL"
$instances = $props.psobject.properties | ?{$_.Value -like 'MSSQL*'} | select Value

$instances | %{
    $inst = $_.Value;
    $regKey = "$hklmRootNode\$inst\MSSQLServer\Parameters"
    $props = Get-ItemProperty $regKey
    $params = $props.psobject.properties | ?{$_.Name -like 'SQLArg*'} | select Name, Value
    #$params | ft -AutoSize
    $hasFlag = $false
    foreach ($param in $params) {
        if($param.Value -eq $StartupParameter) {
            $hasFlag = $true
            break;
        }
    }
    if (-not $hasFlag) {
        "Adding $StartupParameter"
        $newRegProp = "SQLArg"+($params.Count)
        Set-ItemProperty -Path $regKey -Name $newRegProp -Value $StartupParameter
    } else {
        "$StartupParameter already set"
    }
}

Monday, September 11, 2017

Backup Master Key for Encryption - DB Related

USE DBName
BACKUP MASTER KEY TO FILE = 'c:\masterkey'
    ENCRYPTION BY PASSWORD = 'Myw0rld123'

--RESTORE MASTER KEY
--    FROM FILE = 'c:\masterkey'
--    DECRYPTION BY PASSWORD = 'Myw0rld123'
--    ENCRYPTION BY PASSWORD = 'Myw0rld123'

--FORCE

Thursday, August 17, 2017

Audit Object DDL operation - Default Trace

create table #objecttype
(id int, objectname nvarchar(255))
insert into #objecttype
SELECT 8259,'Check Constraint'
UNION ALL SELECT 8260,'Default (constraint or standalone)'
UNION ALL SELECT 8262,'Foreign-key Constraint'
UNION ALL SELECT 8272,'Stored Procedure'
UNION ALL SELECT 8274,'Rule'
UNION ALL SELECT 8275,'System Table'
UNION ALL SELECT 8276,'Trigger on Server'
UNION ALL SELECT 8277,'(User-defined) Table'
UNION ALL SELECT 8278,'View'
UNION ALL SELECT 8280,'Extended Stored Procedure'
UNION ALL SELECT 16724,'CLR Trigger'
UNION ALL SELECT 16964,'Database'
UNION ALL SELECT 16975,'Object'
UNION ALL SELECT 17222,'FullText Catalog'
UNION ALL SELECT 17232,'CLR Stored Procedure'
UNION ALL SELECT 17235,'Schema'
UNION ALL SELECT 17475,'Credential'
UNION ALL SELECT 17491,'DDL Event'
UNION ALL SELECT 17741,'Management Event'
UNION ALL SELECT 17747,'Security Event'
UNION ALL SELECT 17749,'User Event'
UNION ALL SELECT 17985,'CLR Aggregate Function'
UNION ALL SELECT 17993,'Inline Table-valued SQL Function'
UNION ALL SELECT 18000,'Partition Function'
UNION ALL SELECT 18002,'Replication Filter Procedure'
UNION ALL SELECT 18004,'Table-valued SQL Function'
UNION ALL SELECT 18259,'Server Role'
UNION ALL SELECT 18263,'Microsoft Windows Group'
UNION ALL SELECT 19265,'Asymmetric Key'
UNION ALL SELECT 19277,'Master Key'
UNION ALL SELECT 19280,'Primary Key'
UNION ALL SELECT 19283,'ObfusKey'
UNION ALL SELECT 19521,'Asymmetric Key Login'
UNION ALL SELECT 19523,'Certificate Login'
UNION ALL SELECT 19538,'Role'
UNION ALL SELECT 19539,'SQL Login'
UNION ALL SELECT 19543,'Windows Login'
UNION ALL SELECT 20034,'Remote Service Binding'
UNION ALL SELECT 20036,'Event Notification on Database'
UNION ALL SELECT 20037,'Event Notification'
UNION ALL SELECT 20038,'Scalar SQL Function'
UNION ALL SELECT 20047,'Event Notification on Object'
UNION ALL SELECT 20051,'Synonym'
UNION ALL SELECT 20549,'End Point'
UNION ALL SELECT 20801,'Adhoc Queries which may be cached'
UNION ALL SELECT 20816,'Prepared Queries which may be cached'
UNION ALL SELECT 20819,'Service Broker Service Queue'
UNION ALL SELECT 20821,'Unique Constraint'
UNION ALL SELECT 21057,'Application Role'
UNION ALL SELECT 21059,'Certificate'
UNION ALL SELECT 21075,'Server'
UNION ALL SELECT 21076,'Transact-SQL Trigger'
UNION ALL SELECT 21313,'Assembly'
UNION ALL SELECT 21318,'CLR Scalar Function'
UNION ALL SELECT 21321,'Inline scalar SQL Function'
UNION ALL SELECT 21328,'Partition Scheme'
UNION ALL SELECT 21333,'User'
UNION ALL SELECT 21571,'Service Broker Service Contract'
UNION ALL SELECT 21572,'Trigger on Database'
UNION ALL SELECT 21574,'CLR Table-valued Function'
UNION ALL SELECT 21577,'Internal Table (For example, XML Node Table, Queue Table.)'
UNION ALL SELECT 21581,'Service Broker Message Type'
UNION ALL SELECT 21586,'Service Broker Route'
UNION ALL SELECT 21825,'User'
UNION ALL SELECT 21827,'User'
UNION ALL SELECT 21831,'User'
UNION ALL SELECT 21843,'User'
UNION ALL SELECT 21847,'User'
UNION ALL SELECT 21587,'Statistics'
UNION ALL SELECT 22099,'Service Broker Service'
UNION ALL SELECT 22601,'Index'
UNION ALL SELECT 22604,'Certificate Login'
UNION ALL SELECT 22611,'XMLSchema'
UNION ALL SELECT 22868,'Type'
GO

declare @d1 datetime;
declare @diff int;
declare @curr_tracefilename varchar(500);
declare @base_tracefilename varchar(500);
declare @indx int ;

select @curr_tracefilename = path from sys.traces where is_default = 1 ;
set @curr_tracefilename = reverse(@curr_tracefilename)
select @indx  = PATINDEX('%\%', @curr_tracefilename)
set @curr_tracefilename = reverse(@curr_tracefilename)
set @base_tracefilename = LEFT( @curr_tracefilename,len(@curr_tracefilename) - @indx) + '\log.trc';

--sp_configure 'show advanced option',1
--reconfigure with override
--exec sp_configure 'default trace enabled',1
--reconfigure with override
select distinct  DatabaseName
,  a.ObjectName
,       ObjectID
,  b.name Operation
,  c.subclass_name ObjType
, HostName
,       LoginName
,       StartTime
,       ApplicationName
from ::fn_trace_gettable( @curr_tracefilename, default )  a
 left join sys.trace_events b on a.EventClass = b.trace_event_id
 left join sys.trace_subclass_values c on a.ObjectType = c.subclass_value  
where EventSubclass = 0 and c.subclass_name not like 'IX' and DatabaseName not like 'ReportServer%' and DatabaseName not like '%snapshot'
and c.subclass_name not like 'ST' and databasename not like 'tempdb'
--and DatabaseID = db_id() and DatabaseName not like 'reportserver%'
--and DatabaseName not like 'tempdb' and c.objectname not like 'statistics' and c.objectname is not null
ORDER BY 8 DESC
go
drop table #objecttype

Friday, August 11, 2017

Reduce HIGH VLF count - SQL Server


-- 2011-05-24 Pedro Lopes (Microsoft) pedro.lopes@microsoft.com (http://aka.ms/sqlinsights)
--
-- 2012-03-25 Added SQL 2012 support
-- 2012-09-19 Simplified logic
-- 2012-09-20 Changed grow settings if not SQL Server 2012
--
-- Generates the sql statements to preemtively fix VLF issues in all DBs within the server, based on the transaction log current size.
--
SET NOCOUNT ON;

DECLARE @query VARCHAR(1000), @dbname VARCHAR(255), @count int, @usedlogsize bigint, @logsize bigint
DECLARE @sqlcmd NVARCHAR(1000), @sqlparam NVARCHAR(100), @filename VARCHAR(255), @i int, @recmodel NVARCHAR(128)
DECLARE @potsize int, @n_iter int, @n_iter_final int, @initgrow int, @n_init_iter int, @bckpath NVARCHAR(255)
DECLARE @majorver smallint, @minorver smallint, @build smallint, @DefaultBackupDirectory VARCHAR(1024)

CREATE TABLE #loginfo (dbname varchar(100), num_of_rows int, used_logsize_MB DECIMAL(20,1))

DECLARE @tblvlf TABLE (dbname varchar(100),
Actual_log_size_MB DECIMAL(20,1),
Potential_log_size_MB DECIMAL(20,1),
Actual_VLFs int,
Potential_VLFs int,
Growth_iterations int,
Log_Initial_size_MB DECIMAL(20,1),
File_autogrow_MB DECIMAL(20,1))

SELECT TOP 1 @bckpath = REVERSE(RIGHT(REVERSE(physical_device_name), LEN(physical_device_name)-CHARINDEX('\',REVERSE(physical_device_name),0))) FROM msdb.dbo.backupmediafamily WHERE device_type = 2

SELECT @majorver = (@@microsoftversion / 0x1000000) & 0xff, @minorver = (@@microsoftversion / 0x10000) & 0xff, @build = @@microsoftversion & 0xffff

--DECLARE csr CURSOR FAST_FORWARD FOR SELECT name FROM master..sysdatabases WHERE dbid > 4 AND DATABASEPROPERTYEX(name,'status') = 'ONLINE' AND DATABASEPROPERTYEX(name,'Updateability') = 'READ_WRITE' AND name <> 'tempdb' AND name <> 'ReportServerTempDB'
DECLARE csr CURSOR FAST_FORWARD FOR SELECT name FROM master.sys.databases WHERE is_read_only = 0 AND state = 0 AND database_id <> 2;
OPEN csr
FETCH NEXT FROM csr INTO @dbname
WHILE (@@FETCH_STATUS <> -1)
BEGIN
CREATE TABLE #log_info (recoveryunitid int NULL,
fileid tinyint,
file_size bigint,
start_offset bigint,
FSeqNo int,
[status] tinyint,
parity tinyint,
create_lsn numeric(25,0))

SET @query = 'DBCC LOGINFO (' + '''' + @dbname + ''') WITH NO_INFOMSGS'
IF @majorver < 11
BEGIN
INSERT INTO #log_info (fileid, file_size, start_offset, FSeqNo, [status], parity, create_lsn)
EXEC (@query)
END
ELSE
BEGIN
INSERT INTO #log_info (recoveryunitid, fileid, file_size, start_offset, FSeqNo, [status], parity, create_lsn)
EXEC (@query)
END
SET @count = @@ROWCOUNT
SET @usedlogsize = (SELECT (MIN(l.start_offset) + SUM(CASE WHEN l.status <> 0 THEN l.file_size ELSE 0 END))/1024.00/1024.00 FROM #log_info l)
DROP TABLE #log_info;
INSERT #loginfo
VALUES(@dbname, @count, @usedlogsize);
FETCH NEXT FROM csr INTO @dbname
END

CLOSE csr
DEALLOCATE csr

PRINT '/* Generated on ' + CONVERT (VARCHAR, GETDATE()) + ' in ' + @@SERVERNAME + ' */' + CHAR(10)

DECLARE cshrk CURSOR FAST_FORWARD FOR SELECT dbname, num_of_rows FROM #loginfo
WHERE num_of_rows >= 50 --My rule of thumb is 50 VLFs. Your mileage may vary.
ORDER BY dbname
OPEN cshrk
FETCH NEXT FROM cshrk INTO @dbname, @count
WHILE (@@FETCH_STATUS <> -1)
BEGIN
SET @sqlcmd = 'SELECT @nameout = name, @logsizeout = (size*8)/1024 FROM [' + @dbname + '].dbo.sysfiles WHERE (64 & status) = 64'
SET @sqlparam = '@nameout NVARCHAR(100) OUTPUT, @logsizeout bigint OUTPUT'
EXEC sp_executesql @sqlcmd, @sqlparam, @nameout = @filename OUTPUT, @logsizeout = @logsize OUTPUT;
PRINT '---------------------------------------------------------------------------------------------------------- '
PRINT CHAR(13) + 'USE ' + QUOTENAME(@dbname) + ';'
PRINT 'DBCC SHRINKFILE (N''' + @filename + ''', 1, TRUNCATEONLY);'
PRINT '--'
PRINT '-- CHECK: if the tlog file has shrunk with the following query:'
PRINT 'SELECT name, (size*8)/1024 AS log_MB FROM [' + @dbname + '].dbo.sysfiles WHERE (64 & status) = 64'
PRINT '--'
SET @recmodel = CONVERT(NVARCHAR, DATABASEPROPERTYEX(@dbname,'Recovery'))
IF @recmodel <> 'SIMPLE'
BEGIN
PRINT '-- If the log has not shrunk, you must backup the transaction log next.'
PRINT '-- Repeat the backup and shrink process alternatively until you get the desired log size (about 1MB).'
PRINT '--'
PRINT '-- METHOD: Backup -> Shrink (repeat the backup and shrink process until the log has shrunk):'
PRINT '--'
PRINT '-- Create example logical backup device.'
EXECUTE master..xp_instance_regread N'HKEY_LOCAL_MACHINE', N'SOFTWARE\Microsoft\MSSQLServer\MSSQLServer', N'BackupDirectory', @DefaultBackupDirectory OUTPUT;
SET @bckpath =  @DefaultBackupDirectory + '\' + @dbname + '.trn'';'
PRINT 'USE ' + QUOTENAME(@dbname) + ';'
PRINT '-- Backup Log'
PRINT 'BACKUP LOG ' + QUOTENAME(@dbname) + ' TO DISK = N'''+ @bckpath;
PRINT '-- Shrink'
PRINT 'DBCC SHRINKFILE (N''' + @filename + ''', 1);'
PRINT '--'
PRINT '-- METHOD: Alter recovery model -> Shrink:'
PRINT '-- NOTE: Because the database is in ' + @recmodel + ' recovery model, one alternative is to set it to SIMPLE to truncate the log, shrink it, and reset it to ' + @recmodel + '.'
PRINT '-- NOTE2: This method of setting the recovery model to SIMPLE and back again WILL BREAK log chaining, and thus any log shipping or mirroring.'

PRINT '--Commented changing simple recovery'

PRINT '--USE [master]; ' + CHAR(13) + '--ALTER DATABASE ' + QUOTENAME(@dbname) + ' SET RECOVERY SIMPLE;'
PRINT '--USE ' + QUOTENAME(@dbname) + ';' + CHAR(13) + '--DBCC SHRINKFILE (N''' + @filename + ''', 1);'
PRINT '--USE [master]; ' + CHAR(13) + '--ALTER DATABASE ' + QUOTENAME(@dbname) + ' SET RECOVERY ' + @recmodel + ';'
PRINT '--'
PRINT '-- CHECK: if the tlog file has shrunk with the following query:'
PRINT 'SELECT name, (size*8)/1024 AS log_MB FROM [' + @dbname + '].dbo.sysfiles WHERE (64 & status) = 64'
END
ELSE
BEGIN
PRINT '-- If not, then proceed to the next step (it may be necessary to execute multiple times):'
PRINT 'DBCC SHRINKFILE (N''' + @filename + ''', 1);'
PRINT '-- CHECK: if the tlog file has shrunk with the following query:'
PRINT 'SELECT name, (size*8)/1024 AS log_MB FROM [' + @dbname + '].dbo.sysfiles WHERE (64 & status) = 64'
END

-- We are growing in MB instead of GB because of known issue prior to SQL 2012.
-- More detail here: http://www.sqlskills.com/BLOGS/PAUL/post/Bug-log-file-growth-broken-for-multiples-of-4GB.aspx
-- and http://connect.microsoft.com/SQLServer/feedback/details/481594/log-growth-not-working-properly-with-specific-growth-sizes-vlfs-also-not-created-appropriately
-- or https://connect.microsoft.com/SQLServer/feedback/details/357502/transaction-log-file-size-will-not-grow-exactly-4gb-when-filegrowth-4gb
IF @majorver >= 11
BEGIN
SET @n_iter = (SELECT CASE WHEN @logsize <= 64 THEN 1
WHEN @logsize > 64 AND @logsize < 256 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/256, 0)
WHEN @logsize >= 256 AND @logsize < 1024 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/512, 0)
WHEN @logsize >= 1024 AND @logsize < 4096 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/1024, 0)
WHEN @logsize >= 4096 AND @logsize < 8192 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/2048, 0)
WHEN @logsize >= 8192 AND @logsize < 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/4096, 0)
WHEN @logsize >= 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/8192, 0)
END)
SET @potsize = (SELECT CASE WHEN @logsize <= 64 THEN 1*64
WHEN @logsize > 64 AND @logsize < 256 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/256, 0)*256
WHEN @logsize >= 256 AND @logsize < 1024 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/512, 0)*512
WHEN @logsize >= 1024 AND @logsize < 4096 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/1024, 0)*1024
WHEN @logsize >= 4096 AND @logsize < 8192 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/2048, 0)*2048
WHEN @logsize >= 8192 AND @logsize < 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/4096, 0)*4096
WHEN @logsize >= 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/8192, 0)*8192
END)
END
ELSE
BEGIN
SET @n_iter = (SELECT CASE WHEN @logsize <= 64 THEN 1
WHEN @logsize > 64 AND @logsize < 256 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/256, 0)
WHEN @logsize >= 256 AND @logsize < 1024 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/512, 0)
WHEN @logsize >= 1024 AND @logsize < 4096 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/1024, 0)
WHEN @logsize >= 4096 AND @logsize < 8192 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/2048, 0)
WHEN @logsize >= 8192 AND @logsize < 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/4000, 0)
WHEN @logsize >= 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/8000, 0)
END)
SET @potsize = (SELECT CASE WHEN @logsize <= 64 THEN 1*64
WHEN @logsize > 64 AND @logsize < 256 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/256, 0)*256
WHEN @logsize >= 256 AND @logsize < 1024 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/512, 0)*512
WHEN @logsize >= 1024 AND @logsize < 4096 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/1024, 0)*1024
WHEN @logsize >= 4096 AND @logsize < 8192 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/2048, 0)*2048
WHEN @logsize >= 8192 AND @logsize < 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/4000, 0)*4000
WHEN @logsize >= 16384 THEN ROUND(CONVERT(FLOAT, ROUND(@logsize, -2))/8000, 0)*8000
END)
END

-- If the proposed log size is smaller than current log, and also smaller than 4GB,
-- and there is less than 512MB of diff between the current size and proposed size, add 1 grow.
SET @n_iter_final = @n_iter
IF @logsize > @potsize AND @potsize <= 4096 AND ABS(@logsize - @potsize) < 512
BEGIN
SET @n_iter_final = @n_iter + 1
END
-- If the proposed log size is larger than current log, and also larger than 50GB,
-- and there is less than 1GB of diff between the current size and proposed size, take 1 grow.
ELSE IF @logsize < @potsize AND @potsize <= 51200 AND ABS(@logsize - @potsize) > 1024
BEGIN
SET @n_iter_final = @n_iter - 1
END

IF @potsize = 0
BEGIN
SET @potsize = 64
END
IF @n_iter = 0
BEGIN
SET @n_iter = 1
END

SET @potsize = (SELECT CASE WHEN @n_iter < @n_iter_final THEN @potsize + (@potsize/@n_iter)
WHEN @n_iter > @n_iter_final THEN @potsize - (@potsize/@n_iter)
ELSE @potsize END)

SET @n_init_iter = @n_iter_final
IF @potsize >= 8192
BEGIN
SET @initgrow = @potsize/@n_iter_final
END
IF @potsize >= 64 AND @potsize <= 512
BEGIN
SET @n_init_iter = 1
SET @initgrow = 512
END
IF @potsize > 512 AND @potsize <= 1024
BEGIN
SET @n_init_iter = 1
SET @initgrow = 1023
END
IF @potsize > 1024 AND @potsize < 8192
BEGIN
SET @n_init_iter = 1
SET @initgrow = @potsize
END

INSERT INTO @tblvlf
SELECT @dbname, @logsize, @potsize, @count,
CASE WHEN @potsize <= 64 THEN (@potsize/(@potsize/@n_init_iter))*4
WHEN @potsize > 64 AND @potsize < 1024 THEN (@potsize/(@potsize/@n_init_iter))*8
WHEN @potsize >= 1024 THEN (@potsize/(@potsize/@n_init_iter))*16
END,
@n_init_iter, @initgrow, CASE WHEN (@potsize/@n_iter_final) <= 1024 THEN (@potsize/@n_iter_final) ELSE 1024 END

SET @i = 0
WHILE @i <= @n_init_iter
BEGIN
IF @i = 1
BEGIN
--Log Autogrow should not be above 1GB
PRINT CHAR(13) + '-- Now for the log file growth:'
PRINT 'ALTER DATABASE [' + @dbname + '] MODIFY FILE ( NAME = N''' + @filename + ''', SIZE = ' + CONVERT(VARCHAR, @initgrow) + 'MB , FILEGROWTH = ' + CASE WHEN (@potsize/@n_iter_final) <= 1024 THEN CONVERT(VARCHAR, (@potsize/@n_iter_final)) ELSE '1024' END + 'MB );'
END
IF @i > 1
BEGIN
PRINT 'ALTER DATABASE [' + @dbname + '] MODIFY FILE ( NAME = N''' + @filename + ''', SIZE = ' + CONVERT(VARCHAR, @initgrow*@i)+ 'MB );'
END
SET @i = @i + 1
CONTINUE
END
FETCH NEXT FROM cshrk INTO @dbname, @count
END
CLOSE cshrk
DEALLOCATE cshrk;

DROP TABLE #loginfo;

SELECT dbname AS [Database_Name], Actual_log_size_MB, Potential_log_size_MB, Actual_VLFs,
Potential_VLFs, Growth_iterations, Log_Initial_size_MB, File_autogrow_MB
FROM @tblvlf;
GO

Monday, August 7, 2017

Restore Missing .msi and .msp files - SQL Server

1. Download vb script from below site.

https://support.microsoft.com/en-us/help/969052/how-to-restore-the-missing-windows-installer-cache-files-and-resolve-p

To resolve these problems by using a script, follow these steps:
  1. Locate the directory to which you saved the script content.
  2. Open an elevated command prompt to the directory to which you saved the script, and run the following command:
    Cscript FindSQLInstalls.vbs %computername%_sql_install_details.txt
  3. Open the file from step 2 in a text editor such as Notepad, and identify the problems that are causing the failure. To do this, search the text file for string patterns such as the following:
    • do not
    • !!!
  4. Based on the results in step 3, take the steps that are required.

    Note Look for more information about these steps in the "Examples" section.
  5. Repeat steps 2 through 4 until the text file that is created in step 2 no longer contains text that references invalid paths or missing files for the component that is being updated.

You will have to copy either RTM install or SP1 or SP2 or SP3 install depends upon error message. If you need help please let me know.