There are times when you need to migrate data or scale out your database. Here is a script to get you how much space you’re actually using per Table.
Here are 3 methods but not limited.
1. Get a specific table size
EXEC sp_spaceused N’dbo.MyTable’
2. Get all table size
CREATE TABLE #TableSize(Name VARCHAR(100), Rows VARCHAR(50), Reserved VARCHAR(50), Data VARCHAR(50), IndexSize VARCHAR(50), Unused VARCHAR(50))
INSERT INTO #TableSize
EXEC sp_msforeachtable ‘EXEC sp_spaceused [?]’
SELECT * FROM #TableSize ORDER BY Name ASC
3. Custom Code
TableName = t.Name
,SchemaName = s.Name
,[RowCount] = p.Rows
,TotalSpaceKB = SUM(a.Total_Pages) * 8
,UsedSpaceKB = SUM(a.Used_Pages) * 8
,UnusedSpaceKB = (SUM(a.Total_Pages) – SUM(a.Used_Pages)) * 8
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 i.OBJECT_ID > 255
GROUP BY t.Name
ORDER BY t.Name
- SET TRANSACTION ISOLATION LEVEL XXXXX is for the entire script
- WITH (NOLOCK) is for that specific table
SET TRANSACTION ISOLATION LEVEL READ COMMITTED;
SET TRANSACTION ISOLATION LEVEL READ UNCOMMITTED;
SET TRANSACTION ISOLATION LEVEL REPEATABLE READ;
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
SELECT * FROM MyTable WITH (NOLOCK)
Specifies that shared locks are held while the data is being read to avoid dirty reads, but the data can be changed before the end of the transaction, resulting innonrepeatable reads or phantom data. This option is the SQL Server default.
Implements dirty read, or isolation level 0 locking, which means that no shared locks are issued and no exclusive locks are honored. When this option is set, it is possible to read uncommitted or dirty data; values in the data can be changed and rows can appear or disappear in the data set before the end of the transaction. This option has the same effect as setting NOLOCK on all tables in all SELECT statements in a transaction. This is the least restrictive of the four isolation levels.
Locks are placed on all data that is used in a query, preventing other users from updating the data, but new phantom rows can be inserted into the data set by another user and are included in later reads in the current transaction. Because concurrency is lower than the default isolation level, use this option only when necessary.
Places a range lock on the data set, preventing other users from updating or inserting rows into the data set until the transaction is complete. This is the most restrictive of the four isolation levels. Because concurrency is lower, use this option only when necessary. This option has the same effect as setting HOLDLOCK on all tables in all SELECT statements in a transaction.
There are times when you need to figure out what Stored Procedures are currently running and if they are blocking any if all other processes.
FROM sys.dm_exec_requests req
CROSS APPLY sys.dm_exec_sql_text(sql_handle) AS sqltext
WHERE DB_NAME(database_id) = ‘MyDatabaseName‘
If you have the need to take multiple rows into 1 comma separated row then this should do the trick. One of the biggest usages for this is when you’re using SSRS with multiple parameters.
DECLARE @COLUMNS VARCHAR(MAX)
SELECT @COLUMNS =
COALESCE (@COLUMNS + ‘,’ + CAST(FirstName AS NVARCHAR) + ”,” + CAST(FirstName AS NVARCHAR) + ”)
Using OpenRowSet to read all data from an Excel worksheet into a global temp table.
INSERT INTO ##ABC
‘Excel 8.0;Database=D:\EDI\SAMPLE_051311_1.xls‘, [Sheet1$])
For those who are interested in using SQL cdosysmail to email embedded HTML follow this code as a model. Keep in mind that you are required to have the Stored Procedure sp_send_cdosysmail.
SET NOCOUNT ON
— SETUP VARIABLES
DECLARE @body1 VARCHAR(MAX)
DECLARE @subjecttitle VARCHAR(100)
DECLARE @email VARCHAR(200)
DECLARE @today DATETIME
SET @today = (SELECT CONVERT(VARCHAR,GETDATE()-1,101))
SET @subjecttitle = ‘Daily Sales Summary Report for ‘ + CONVERT(VARCHAR,@today)
— PROCESS EMAIL DATA
SELECTTOP 20 @body1 = @body1 +’
WHERE DIMENSION = 1001 AND RECEIPTDATE = @today
GROUP BY DIMENSION— SEND THE EMAIL
@Subject = @subjecttitle,
@Body = @body1
|Act||Plan||Var $||Var %||LY||Comp|
|‘ + CONVERT(VARCHAR,DIMENSION) + ‘||‘+ CONVERT(VARCHAR,SUM(CONVERT(DECIMAL(12,2),TOTALBEFORETAX))) + ‘||‘ + ‘VAR $’ + ‘||‘+ ‘Var %’ + ‘||‘ + ‘LY’ + ‘||‘ + ‘Comp’ + ‘|