stormtrooper-1343772_640 SQL Server Section

Another way in SQL to Delete Duplicate Records

Using a CTE to delete duplicates Some of my other posts show how to find and delete duplicate records from SQL Server. This is another way to delete duplicate records using a CTE. A Common Table Expression (CTE) is a named result set in a SQL query. CTEs help keep your code organized and allow you to perform multi-level aggregations on your data, like finding the average of a set of counts. WITH cte      AS (SELECT ROW_NUMBER() OVER (PARTITION BY Col1, Col2, Col3                                        ORDER BY ( SELECT 0)) RN          FROM   #MyTable) DELETE FROM cte WHERE  RN >…

Read More
paperclip-168336_640 SQL Server Section

SQL Records Per Table

Why you need to know There are lots of reasons why you might want to know the number of records in your tables. Database maintenance is one; user and business interest is another – I have been asked how many records are in certain tables for sales calls, blog posts, invoicing and many other reasons. How do you count the number of records? If you only want to know the number of records in a single table, you just run this command: – SELECT COUNT(1) FROM [Table] If you want to know the number of records in all tables, you…

Read More
matrix-4980033_640 SQL Server Section

Delete a constraint on a table / field combination without knowing the name of the constraint

Overview In database management, especially when dealing with SQL Server, modifying or removing default constraints is a common task. This blog post will delve into a specific SQL script designed to drop a default constraint from a table column. This operation is crucial when you need to change the default value of a column or remove the default setting entirely. Understanding Constraints Before diving into the script, let’s briefly touch upon what a default constraint is. In SQL Server, a default constraint is used to set a default value for a column. This means that the default value is automatically…

Read More
recipe-312959_640 SQL Server Section

SQL Indexes and Check if One Exists

Managing a database efficiently often requires a deep understanding of its structure and components, particularly indexes. In SQL Server, indexes are crucial in optimizing query performance and ensuring data integrity. This blog post will explore a specific SQL query that helps database administrators and developers understand and manage indexes more effectively. Introduction to SQL Server Indexes Indexes in SQL Server are database objects that improve the speed of data retrieval. They are essential for large tables, where they can significantly reduce the time it takes to search for and retrieve data. Check if an Index Exists Here’s the query we’ll…

Read More
cup-617422_640 SQL Server Section

How to duplicate SQL Rows

In the world of database management, efficiency and precision are key. With its versatile commands, SQL offers a wide range of functionalities for handling data. One such functionality is the combination of the INSERT INTO and SELECT statements, a powerful tool for duplicating or reorganizing data within a database. This blog post will explore how this combination can be used effectively, as illustrated by a specific SQL code example. The SQL Code in Focus Let’s take a look at the SQL code: INSERT INTO Table           ( Event_ID           , col2            …           )      SELECT "155"           ,…

Read More
isolated-316392_640 SQL Server Section

Restore a SQL database

In the realm of database administration, one of the most critical tasks is the ability to restore databases efficiently and accurately. This blog post will delve into an essential aspect of SQL Server management: restoring a database from a backup file. We will specifically look at two variations of the RESTORE DATABASE command, using the well-known AdventureWorks2012 database as an example. Understanding the Basics of Database Restoration Database restoration is the process of bringing a database back to its previous state by using a backup. This operation is crucial for data recovery in cases of data loss or corruption or…

Read More
1 2 3 4 5