Wednesday, 21 August 2013

SQL SERVER – Fix: Error: 15138 – The database principal owns a schema in the database, and cannot be dropped

Today I was performing audit on Database users and I received below error while deleting the invalid login-

The database principal owns a schema in the database, and cannot be dropped. (Microsoft SQL Server, Error: 15138)

Workaround / Resolution / Fix:
Let us assume that user was trying to delete user which is named as ‘pawan’ and it exists in the database ‘SQLOPSDB’.
Now run following script with the context of the database where user belongs.
USE SQLOPSDB;
SELECT s.name
FROM sys.schemas s
WHERE s.principal_id USER_ID('pawan');

In my query I get following two schemas as a result-

Now let us run following queries where I will take my schema and alter authorization on schema. In my case I have two schemas so I will execute it two times.
ALTER AUTHORIZATION ON SCHEMA::db_denydatareader TO dbo;
ALTER AUTHORIZATION ON SCHEMA::db_denydatawriter TO dbo;
Now if you drop the database owner it will not throw any error.
Below is generic script for resolving the error:
SELECT s.name
FROM sys.schemas s
WHERE s.principal_id = USER_ID('YourUserID');

Now replace the result name in following script:

ALTER AUTHORIZATION ON SCHEMA::YourSchemaName TO dbo;

Thursday, 15 August 2013

CPU Utilization by SQL Server

SQL Server DMV dm_os_ring_buffers stores information about the CPU utilization for 4 hours. Below scripts can be used and stored result in a table to do the analysis on CPU utilization by SQL Server-


/***************************************************************************************************************
Description: This script will display CPU usage on the server.  The data comes from dm_os_ring_buffers which only stored data for the past 4 HOURS.  Within the ring buffers, data is averaged at the minute level.  There are variables limit the results to a smaller time window and for hiding the details.

Sources:
***************************************************************************************************************/

/* Variables */
DECLARE
      @StartTime DATETIME = '01/01/1900 00:00:00'
      ,@EndTime DATETIME = '01/01/2014 23:59:59'
      ,@ShowDetails BIT = 1 -- 1 = True, 0 = False




/* Script Begin */
/* Be careful modifying anything below */


-- Find the timestamp for current server time
DECLARE @ts_now BIGINT
SELECT @ts_now = cpu_ticks / (cpu_ticks / ms_ticks)
FROM sys.dm_os_sys_info;

DECLARE @Results TABLE
      (record_ID BIGINT NOT NULL
      ,EventTime datetime NOT NULL
      ,SQLProcessUtilization tinyint NOT NULL
      ,SystemIdle tinyint NOT NULL
      ,OtherProcessUtilization tinyint NOT NULL
  ) 

INSERT INTO
      @Results
(    
      record_ID
      ,EventTime
      ,SQLProcessUtilization
      ,SystemIdle
      ,OtherProcessUtilization
) 
SELECT
    record_id
   ,DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) AS EventTime
   ,SQLProcessUtilization
   ,SystemIdle
   ,100 - SystemIdle - SQLProcessUtilization AS OtherProcessUtilization
FROM
    (
     SELECT
        record.value('(./Record/@id)[1]', 'int') AS record_id
       ,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/SystemIdle)[1]',
                     'int') AS SystemIdle
       ,record.value('(./Record/SchedulerMonitorEvent/SystemHealth/ProcessUtilization)[1]',
                     'int') AS SQLProcessUtilization
       ,TIMESTAMP
     FROM
        (
         SELECT
            TIMESTAMP
           ,CONVERT(XML, record) AS record
         FROM
            sys.dm_os_ring_buffers
         WHERE
            ring_buffer_type = N'RING_BUFFER_SCHEDULER_MONITOR'
            AND record LIKE '% %'
                  AND DATEADD(ms, -1 * (@ts_now - [timestamp]), GETDATE()) BETWEEN @StartTime AND @EndTime
        ) AS x
    ) AS y


--Return details
IF @ShowDetails = 1
BEGIN
      SELECT    
            record_ID
         ,EventTime
         ,SQLProcessUtilization
         ,SystemIdle
         ,OtherProcessUtilization
      FROM @Results
END


--Return average
SELECT
      AVG(SQLProcessUtilization)
      ,MIN(EVENTTIME) StartTime
      ,MAX(EVENTTIME) EndTime
FROM

      @Results          

How to identify all active SQL Server Sessions

The query below identifies all currently active SQL Server user connections by their SQL Server Login name. It provides details of the IP address that the connection is sourced from, along with the number of sessions and connections that the SQL Server Login is currently responsible for.

SELECT
    B.login_name,
    A.client_net_address,
    NoOfConnections = COUNT(*)
FROM
    sys.dm_exec_connections A
INNER JOIN sys.dm_exec_sessions B ON
A.session_id = B.session_id
GROUP BY
    login_name,
    client_net_address

Memory used by each SQL Server Databases

As you know SQL Server stores database data and index pages in memory in an area know as the Buffer Pool. Using my trusty friend Google, I researched an answer, here you go...


SELECT
(CASE WHEN ([is_modified] = 1) THEN 'Dirty' ELSE 'Clean' END) AS 'Page State',
(CASE WHEN ([database_id] = 32767) THEN 'Resource Database' ELSE DB_NAME (database_id) END) AS 'Database Name',
COUNT (*) AS 'Page Count'
FROM sys.dm_os_buffer_descriptors
GROUP BY [database_id], [is_modified]
ORDER BY [database_id], [is_modified];

GO

Automated Monitoring Database Size Using sp_spaceused

Use this:
exec master.dbo.sp_msforeachdb "exec [?].dbo.sp_spaceused"


For more information please go to the below link:

SQL Server Wait Types - SQL Server Performance Troubleshooting

T-SQL Query to rank SQL Server Wait Types by highest percentage of total wait time

Below query uses the SQL Server Dynamic Management View (DMV) sys.dm_os_wait_stats in order to extrapolate the desired information. A column has also been added to provide details of the percentage of total wait time that a particular Wait Type is responsible for.

SELECT
wait_type,
waiting_tasks_count,
max_wait_time_ms,
resource_wait_time_ms = (wait_time_ms - signal_wait_time_ms),
PercentOfAllResourceWaitTime =
(cast((wait_time_ms - signal_wait_time_ms) as decimal(19,2)) /
(select sum((wait_time_ms - signal_wait_time_ms)) from sys.dm_os_wait_stats))
* 100
FROM sys.dm_os_wait_stats
ORDER BY PercentOfAllResourceWaitTime DESC

Friday, 23 September 2011

Unable to bring a Named Instance of SQL server 2005 Online in Cluster

The problem is that we have a Named instance of SQL Server 2005 in a 2 node cluster. While trying to bring the SQL Services online we get these error messages in the Application Event Log.

Application Event Log: 
Event Type:       Error
Event Source:    MSSQL$SQL2k5
Event Category: (3)
Event ID:          19019
Date:                7/17/2009
Time:                7:12:00 AM
User:                N/A
Computer:         BLRS2R17-3
Description:
[sqsrvres] ODBC sqldriverconnect failed

Event Type:       Error
Event Source:    MSSQL$SQL2k5
Event Category: (3)
Event ID:          19019
Date:                7/17/2009
Time:                7:12:00 AM
User:                N/A
Computer:         BLRS2R17-3
Description:
[sqsrvres] checkODBCConnectError: sqlstate = 08001; native error = ffffffff; message = [Microsoft][SQL Native Client]SQL Network Interfaces: Error Locating Server/Instance Specified [xFFFFFFFF].

Event Type:       Error
Event Source:    MSSQL$SQL2k5
Event Category: (3)
Event ID:          19019
Date:                7/17/2009
Time:                7:12:00 AM
User:                N/A
Computer:         BLRS2R17-3
Description:
[sqsrvres] ODBC sqldriverconnect failed

Event Type:       Error
Event Source:    MSSQL$SQL2k5
Event Category: (3)
Event ID:          19019
Date:                7/17/2009
Time:                7:12:00 AM
User:                N/A
Computer:         BLRS2R17-3
Description:
[sqsrvres] checkODBCConnectError: sqlstate = HYT00; native error = 0; message = [Microsoft][SQL Native Client]Login timeout expired


Cause:
The cluster service is not able to Connect with Clustered Named instance of SQL Server. SQL Browser even though running is not actually listening for requests.  

Note: The SQL Server Browser program runs as a Windows service. SQL Server Browser listens for incoming requests for Microsoft SQL Server resources and provides information about SQL Server instances installed on the computer.

Resolution:

We restarted the SQL Server Browser Service on both the nodes and after that SQL resource came online in cluster. :)