You can then add the desired compression type to a table as follows:EXEC sp_estimate_data_compression_savings [Schema], [TableName], NULL, NULL, 'ROW'
ALTER TABLE [TableName]REBUILD WITH (DATA_COMPRESSION = PAGE)
You can then add the desired compression type to a table as follows:EXEC sp_estimate_data_compression_savings [Schema], [TableName], NULL, NULL, 'ROW'
ALTER TABLE [TableName]REBUILD WITH (DATA_COMPRESSION = PAGE)
Alternatively, as suggested by Pinal Dave in his blog post, you could set the server to Single User Mode and back to Multi User Mode as follows. This method is very quick and neat, however, this will kill ALL connections to the database, not just those from domain users:select 'Kill ' + cast(spid as varchar) from sys.sysprocesseswhere loginame like 'DOMAIN\%'
Credit for Pinal Dave blog post: http://blog.sqlauthority.com/2009/02/06/sql-server-quickest-way-to-kill-all-threads-kill-all-user-session-kill-all-processes/USE master;GOALTER DATABASE AdventureWorksSET SINGLE_USERWITH ROLLBACK IMMEDIATE;ALTER DATABASE AdventureWorksSET MULTI_USER;GO
selectso.name,last_user_seek = max(last_user_seek),last_user_scan = max(last_user_scan),last_user_lookup = max(last_user_lookup),last_user_update = max(last_user_update)fromsys.dm_db_index_usage_stats sinner join sysobjects soon s.object_id = so.idwhere[database_id] = db_id()group byso.name
declare @dt datetime
set @dt = 0
select @dt
select getdate()
select datediff(year,0,getdate())The query above basically says "what is the difference (datediff), in whole years (year), between 1st Jan 1900 (0) and today's date (getdate())". You can then add this number of whole years to 1st Jan 1900, to give you the 1st of Jan this year, by using the DATEADD function:
select dateadd(year,datediff(year,0,getdate()),0)The query above can be adapted to get the first of the current month, week, or to strip off the time portion of today's date, by substituting "year" for "week" or "day" respectively:
select dateadd(week,datediff(week,0,getdate()),0)To get the first of next month, you can find the first of this month, and add an extra month, as follows (note the "+1"):
select dateadd(day,datediff(day,0,getdate()),0)
select dateadd(month,datediff(month,0,getdate())+1,0)Last day of this month:
select dateadd(day,-1,dateadd(month,datediff(month,0,getdate())+1,0))Last day of last year:
select dateadd(day,-1,dateadd(year,datediff(year,0,getdate()),0))
USE [msdb]
GO
SELECT
j.name,
js.step_id,
js.command,
j.enabled,
j.job_id
FROM dbo.sysjobs j
JOIN dbo.sysjobsteps js
ON js.job_id = j.job_id
WHERE js.command LIKE N'%Insert search string here%'
ORDER BY j.name
select
t.NAME as TableName
,s.Name as SchemaName
,p.rows as RowCounts
,sum(a.total_pages) * 8 as TotalSpaceKB
,sum(a.used_pages) * 8 as UsedSpaceKB
,(sum(a.total_pages) - sum(a.used_pages)) * 8 as UnusedSpaceKB
from
sys.tables t
inner join sys.indexes i
on t.OBJECT_ID = i.object_id
inner join sys.partitions p
on i.object_id = p.OBJECT_ID
and i.index_id = p.index_id
inner join sys.allocation_units a
on p.partition_id = a.container_id
left outer join sys.schemas s
on t.schema_id = s.schema_id
where
t.NAME not like 'dt%'
and t.is_ms_shipped = 0
and i.OBJECT_ID > 255
group by
t.Name,
s.Name,
p.Rows
order by
t.Name
alter database [DatabaseName] collate [Collation]
EXEC sp_MSforeachtable @command1="ALTER TABLE ? NOCHECK CONSTRAINT ALL"
SELECT 'ALTER TABLE [' + SYSOBJECTS.Name + '] ALTER COLUMN [' + SYSCOLUMNS.Name + '] ' +4, Check the output to ensure the previous script completed for each column (i.e. check there were no errors). Finally, re-enable the constraint checking for each table by running the following:
SYSTYPES.name +
CASE systypes.NAME
WHEN 'text' THEN ' '
ELSE
'(' + RTRIM(CASE SYSCOLUMNS.length
WHEN -1 THEN 'MAX'
ELSE CONVERT(CHAR,SYSCOLUMNS.length)
END) + ') '
END
+ ' ' + ' COLLATE Latin1_General_CI_AS ' + CASE ISNULLABLE WHEN 0 THEN 'NOT NULL' ELSE 'NULL' END
FROM SYSCOLUMNS , SYSOBJECTS , SYSTYPES
WHERE SYSCOLUMNS.ID = SYSOBJECTS.ID
AND SYSOBJECTS.TYPE = 'U'
AND SYSTYPES.Xtype = SYSCOLUMNS.xtype
AND SYSCOLUMNS.COLLATION IS NOT NULL
AND NOT ( sysobjects.NAME LIKE 'sys%' )
AND NOT ( SYSTYPES.name LIKE 'sys%' )
GO
EXEC sp_MSforeachtable @command1="ALTER TABLE ? CHECK CONSTRAINT ALL"
The following SQL is a good means to split an update on a massive table into smaller chunks, whilst reducing blocking. The method is to upda...