The mystique of the DELETE FROM FROM

Recently I came across a CRM query which used FROM twice in the DELETE statement.

Below is an example of it in action and yes a warning the results can be a bit perplexing.
 

-- Create a table
 CREATE TABLE your_table
 (
  id int IDENTITY (1,1)
 ,y_n char(1) NULL
 );
 GO
 -- Create a view
 CREATE VIEW your_view
 AS
 SELECT id,y_n
 FROM your_table
 WHERE y_n = 'y';
 GO

 -- Insert some data into the table
 INSERT INTO your_table (y_n)
 VALUES
  ('y')
 ,('y')
 ,('y')
 ,('y')
 ,('n');
 GO

image1_1

-- First DELETE test
-- With the view after the first FROM with 'n'
BEGIN TRAN
DELETE 
FROM your_view
FROM your_table t
WHERE t.y_n = 'n';

 -- You get 1 row returned
SELECT * FROM [your_table] WITH (NOLOCK);

ROLLBACK

image2

-- Second DELETE test
-- With the view after the first FROM with 'y'
BEGIN TRAN
DELETE 
FROM your_view
FROM your_table t
WHERE t.y_n = 'y';

-- You also get the same row returned
SELECT * FROM your_table WITH (NOLOCK);

ROLLBACK

image3

-- Third DELETE test
-- With the table after the first FROM with 'n'
BEGIN TRAN
DELETE 
FROM your_table
FROM your_view v
WHERE v.y_n = 'n';

-- You get all rows returned
SELECT * FROM your_table WITH (NOLOCK); 

ROLLBACK

image4

-- Final DELETE test
-- With the table after the first FROM with 'y'
BEGIN TRAN
DELETE 
FROM your_table
FROM your_view v
WHERE v.y_n = 'y';

-- All rows are now deleted
SELECT * FROM your_table WITH (NOLOCK);

ROLLBACK

image5
To remove the table and view use:

DROP VIEW your_view;
DROP TABLE your_table;

image6

Microsoft Azure SQL Server 2014 AlwaysOn Gallery option

Microsoft has recently added SQL Server 2014 AlwaysOn to the Microsoft Azure Gallery and the following screenshots go through the process of using it with the default settings.

The follow description is supplied from the gallery:

‘Automates the deployment of a SQL Server AlwaysOn Availability Group for high availability of SQL Server. It provisions 2 SQL Server 2014 Enterprise replicas (primary and secondary) and 1 witness file share in a Windows Cluster. It also provisions 2 Domain Controller replicas (primary and secondary). In addition, it configures an Availability Group Listener for clients to connect to the primary SQL Server replica.’

It should be noted that this gallery option will be very expensive to run long term due to the included Microsoft SQL licence costs. I suspect Microsoft should also offer a SQL Server 2014 Evaluation Edition alternative to allow cheaper testing and the ability to use in-house SQL licensing if the testing works out.

image1
image2
image3
image4
Warning: As mentioned, this will be expensive to run long term as shown in the pricing below (as at 23/08/2014):
image5
image6
image7
image8
image9
image10
From clicking Create the process took ~ 2 hours.

You will see the following progress on the Startboard:
image11
The Notifications section shows more info on the creation process
image12
image13
Here is what gets created:
image14
and SQL Server looks like:
image15

SQL Server ODBC system error code 126

Recently at a site which had Windows Server 2003 R2 64bit SP2, the following error message was received when attempting to create a new SQL Server ODBC data source:

The setup routines for the SQL Server ODBC driver could not be loaded due to system error code 126.

image1
image2
image3
The resolution in this case was to locate, right click on C:\WINDOWS\inf\mdac.inf and select Install.
image4
You may need to locate the media to complete the process.
image5

Object Explorer Details footer section

An often over looked feature of SQL Server Management Studio is the footer section of Object Explorer Details.

You can open Object Explorer Details via the View menu or pressing F7.

image1

Depending on where you click in Object Explorer and Object Explorer Details, the footer in Object Explorer Details changes.

Some examples are:

image2

image3

image4

In addition to the footer; there are hidden columns in Object Explorer Details, via a right click, which may make your investigative life a bit easier i.e.

image5
image6

ssms –log

If you haven’t come across the –log switch before, here are a few basic screen shots of it in action on SQL Server 2014.

image1

I didn’t supply a log file to use, so the 2 files ActivityLog.xml and ActivityLog.xsl were created by default under C:\Users\yourdba\AppData\Roaming\Microsoft\AppEnv\10.0

image2

Opening the ActivityLog.xml file using Internet Explorer gives the following info:
(click to see a larger image)

image3

So, if you’re having issues with ssms and your version supports the log switch, then this may be of some use to you.

Setting the database owner using an existing user in the database without stopping the application

You may come across a request to set an existing user in a database to be the owner. The user is being used by an application and the application cannot have an outage.

The main issue here is that the database user has to first be dropped from the database before it can be made the owner. I’m not talking about the db_owner database role, but rather the actual database owner.

This begs the question of does the existing connection drop when the user is removed from the database?

To test this, below is a user called youruser which is a member of the db_owner database role in your_database_1.

image1

Start a long running query using youruser in the database.

image2

From another session drop youruser from the database.

image3

As soon as you do this, the youruser query fails.

image4

Hmmm, so how do you get around setting this user as the owner without stopping the application?

One way is to temporarily elevate the youruser privileges e.g.
 

-- Temporarily elevate the youruser privileges
ALTER SERVER ROLE [sysadmin] ADD MEMBER [youruser];

-- Remove youruser from the database
USE [your_database_1];

-- Note you may need to drop youruser's schema in the database prior to removing youruser
-- DROP SCHEMA [youruser];
DROP USER [youruser];

-- Make youruser the database owner
ALTER AUTHORIZATION ON DATABASE::[your_database_1] TO [youruser];

-- Revoke the Temporarily elevated privileges
ALTER SERVER ROLE [sysadmin] DROP MEMBER [youruser];

image5

And there you have it, the query has not stopped and the owner has been successfully changed.

image6

Granting read-only access to multiple databases in SQL Server 2014

You may have a group of users which require read-only access to multiple or all databases in a SQL Server instance.
 
Typically this would be performed by adding the users, or preferably an AD security group which contains the users, to the db_datareader fixed database role in each database.
 
With SQL Server 2014, the Select All User Securables permission can simplify this process.
 
As shown below, select the properties of the login.
 
image1
 
Grant the login the Select All User Securables permission.
 
image2
 
Simply tick the Map option next to each database the login requires read-only access to and select OK.
 
image3
 
Now when the user connects to SQL Server, they will be able to query user tables and views in the databases they have been granted access to. They will not be able to insert, delete or modify data unless these privileges are specifically granted.
 
image4
 
If the user attempts to access a database which they have not been granted access to, they will receive an error e.g.
 
image5
 
If the login is also granted the Connect Any Database permission in addition to Select All User Securables, then the login could access and read user tables and views in all databases without requiring the databases to be selected in the User Mapping tab.
 
image6

sys.fn_translate_permissions example

I couldn’t locate an output example of the function sys.fn_translate_permissions or how to really use it and hence this blog.
 
You’ll likely never have a need to use this function, but here goes…
 
The function has 2 arguments
 
level – which is nvarchar(60) and relevant entries can be found in sys.securable_classes
 
perms – which is varbinary(16) and integers up to 38 precision can be used e.g. 99999999999999999999999999999999999999 is ok, but adding another 9 would fail.
 
Below is the function in action:
 
image1

Check CPU clock speed using WMI

The following simple WMI query can help you quickly check if the CPUs in your SQL server are using the maximum clock speed and are not in power saving mode.
 
The following works on Windows 2003 and higher:
 
wmic path win32_processor get CurrentClockSpeed, MaxClockSpeed, Name
 
image1
 
If you’re running Windows 2008 and higher, then there are some extra columns which may be of interest to you i.e.
 
wmic path win32_processor get CurrentClockSpeed, MaxClockSpeed, Name, NumberOfCores, NumberOfLogicalProcessors
 
image2

Identify and change a SQL Server endpoint owner

It is quite common for a DBA or consultant to setup database mirroring or an always on availability group and then leave the organization.
 
When it is time to clean up SQL Server logins on your SQL Server you discover, that although you have revoked access for the login to all databases and server roles, you are still unable to delete the login as it is reported to own an endpoint.
 
Below shows the error reported in SQL Server 2014.
 
image1
 
To confirm the endpoint owner you could use the following:
 

USE master;
SELECT 
 SUSER_NAME(principal_id) AS endpoint_owner
,name AS endpoint_name
FROM sys.database_mirroring_endpoints;

 
image2
 
To change the endpoint owner to sa for an endpoint called Mirroring you would use the following:
 

USE master;
ALTER AUTHORIZATION ON ENDPOINT::Mirroring TO sa;

 
image3
 
Confirm that the endpoint is now owned by sa:
 
image4
 
The legacy DBA or consultant’s login can now be removed from the SQL Server.