DBCC SHRINKFILE for specific files

select '

DBCC SHRINKFILE(''' + name + ''')

' from sys.database_files where type = 0 and name like 'YOURFILE%'

nice one

Posted in t-sql | Tagged | Kommentare deaktiviert

encryption for sql server

just read this  to create certificates for you dev box.

just read this  to implement your certificates to your box

 

Posted in encryption-for-sql-server | Tagged | Kommentare deaktiviert

Logging issue

 

The following table lists index operations and the type of logging available in each database recovery model for those operations. These recovery models are supported for both online and offline index operations.

 
Index operation Full Bulk-logged Simple
ALTER INDEX REORGANIZE Fully logged Fully logged Fully logged
ALTER INDEX REBUILD Fully logged Minimally logged Minimally logged
CREATE INDEX Fully logged Minimally logged Minimally logged
DBCC INDEXDEFRAG Fully logged Fully logged Fully logged
DBCC DBREINDEX Fully logged Minimally logged Minimally logged
DROP INDEX Index page deallocation is fully logged; new heap rebuild, if applicable, is fully logged. Index page deallocation is fully logged; new heap rebuild, if applicable, is minimally logged. Index page deallocation is fully logged; new heap rebuild, if applicable, is minimally logged.

For more information, see Choosing the Recovery Model for a Database.

Posted in cpu, fun, index | Tagged | Kommentare deaktiviert

create database snapshot with multiple filegroups/files

I found this script to create a database snapshot with multiple filegroups/fils, very usefully!  many thx to Ravikanth

 

 

$computerName = "YOURDBINSTANCE"

$dbName = "YOURDB"

$snapshotName = "YOURSNAPSHOTNAME"            

#Load all SQL necesssary assemblies

#this is a common section in all my SQL scripts

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMo") | Out-Null

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoExtended") | Out-Null

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.SMoEnum") | Out-Null

[Reflection.Assembly]::LoadWithPartialName("Microsoft.SqlServer.ConnectionInfo") | Out-Null            

#Get the server and database objects

$server = New-Object Microsoft.SqlServer.Management.Smo.Server($computerName)

$database = $server.Databases[$DBName]            

#Define a snapshot database and give the same name as the base databse

$snapshot = New-Object ("Microsoft.SqlServer.Management.Smo.Database") ($computerName,$snapshotName)

$snapshot.DatabaseSnapshotBaseName=$database.Name            

#Add all filegroups from the base database to snapshot

foreach ($filegroup in $database.FileGroups) {

$newfg = New-Object ("Microsoft.SqlServer.Management.Smo.FileGroup") ($snapshot,$filegroup.Name)

$snapshot.FileGroups.Add($newfg)

}            

#Add all datafiles from each filegroup of the base database to snapshot with a new datafile name

#in this example, I am using .ss as the datafile extension for the snapshot file

#By default, the snapshot files will reside in the same folder as base DB data files

foreach ($filegroup in $database.FileGroups) {

foreach ($datafile in $filegroup.Files) {

  $newDataFile = New-Object ("Microsoft.SqlServer.Management.Smo.DataFile") ($snapshot.FileGroups[$filegroup.Name],$datafile.Name,"$($database.PrimaryFilePath)\$($datafile.Name).ss")

  $newDataFile.FileName

  $snapshot.FileGroups[$filegroup.Name].Files.Add($newDataFile)

}

}            

#Create the snapshot

#This is it

$snapshot.Create()

 

 

Posted in backup, t-sql | Tagged | Kommentare deaktiviert

fast faster fedex

see this, i orderd memory(usa to germany) on Monday, now it is friday :D thx fedex

 

Posted in Uncategorized | Kommentare deaktiviert

File activation failure. The physical file name The log can not be rebuilt because the database was not cleanly shut down

In certain situations you do not want to be a DBA

Last week I was asked by a colleague to take care of a database. No one had bothered to really work/administrate with this database since 2008 but everyone is using this DB….

what I saw there was already a quite a desaster. The database was no longer visible to the SQL Server, just as there was no backup: (

So I tried to query the database again to attach to the SQL server, the message which is then followed lies not make me happy..

 

 File activation failure. The physical file name "xyz \ logDeletion.ldf" may be incorrect.
 The log can not be rebuilt because the database was not cleanly shut down.
 Msg 1813, Level 16, State 2, Line 1
 Could not open new database 'xyz'. CREATE DATABASE is aborted.

 What has happened? The database was not available, a corrupt log file … The weekend may be more beautiful?

 After many many hours reading test and I can only recommend always to Satisfy the backup exists.

 Everyone who gets into a situation like this, I recommend the following steps.

  1. Backup of the MDF / NDF and eventually present LOG files
  2.  A new database created with exactly the same name as the corrupt database(and file names eg. mdf,ndf,ldf)
  3. Shut down the SQL server.
  4. All files from the corrupt database in the directory to copy to the new DB is. Not the log file overwrite.
  5. SQL Server starts, the DB is now in status ‘SUSPECTET’
  6.  ALTER DATABASE SET SINGLE_USER Your Database
  7. ALTER DATABASE Your Database SET EMERGENCY
  8.  DBCC CHECKDB (Your Database, REPAIR_ALLOW_DATA_LOSS)
  9. ALTER DATABASE SET MULTI_USER Your Database

Test all this steps first with a dummy Database. Paul Randal has a great articel, read this first try this , then try again! understand what you are doing ;)

so far i hope you will never come in this situations….

Posted in log, monitor, t-sql | Tagged , | Kommentare deaktiviert

links links

Export data from Postgres to SQL Server using SSIS

SQL Server Performance Monitoring and Tuning Tutorial

Posted in index, monitor, t-sql | Kommentare deaktiviert

Disable/Enable all triggers and constraints in a database

USE yourDataBase;

-- SQL disable all triggers - disable all triggers sql server - t sql disable trigger

EXEC sp_MSforeachtable @command1="ALTER TABLE ? DISABLE TRIGGER ALL"

GO

-- SQL disable all constraints - disable all constraints sql server

EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"

GO

-- How to check if a trigger is disabled

USE yourDataBase;

SELECT * FROM sys.triggers WHERE name='table'
Posted in constraints, t-sql, trigger | Tagged | Kommentare deaktiviert

how much percent is already done by DBCC CHECKDB

Sometimes it is necessary that you know how long your DBCC CHECKDB will run and  how much percent is already done. Let’s find it out:

sp_who2 'active'

SELECT * FROM sys.dm_exec_sessions WHERE session_id = 60

SELECT command,percent_complete FROM sys.dm_exec_requests WHERE session_id = 60
Posted in monitor, t-sql | Tagged | Kommentare deaktiviert

who is active get detail information for the transactions

one of the best monitor sql scripts to get detail information for you sql server transactions

 

Posted in monitor, t-sql | Tagged | Kommentare deaktiviert