Thursday, July 26, 2012

SQL Server 2008 Rows in Tables

this code gives the row count for each table in a SQL Server 2008 database:

SET NOCOUNT ON

DBCC UPDATEUSAGE(0)

-- DB size.
EXEC sp_spaceused

-- Table row counts and sizes.
CREATE TABLE ##t
(
    [name] NVARCHAR(128),
    [rows] CHAR(11),
    reserved VARCHAR(18),
    data VARCHAR(18),
    index_size VARCHAR(18),
    unused VARCHAR(18)
)

INSERT ##t  EXEC sp_msForEachTable 'EXEC sp_spaceused ''?'''

SELECT *
FROM   ##t

-- # of rows.
SELECT SUM(CAST([rows] AS int)) AS [rows]
FROM   ##t
ORDER BY rows DESC


DROP TABLE ##t

Tuesday, July 10, 2012

current system date and time TSQL


Getting the current system date and time

SELECT SYSDATETIME()
    ,SYSDATETIMEOFFSET()
    ,SYSUTCDATETIME()
    ,CURRENT_TIMESTAMP
    ,GETDATE()
    ,GETUTCDATE();
Here is the result set.
SYSDATETIME() 2007-04-30 13:10:02.0474381
SYSDATETIMEOFFSET()2007-04-30 13:10:02.0474381 -07:00
SYSUTCDATETIME() 2007-04-30 20:10:02.0474381
CURRENT_TIMESTAMP 2007-04-30 13:10:02.047
GETDATE() 2007-04-30 13:10:02.047
GETUTCDATE() 2007-04-30 20:10:02.047