Programming

Who Deleted Rows from SQL Server

Posted on

Want to know who deleted rows from your SQL database? The script below joins up delete transactions with users in order to find out who just deleted something. Because this comes from the transaction log, you’ll only find stuff that is still available in there. This means it will be affected by retention/recovery modes. If […]

Programming

SQL Server: Filter and Sort Records from sp_who2

Posted on

Sometimes you need to filter and sort the records you get from sp_who2. You can’t do this directly, but you can use a temporary table to do it. — Create an intermediate table to put all the results CREATE TABLE #spootoo ( SPID INT, Status VARCHAR(1000) NULL, Login SYSNAME NULL, HostName SYSNAME NULL, BlkBy SYSNAME […]

Programming

SQL Availability Groups: Find the Primary Server

Posted on

Here is a quick SQL script that will get back information on SQL availability groups, including the role of each server. This can help you find the primary server in an availability group. IF SERVERPROPERTY (‘IsHadrEnabled’) = 1 BEGIN SELECT AvailabilityGroup.name AS AvailabilityGroupName, ReplicaClusterStates.replica_server_name as ServerName, ReplicaStates.role_desc as ReplicaRole, Listeners.dns_name as DnsName FROM sys.availability_groups_cluster AS […]

Programming

SQL Server: Quickly Find Row Counts and Table Size

Posted on

I have a script I use to find table sizes in SQL server. Sometimes, though, I need to find the rough table size of a massive table without the need for absolute precision. SQL server has a procedure for finding out the number of rows, space, and index size of a table; and it can […]

Programming

SQL Bulk Update in Batches

Posted on

When you perform a SQL bulk update, you can just press go and wait. Most humans, though, get an increasing feeling of impending doom when the clock ticks up towards several hours with no visible progress. If you are running a transaction and have other things competing for the table, you’ll start hearing about slow […]

Programming

SQL Server Index Fragmentation

Posted on

I was investigating an issue with am Azure SQL Server database that was much bigger than it ought to be. It was around 50 GB, but should really have been about 15 GB. I ran an old query I keep lying around that finds the size of tables in a SQL database and there was […]

Programming

SQL Server Replication and Non-Clustered Indexes

Posted on

If you want non-clustered indexes to be copied to your replication slaves (you probably do) you simply adjust the setting that you’ll find here… SQL -> Replication -> Local Publications -> Publication Name -> Properties -> Articles -> Article Properties -> Copy nonclustered indexes When you set this to “True”, the indexes will be copied. […]