Wednesday, March 11, 2015

Get All Table Rows and Table Size in SQL Server


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 RowCounts desc

Sunday, October 26, 2014

SQL Server - Difference between clustered and a non-clustered index

What are the difference between clustered and a non-clustered index?

1). A clustered index is a special type of index that reorders the way records in the table are physically stored. Therefore table can have only one clustered index. The leaf nodes of a clustered index contain the data pages.

2). A non clustered index is a special type of index in which the logical order of the index does not match the physical stored order of the rows on disk. The leaf node of a non clustered index does not consist of the data pages. Instead, the leaf nodes contain index rows.

Monday, December 9, 2013

Read XML into SQL Database

Declare @xml XML
set @xml = (select cast(c1 as xml) from OPENROWSET (BULK 'XMLpath.xml',SINGLE_BLOB) as T1(c1))
insert into TableName (Col1,Col2,Col3)
SELECT A.Column1,A.Column2,A.Column3 FROM
(SELECT
       Tbl.Col.value('@XmlTag', 'varchar(200)') as Col1,
       Tbl.Col.value('@XmlTag', 'datetime')  as Col2,
       Tbl.Col.value('@XmlTag', 'varchar(2000)')  as Col3
FROM   @xml.nodes('//XMLNode') Tbl(Col) )  A