Tag: SQL Tips and Tricks

  • Monitoring and Troubleshooting using sys.dm_os_ring_buffers

    sys.dm_os_ring_buffers: You can use the undocumented Ring Buffer DMV to troubleshoot the below issues.

    • Security Exceptions
    • Exception raised at SQL Operating System level
    • Connection Dropped By the Server
    • System Resource Utilization
    • Memory Pressure
    • CLR Integration Scheduler State
    • Extended Events Subsystems State

    Execute the below query to get the distinct ring buffer type.

    select distinct ring_buffer_type from sys.dm_os_ring_buffers
    
    • RING_BUFFER_RESOURCE_MONITOR
    • RING_BUFFER_SCHEDULER_MONITOR
    • RING_BUFFER_MEMORY_BROKER
    • RING_BUFFER_SECURITY_ERROR
    • RING_BUFFER_XE_BUFFER_STATE
    • RING_BUFFER_SCHEDULER
    • RING_BUFFER_CONNECTIVITY
    • RING_BUFFER_EXCEPTION
    • RING_BUFFER_XE_LOG

    Check below script as example to troubleshoot the Security Issue using ring buffer. You can change the ring buffer type in below script to troubleshoot the different issues.

    -- Check the Ring Buffer in SQL Server 2008
    
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    SET ANSI_WARNINGS ON
    SET ANSI_PADDING ON
    
    SELECT CONVERT (varchar(30), GETDATE(), 121) as Run_Time,
    dateadd (ms, (ST.[RecordTime] - sys.ms_ticks), GETDATE()) as [Notification_Time],
    ST.* , sys.ms_ticks AS [Current Time]
    FROM
    (SELECT
    RBXML.value('(//Record/Error/ErrorCode)[1]', 'varchar(30)') AS [ErrorCode],
    RBXML.value('(//Record/Error/CallingAPIName)[1]', 'varchar(255)') AS [CallingAPIName],
    RBXML.value('(//Record/Error/APIName)[1]', 'varchar(255)') AS [APIName],
    RBXML.value('(//Record/Error/SPID)[1]', 'int') AS [SPID],
    RBXML.value('(//Record/@id)[1]', 'bigint') AS [Record Id],
    RBXML.value('(//Record/@type)[1]', 'varchar(30)') AS [Type],
    RBXML.value('(//Record/@time)[1]', 'bigint') AS [RecordTime]
    FROM (SELECT CAST (record as xml) FROM sys.dm_os_ring_buffers
    WHERE ring_buffer_type = 'RING_BUFFER_SECURITY_ERROR') AS RB(RBXML)) ST
    CROSS JOIN sys.dm_os_sys_info sys
    ORDER BY ST.[RecordTime] ASC
    
    -- Script to Check the Ring Buffer in SQL Server 2005
    
    SET ANSI_NULLS ON
    SET QUOTED_IDENTIFIER ON
    SET ANSI_WARNINGS ON
    SET ANSI_PADDING ON
    
    SELECT CONVERT (varchar(30), GETDATE(), 121) as runtime,
    DATEADD (ms, -1 * ((sys.cpu_ticks / sys.cpu_ticks_in_ms) - ST.[RecordTime]), GETDATE()) AS NotificationTime,
    ST.* , sys.ms_ticks AS [CurrentTime]
    FROM
    (SELECT
    RBXML.value('(//Record/Error/ErrorCode)[1]', 'varchar(30)') AS [ErrorCode],
    RBXML.value('(//Record/Error/CallingAPIName)[1]', 'varchar(255)') AS [CallingAPIName],
    RBXML.value('(//Record/Error/APIName)[1]', 'varchar(255)') AS [APIName],
    RBXML.value('(//Record/Error/SPID)[1]', 'int') AS [SPID],
    RBXML.value('(//Record/@id)[1]', 'bigint') AS [Record Id],
    RBXML.value('(//Record/@type)[1]', 'varchar(30)') AS [Type],
    RBXML.value('(//Record/@time)[1]', 'bigint') AS [RecordTime]
    FROM (SELECT CAST (record as xml) FROM sys.dm_os_ring_buffers
    WHERE ring_buffer_type = 'RING_BUFFER_SECURITY_ERROR') AS RB(RBXML)) ST
    CROSS JOIN sys.dm_os_sys_info sys
    ORDER BY ST.[RecordTime] ASC
    
    

    From the output we can see the hexadecimal error code 0x6FD. You have to convert these error code into decimal value, which will be 0x6FD = 1789

    Check the above decimal error codes using the NET HELPMSG command, which will give you more information on the issue.

  • Steps to add Log Shipping monitor into an existing SQL Server

    Problem
    I have a requirement to add the Log Shipping Monitor for an existing installation. I have heard you can only complete this by rebuilding the Log Shipping infrastructure. Is that true? Are there any other options? In this tip I will explain how we can add the Log Shipping monitor to a SQL Server 2005, 2008, 2008 R2 or 2012 environment without rebuilding the Log Shipping installation.

    Solution
    http://www.mssqltips.com/sqlservertip/2799/steps-to-add-log-shipping-monitor-into-an-existing-sql-server/

  • Script to Create Foreign Key on the Compound Primary Key

    Compound Primary key is a primary key which is created on more than one column. Now the questions is how to create the foreign key for the compound primary key where it references more than one column.

    Check the below example.

    create table employee
    (
    	empID int not null,
    	SSN int not null,
    	name varchar(20)
    )
    
    
    ALTER TABLE [employee]
    ADD CONSTRAINT pk_employee PRIMARY KEY (empID, SSN)
    
    
    create table EmpDetail
    (
    		empID int,
    		SSN int,
    		address varchar(20),
    		city varchar(20),
    		pin varchar(20)
    )
    
    ALTER TABLE dbo.empDetail
       ADD CONSTRAINT FK_Employee
       FOREIGN KEY(empID, SSN)
       REFERENCES dbo.employee(empID, SSN)
    
    
    SELECT
        tc.TABLE_NAME,
        tc.CONSTRAINT_NAME, 
        ccu.COLUMN_NAME
    FROM 
        INFORMATION_SCHEMA.TABLE_CONSTRAINTS tc
    INNER JOIN 
        INFORMATION_SCHEMA.CONSTRAINT_COLUMN_USAGE ccu 
          ON ccu.TABLE_NAME = tc.TABLE_NAME AND ccu.CONSTRAINT_NAME = tc.CONSTRAINT_NAME
    WHERE
        tc.TABLE_NAME IN ('employee','employeeDetail')
    
    
  • Primary Key, Unique Key Constraints – Clustered Index and Non Clustered Index

    You can use the below script to create the Primary Key on the already existing tables. Primary key enforces a uniqueness in the column and created the clustered index as default.

    Primary key will not allow NULL values.

    -- Adding the NON NULL constraint
    ALTER TABLE [TableName]	 
    ALTER COLUMN PK_ColumnName int NOT NULL
    
    --Script to add the primary key on the existing table
    ALTER TABLE [TableName]
    ADD CONSTRAINT pk_ConstraintName PRIMARY KEY (PK_ColumnName)
    

    If you want to define or create the non-clustered index on the existing table, you can use the below script. If the data in the column is unique, you can create the Unique Constraint as well.

    Unique Key enforces uniqueness of the column on which they are defined. Unique Key creates a non-clustered index on the column. Unique Key allows only one NULL Value.

    --script to create non-clustered Index
    create index IX_ColumName on TableName(ColumnName)
    --script to create Unique constraint on the existing table
    ALTER TABLE TableName ADD CONSTRAINT ConstraintName UNIQUE(ColumnName)
    
  • How to insert value into IDENTITY column?

    If you will try to insert the value into Identity column you will get the one of the below error.

    Error 1:
    Msg 544, Level 16, State 1, Line 1
    Cannot insert explicit value for identity column in table ‘Employee’ when IDENTITY_INSERT is set to OFF.

    Error 2:
    Error 8101 An explicit value for the identity column in table can only be specified when a column list is used and IDENTITY_INSERT is ON

    Solution:
    Write SET IDENTITY_INSERT table name ON before the insert script and SET IDENTITY_INSERT table name Off after insert script.

    Example,

    use db1
    
    create table Employee
    (
    	myID int identity(100,1),
    	name varchar(20)
    )
    
    insert into Employee(name) values('Jugal')
    
    --if i will try to insert the value into Identity column it will fail
    insert into Employee(myID,name) values (101,'DJ')
    
    --you can add the data into identiy column by turning on the IDENTITY_INSERT ON
    
    SET IDENTITY_INSERT Employee ON
    	insert into Employee(myID,name) values (101,'DJ')
    SET IDENTITY_INSERT Employee OFF