Wednesday, April 11, 2012

How to Shrink database in SQL Server


--Shrink Log


if object_id('tempdb.dbo.#file_info','u') is not null
drop table tempdb.dbo.#file_info


create table #file_info
(
dbname varchar(255),
logical_name varchar(255),
physical_name varchar(255)
)


insert into #file_info (dbname)
select '['+name+']' from master..sysdatabases where name not in ('Database1','Database2')


declare @sql varchar(2000), @dbname varchar(255)
declare c_1 cursor for select distinct dbname from #file_info
open c_1
fetch next from c_1 into @dbname
while @@fetch_status <> -1
begin
select @sql = 'update #file_info 
set physical_name = filename, logical_name = name
from '+@dbname+'..sysfiles 
where dbname = '''+@dbname+''' 
and filename like ''%.ldf'''


exec (@sql)


fetch next from c_1 into @dbname
end
close c_1
deallocate c_1


----------------------------------------------------------


declare @shrink_sql varchar(2000),
@shrink_dbname varchar(255),
@shrink_logical_name varchar(255)
declare c_2 cursor for select dbname,logical_name from #file_info
open c_2
fetch next from c_2 into @shrink_dbname,@shrink_logical_name
while @@fetch_status <> -1
begin
select @shrink_sql = '
use '+@shrink_dbname+'
dbcc shrinkfile ('+'['+@shrink_logical_name+']'+', notruncate)
dbcc shrinkfile ('+'['+@shrink_logical_name+']'+', truncateonly)
'
exec (@shrink_sql)
fetch next from c_2 into @shrink_dbname,@shrink_logical_name
end
close c_2
deallocate c_2
go

How to Check File Existance in the SSIS package


SET
--Read only Variables:
--User::SourceFileExtension,User::SourceFileName,User::SourceFilePath
--ReadWrite Variables:
--User::bolFileExists






' Microsoft SQL Server Integration Services Script Task
' Write scripts using Microsoft Visual Basic 2008.
' The ScriptMain is the entry point class of the script.


Imports System
Imports System.Data
Imports System.Math
Imports Microsoft.SqlServer.Dts.Runtime
Imports System.IO


_
_
 Partial Public Class ScriptMain
    Inherits Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase


    Enum ScriptResults
        Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success
        Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
    End Enum




    Public Sub Main()
        Dim fileLoc, fileName, fileExtension, FileDate As String
        If Dts.Variables.Contains("User::SourceFilePath") = True AndAlso _
           Dts.Variables.Contains("User::SourceFileName") = True AndAlso _
            Dts.Variables.Contains("User::SourceFileExtension") = True Then
            fileLoc = CStr(Dts.Variables("User::SourceFilePath").Value)
            fileName = CStr(Dts.Variables("User::SourceFileName").Value)
            FileDate = Now.Date.ToString("yyyy") & Now.Date.ToString("MM") & Now.Date.ToString("dd")
            fileExtension = CStr(Dts.Variables("User::SourceFileExtension").Value)
            If File.Exists(fileLoc + fileName + FileDate + fileExtension) Then
                Dts.Variables.Item("User::bolFileExists").Value = True
                Dts.TaskResult = ScriptResults.Success
            Else
                Dts.Variables.Item("User::bolFileExists").Value = False
                Dts.TaskResult = ScriptResults.Failure
            End If
            'Dts.TaskResult = ScriptResults.Success
            ' Else
            ' Dts.TaskResult = ScriptResults.Failure
        End If
    End Sub


End Class

ReIndexing of all the tables in the selected SQL DB


DECLARE @SQL VARCHAR(500),
@TBLNAME VARCHAR(100)


DECLARE C_1 CURSOR FOR SELECT NAME FROM SYSOBJECTS WHERE XTYPE = 'U'
OPEN C_1
FETCH NEXT FROM C_1 INTO @TBLNAME
WHILE @@FETCH_STATUS <> -1
BEGIN
SELECT @SQL = 'DBCC DBREINDEX ('''+@TBLNAME+''','''',90)'
EXEC (@SQL)
INSERT INTO REINDEX_AUDIT (INDEX_NAME,START)
VALUES (@TBLNAME,GETDATE())


FETCH NEXT FROM C_1 INTO @TBLNAME
END
CLOSE C_1
DEALLOCATE C_1


GO

Thursday, January 5, 2012

MDS - Master Data Services

SPI model (SaaS, PaaS, IaaS)

What is SPI (SaaS, PaaS, IaaS)?

SPI is an acronym for the most common cloud computing service models,
Software as a Service,
Platform as a Service and
Infrastructure as a Service.

Software as a Service (SaaS) is a software distribution model in which applications are hosted by a vendor or service provider and made available to customers over a network, typically the Internet.

Platform as a Service (PaaS) is a paradigm for delivering operating systems and associated services over the Internet without downloads or installation.

Infrastructure as a Service (IaaS) involves outsourcing the equipment used to support operations, including storage, hardware, servers and networking components.
The increasing selection of services delivered over the Internet is sometimes referred to as XaaS.