Tag: T-SQL

Core Transact-SQL concepts, patterns, and examples used in day-to-day SQL Server administration, querying, troubleshooting, and scripting.

  • Deleting Rows in Batches in SQL Server

    Deleting large volumes of data from a SQL Server table looks simple, but it can cause real problems if done carelessly. A single large DELETE can: When you’re cleaning up historical data or running routine maintenance, deleting rows in batches is usually the safest and most predictable approach. This post shows a simple, production-safe pattern…

  • RAND() vs NEWID() in SQL Server

    Most people searching for “random numbers in SQL Server” want one of two things: SQL Server gives you two common tools for this: RAND() and NEWID(). They solve different problems, and confusion usually starts when they are treated as interchangeable. This post shows how to use both, starting with simple examples, and explains why they…

  • Applying Data Retention Safely in SQL Server

    Data retention in SQL Server usually means deleting old data in a controlled and repeatable way. That might be driven by compliance requirements, table growth that’s starting to hurt performance, SQL Server Express size limits, or simply keeping log and audit tables under control. Whether this is a one-off cleanup or a permanent maintenance task,…

  • Checking Table Sizes in SQL Server

    Checking table sizes in SQL Server is a routine DBA task that supports capacity planning, performance troubleshooting, and data retention work. There are several ways to get this information. Some methods are better suited to automation and reporting, while others are quicker for ad hoc checks in SQL Server Management Studio. This post walks through…

  • Understanding WHERE 1=1 and WHERE 1=2 in SQL Queries

    In SQL, you will often see queries written using WHERE 1=1 or WHERE 1=2. At first glance, both look pointless. One is always true, the other always false. In practice, both are deliberate patterns used for query construction and control, not filtering logic. They exist to make SQL safer, easier to manipulate, and more predictable…

  • What the USE Command Does in SQL Server

    The USE command changes the database context for the current session. Every statement in SQL Server runs in the context of a database. When you run USE, you are telling SQL Server which database subsequent statements should execute against until the context changes again or the session ends. That single behaviour explains a lot of…