Category: Archive

Engineering notes from 2008–2014: SQL Server scripts, DBA quick-tips, and field notes from the trenches. Preserved as written — the foundation everything since was built on.

  • Ranking Function – Row_Number

    Ranking functions are functions that allow you to sequentially number your result set.

    Syntax

    ROW_NUMBER ( ) OVER ([ ] )

    partition_by_clause is a column or set of columns used to determine the grouping in which the ROW_NUMBER function applies sequential numbering.

    order_by_clause is a column or set of columns used to order the result set within the grouping.

    Examples

    CREATE TABLE Emp(
    EmpName VARCHAR(9),
    Age INT,
    MaritalStatus char(1))

    INSERT INTO Emp VALUES ('Abhinav',40,'S')
    INSERT INTO Emp VALUES ('Dhvani',20,'M')
    INSERT INTO Emp VALUES ('Nehal',20,'M')
    INSERT INTO Emp VALUES ('Sunil',95,'M')
    INSERT INTO Emp VALUES ('Bill',11,'S')
    INSERT INTO Emp VALUES ('Ram',100,'S')
    INSERT INTO Emp VALUES ('Nirmal',50,'S')


    Sample - 1
    SELECT ROW_NUMBER() OVER (ORDER BY Age) AS [Row Number by Age],
    EmpName,
    Age
    FROM Emp
    Sample - 2
    SELECT ROW_NUMBER() OVER (ORDER BY Age desc) AS [Row Number by Age],
    EmpName,
    Age
    FROM Emp

    Row Number by Age EmpName Age
    ——————– ——— ———–
    1 Bill 11
    2 Dhvani 20
    3 Nehal 20
    4 Abhinav 40
    5 Nirmal 50
    6 Sunil 95
    7 Ram 100

    (7 row(s) affected)

    Sample - 3
    SELECT ROW_NUMBER() OVER (PARTITION BY MaritalStatus ORDER BY Age) AS [Partition by MaritalStatus],
    EmpName,
    Age,
    MaritalStatus
    FROM Emp

    Partition by MaritalStatus EmpName Age MaritalStatus
    -------------------------- --------- ----------- -------------
    1 Dhvani 20 M
    2 Nehal 20 M
    3 Sunil 95 M
    1 Bill 11 S
    2 Abhinav 40 S
    3 Nirmal 50 S
    4 Ram 100 S

    (7 row(s) affected)

  • xp_enumgroups Extended stored procedure

    xp_enumgroups Extended stored procedure returns the list of Windows NT groups and their description. To get the list of the Windows NT groups, run:

    Sample
    EXEC master..xp_enumgroups

  • Operating System Error 112

    Operating system error 112 describes the insufficient disk space on drive.

    OS Error 112 occured while Backup
    1. Delete the old backuo files on drive if not required
    2. Archieve the old files to tape
    3. Compress the old backup files to create space
    4. Take the backup on other drive

    OS Error 112 occured while Restore
    1. Shrink the data and log file using DBCC ShrinkFile to reclaim space
    2. Move un-necessary files to other drives (.Bakup or etc)

  • Script to list out all DMVs and DMFs of SQL Server


    -- To check the diffrent kind of system objects
    SELECT Distinct type_desc
    FROM sys.system_objects

    -- To get the list of DMVs or DMFs
    SELECT name, type, type_desc,SCHEMA_NAME(schema_id) as SNAME
    FROM sys.system_objects
    WHERE name LIKE 'dm[_]%'
    ORDER BY name