Update Without Where Clause
Running Update Without Where Clause might causes a data damage when it is with the critical table that creates a lot of problems and escalations. Recently while working with one of the customer we had a situation where a DBA executed the release script on production and the script contains an update statement without where clause. He immediately realized that he executed an update statement without where clause and he reported us explaining the situation. There were few things we found:
- The change request implemented on a premium database
- Update statement provided from Pre-Prod team which is not having any control on that:
No Where clause
No Transaction control – Commit / Rollback
No Validation on update – Validating and Reporting
- As per the business requirement single record should be updated but in real it updates all records in that table.
- Usually when a DBA implements a CR on prod server a backup or snapshot has to be taken prior to implementing the change. Since it was a small database (6 GB) he did take a full backup before implementing the CR.
Solution we have provided:
Posted in SQL Development, SQL Server DBA
Tagged 2012, 2014, accidental, data recovery, delete, Mistake, read log file, sql, sql dba, SQL Developer, sql server, update, without where
Script to get row count for all tables in a SQL Server database
This post helps you in writing a Script to get row count for all tables in a SQL Server database. For any database developer or administrator the most common requirement is to identifying the larger tables based on data size, index size and row count. This script can quickly list out all tables in the given database and retrieves the below details:
- Name: Table Name
- Rows: Total row count
- Reserved: Space reserved for the table – MB
- Data: Total space allocated for data pages – MB
- Index_size: Total space allocated for Index pages – MB
- Unused: Unused space – MB
Here is the script to get row count for all tables in a SQL Server database:
SET NOCOUNT ON;
--Create a temparory table
CREATE TABLE #Tab (
--Capture all tables data allocation information
EXEC sp_msForEachTable 'EXEC sp_spaceused ''?''' ;
--Alter Rows column datatype to BIGINT to get the result in sorted order
ALTER TABLE #Tab ALTER COLUMN [ROWS] BIGINT ;
-- Get the final result: Remove KB and convert it into MB
CAST(LTRIM(RTRIM(REPLACE(Reserved,'KB',''))) AS BIGINT)/1024.0 AS 'Reserved MB',
CAST(LTRIM(RTRIM(REPLACE(Data,'KB',''))) AS BIGINT)/1024.0 AS 'Data MB',
CAST(LTRIM(RTRIM(REPLACE(Index_Size,'KB',''))) AS BIGINT)/1024.0 AS 'Index_Size MB',
CAST(LTRIM(RTRIM(REPLACE(Unused,'KB',''))) AS BIGINT)/1024.0 AS 'Unused MB'
ORDER BY [rows] DESC;
DROP TABLE #Tab;
-- Drop the temparory table
DROP TABLE #Tab;
When you need to get the list of tables along with the row counts, connect to the database instance open a new query window, paste the above script and execute it. It shouldn’t take much time to execute the script as we are using “sp_spaceused”, for us it took maximum 6 sec on a 3.5 TB database.
Remember there are chances where sp_spaced might give the wrong row counts due to several reasons. There are mainly 3 reasons that cause sp_spaceused gives us the wrong counts:
- When there are huge DDL changes
- Statistics are not updated
- Index maintenance is not being taken care
Here you will find more details and resolution.
New Features Added in SQL Server
This post helps you in quickly reviewing the “New Features Added in SQL Server” from 2008 to 2016. The most common interview question is “What are the new features added in SQL Server XXXX?” To simplify the answer we are just giving the single line abbreviations. While preparing for an interview just have a quick look and try to remember 4 to 5 features on current working and last released version. Let’s say you are currently working on 2012 then you need to look for 2012 and 2008 R2. Also people would expect you to know few new features added in the latest version which is SQL Server 2016. Features are categorised for DBA, Developer and Security. That doesn’t mean that SQL Developer need not look into DBA section or vice versa. This is just to make it more readable, when a feature is more related to Developer we added in Developer section when a feature is more relevant administration part it will be in DBA section. Believe this will be helpful for a quick review for version wise feature support.
New Features Added in SQL Server 2008
Activity Monitor: Great tool to showcase resource utilization and performance using GUI.
Policy Based Management: The ability to manage standards on multiple servers
Enhanced Database Mirroring: Automatic data page repair and compressing outgoing log stream
Resource Governor: We can configure it to control SQL Server resource utilization and workload.
External Key Management: Provides a comprehensive solution for encryption and key management.
Hot Add CPU: Adding resources online without downtime.
PowerShell: SQL Server 2008 ships with PowerShell snap-in built on .Net framework 2.0.
Table Compression: Compress data and index pages to save memory and I/O.
Backup Compression: Native backup compression.
Performance data collection: Centralized data repository for storing and reporting performance data
Extended Events: Event collection is easier now compares to running a trace