<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: nisargupadhyay87</title>
    <description>The latest articles on DEV Community by nisargupadhyay87 (@nisargupadhyay87).</description>
    <link>https://dev.to/nisargupadhyay87</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F862072%2Fa7bdbe7d-32c1-40f7-836e-f9d63eacc80d.jpg</url>
      <title>DEV Community: nisargupadhyay87</title>
      <link>https://dev.to/nisargupadhyay87</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/nisargupadhyay87"/>
    <language>en</language>
    <item>
      <title>Step-by-Step Guide to Restoring SQL Server Databases Without Transaction Log Backup</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Fri, 12 Sep 2025 08:22:23 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/step-by-step-guide-to-restoring-sql-server-databases-without-transaction-log-backup-317a</link>
      <guid>https://dev.to/nisargupadhyay87/step-by-step-guide-to-restoring-sql-server-databases-without-transaction-log-backup-317a</guid>
      <description>&lt;p&gt;Restoring an SQL Server database is one of the most critical tasks for DBA. In ideal scenarios, we use the full, differential, and transaction log backups to bring databases back online. However, in some cases, log backups are not available due to incomplete backups, accidental deletions, or damaged transaction log files. We can restore databases without log backups, but it has some technical challenges and a certain risk of data loss. &lt;br&gt;
In this article, we’ll understand the step-by-step process to restore any database without the transaction log backups. Also, we will go-through the technical challenges that we might encounter while restoring the database without transaction log backup. First, let us understand how unavailability of transaction log backups can impact your recovery strategy.&lt;/p&gt;
&lt;h2&gt;
  
  
  When Do You Need to Restore Without Log Backups?
&lt;/h2&gt;

&lt;p&gt;You might be forced into this scenario in cases such as:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The database recovery model is set to SIMPLE, where log backups are useless.&lt;/li&gt;
&lt;li&gt;The transaction log backup chain is broken due to missing or corrupted transaction log files.&lt;/li&gt;
&lt;li&gt;The log backup is deleted by mistake. Sometimes, when we run out of storage, someone may delete the log backups without verifying the full and differential backups.&lt;/li&gt;
&lt;li&gt;The database is already in FULL recovery mode, but the organization did not configure the log backups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In these situations, you can only rely on full and differential backups for recovery. Here is the step-by-step process of restoring a database without transaction log backups.&lt;/p&gt;
&lt;h2&gt;
  
  
  Steps to restore the database
&lt;/h2&gt;

&lt;p&gt;Let us understand the step-by-step process to restore the database without transaction log. For demonstration, we will be using the Stackoverflow2010 database. &lt;br&gt;
First, we need to check the recovery model of the database. If the database is in SIMPLE recovery model, then the log backups will not be needed.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select database_id, name, create_date, recovery_model_desc from sys.databases where name='Stackoverflow2010'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F773hdrf1l4kcqxmwypmn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F773hdrf1l4kcqxmwypmn.png" alt=" " width="800" height="254"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the database is in FULL recovery model. Now let us confirm the last full, differential and transaction log backup. Here is the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE msdb;
GO

;WITH BackupHistory AS
(
    SELECT 
        bs.database_name,
        bs.backup_start_date,
        bs.backup_finish_date,
        bs.type,
        bmf.physical_device_name,
        ROW_NUMBER() OVER (PARTITION BY bs.database_name, bs.type ORDER BY bs.backup_finish_date DESC) AS rn
    FROM dbo.backupset bs
    INNER JOIN dbo.backupmediafamily bmf 
        ON bs.media_set_id = bmf.media_set_id
)
SELECT 
    database_name,
    CASE type
        WHEN 'D' THEN 'Full Backup'
        WHEN 'I' THEN 'Differential Backup'
        WHEN 'L' THEN 'Transaction Log Backup'
    END AS backup_type,
    backup_start_date,
    backup_finish_date,
    physical_device_name
FROM BackupHistory
WHERE rn = 1
  AND type IN ('D','I','L') and database_name='Stackoverflow2010'
ORDER BY database_name, backup_type;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah1w44djjsrfgmrtkbjt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fah1w44djjsrfgmrtkbjt.png" alt=" " width="800" height="101"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, we do not have the transaction log available. Hence, we must rely on the full and differential backup. As per the process, we will restore a full backup with NORECOVERY option. The NORECOVERY option will allow us to apply differential and log backups. Here is the script to restore the full backups.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE [master]
Go
RESTORE DATABASE [Stackoverflow2010] FROM  DISK = N'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_full_backup.bak' WITH  FILE = 1,  
MOVE N'TTIConfig' TO N'D:\MS_SQL\DATA\Stackoverflow2010.mdf',  MOVE N'TTIConfig_log' TO N'D:\MS_SQL\LOG\Stackoverflow2010_1.ldf',  NOUNLOAD,  STATS = 5
GO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the full backup is restored, we must restore the differential backup. As we do not have any transaction log backup, we will restore the differential backup using WITH RECOVERY option. The WITH RECOVERY option will complete the restoration process and bring database online. You cannot apply any differential or transaction log backups on it. Here is the script.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE [master]
GO
RESTORE DATABASE [Stackoverflow2010] FROM  DISK = N'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_differential_backup.bak' WITH  FILE = 1,  NOUNLOAD,  STATS = 5
GO

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the process is completed, check the status of the database.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select database_id, name, create_date, recovery_model_desc, state_desc from sys.databases where name='Stackoverflow2010'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query Output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fma2mqbfwnk91ouhfkzf1.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fma2mqbfwnk91ouhfkzf1.png" alt=" " width="800" height="267"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, the database is online. The bad news is that we could not restore the database till point of failure. &lt;/p&gt;

&lt;h2&gt;
  
  
  Challenges of Restoring Without Log Backups
&lt;/h2&gt;

&lt;p&gt;Restoring without transaction log backups introduces multiple DBA-level challenges:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You cannot restore the database to a specific moment before the failure. You can only recover data till the last full or differential backup.&lt;/li&gt;
&lt;li&gt;If your last backup is old, you may lose hours or even days of transactions / data. For example, you are taking weekly full and daily differential, then you can recover the data till the last successful differential backup.&lt;/li&gt;
&lt;li&gt;If the database is damaged or the log file is missing, you cannot back up the tail of the log. It leads to permanent transaction loss.&lt;/li&gt;
&lt;li&gt;If differential backups are missing or corrupted, you must use the last full backup. Restoring a full backup increases the downtime and major data loss.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Best Practices to Prevent This
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Always use FULL recovery model for production databases and configure maintenance plans to generate the transaction log backups. If you are using SIMPLE recovery model, take frequent differential backup to minimize the data loss.&lt;/li&gt;
&lt;li&gt;Always automate the backup process. You can use SQL Server maintenance plans, Windows task schedular, or SQL Server agent jobs.&lt;/li&gt;
&lt;li&gt;Validate backups with RESTORE VERIFYONLY and periodic test restores. You can also configure the backup validation process in database maintenance plans. If you have additional server available, regularly test the restore process.&lt;/li&gt;
&lt;li&gt;Store backup on separate and secure location. You can use cloud storage which is more reliable.&lt;/li&gt;
&lt;li&gt;Use monitoring tools (SQL Agent jobs, custom scripts, or third-party solutions like &lt;a href="https://www.stellarinfo.com/sql-database-toolkit.php" rel="noopener noreferrer"&gt;Stellar&lt;/a&gt;, Redgate, or SQL Monitor) to detect missing backups. If you cannot invest money in monitoring tools, create custom scripts that send the alerts if the backup fails.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Restoring SQL Server databases without log backups is possible, but it comes at the cost of higher data loss, downtime, and broken SLAs. While the steps are straightforward (full → differential → recovery), the operational risks are significant. A solid backup strategy including frequent transaction log backups is the only way to ensure reliable point-in-time recovery and avoid these pitfalls in production environments. Now, if the database is corrupted and you do not have backups available, you can use professional tools like &lt;a href="https://www.stellarinfo.com/sql-database-toolkit.php" rel="noopener noreferrer"&gt;Stellar Data recovery software&lt;/a&gt;. It has many features that help to recover the SQL database. Along with database files, you can also repair the corrupted backup files.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Recover SQL Server Database After Backup Corruption</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Tue, 02 Sep 2025 10:11:13 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/how-to-recover-sql-server-database-after-backup-corruption-46i6</link>
      <guid>https://dev.to/nisargupadhyay87/how-to-recover-sql-server-database-after-backup-corruption-46i6</guid>
      <description>&lt;p&gt;Backups are the most important asset of any organization that uses software. The backups are essential to recover the critical business data that has been corrupted or deleted due to hardware failures, accidental deletions, or catastrophic events. However, sometimes backup files themselves become corrupted due to many reasons. It hinders the entire database restore strategy and might cause a lot of financial issues to any organization.&lt;br&gt;
This article explains what backup corruption is, why it happens, and provides a step-by-step guide to recover SQL database after backup corruption. We will also learn the potential reason which we might encounter while restoring the backup. &lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding Backup Corruption in SQL Server
&lt;/h2&gt;

&lt;p&gt;In simple words, a corrupt backup means a corrupted *.bak file which we are unable to restore. The backup can be corrupted due to many reasons.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Hardware issues – The backup might be corrupted due to the hardware issues like the storage sub systems where the backups are being stored. The hardware can fail due to many factors which can lead to the database backup corruption.&lt;/li&gt;
&lt;li&gt;Software problems – Sometimes the backup process get interrupted due to SQL Server instance crashed while backup is in process. &lt;/li&gt;
&lt;li&gt;Incomplete backups – The backup can get corrupted when the backups process is not completed due to the network latency or insufficient space in backup storage.&lt;/li&gt;
&lt;li&gt;Malware or viruses – The malwares or viruses can also corrupt the backup files. Sometimes due to weak security, the hackers might be able to access the backup files and use certain tools which corrupt the backups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The backups are very important because in case of database corruption or failure, we will be able to recover the data from backups only. If the backups are corrupted and you do not have any alternate solution to recover the data, there will be an irreversible data loss. &lt;/p&gt;
&lt;h2&gt;
  
  
  Prerequisites for SQL Database Recovery
&lt;/h2&gt;

&lt;p&gt;Before attempting to recover the database, we must check the reasons for database backup corruption, find the valid backup to begin the restoration process. &lt;/p&gt;
&lt;h3&gt;
  
  
  Identify the corruption source
&lt;/h3&gt;

&lt;p&gt;We can use any of the methods to identify the corruption source.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;The details of the failed or interrupted backups are available in windows event viewer or the SQL Server error logs. If you are using SQL Scripts to take the backup, you can incorporate the logging mechanism in script.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can also schedule a SQL Server agent job or windows task scheduler job to verify the integrity of the backup file. You can check the integrity of the backup by using RESTORE HEADERONLY or RESTORE VERIFYONLY statement. Here is the syntax of both commands.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RESTORE HEADERONLY FROM DISK = N'location_of_backup_file';
GO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;


&lt;p&gt;You can read more about RESTORE HEADERONLY command &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/restore-statements-headeronly-transact-sql?view=sql-server-ver17" rel="noopener noreferrer"&gt;here &lt;/a&gt;and RESTORE VERIFYONLY &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/statements/restore-statements-verifyonly-transact-sql?view=sql-server-ver17" rel="noopener noreferrer"&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The next step is to prepare the environment to begin the database restoration.&lt;/p&gt;
&lt;h3&gt;
  
  
  Preparing for the environment
&lt;/h3&gt;

&lt;p&gt;Once the backup files have been identified, we should start preparing the database server. Along with provisioning the database server, we should take care of following items:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server on which you are restoring the database should have adequate space. Also, the storage which server is using must not have any disk-level corruption.&lt;/li&gt;
&lt;li&gt;The correct version of SQL Server must be installed. Also, verify that the resources allocated to the server are adequate, so the restoration process is not interrupted due to lack of CPU and memory.
&lt;/li&gt;
&lt;li&gt;If the database is partially accessible, place the database in a safe state for recovery.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here is the step-by-step guide to recover the database of backup corruption.&lt;/p&gt;
&lt;h2&gt;
  
  
  Step-by-Step Guide to Recover SQL Database After Backup Corruption
&lt;/h2&gt;

&lt;p&gt;Before restoring the backup, we must validate whether the backup is valid or corrupt. For example, we want to restore the AdventureWorks2012 database. Before we begin the restoration process, verify the backup copy available with us is valid. We have full, differential and transaction log backups. To check that, execute following query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DECLARE @BackupFile_FullBackup NVARCHAR(255) = 'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_full_backup.bak';
DECLARE @BackupFile_DiffBackup NVARCHAR(255) = 'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_differential_backup.bak';
DECLARE @BackupFile_LogBackup NVARCHAR(255) = 'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_log_backup.bak';

-- Step 1: Check the backup header (metadata about backup set)
RESTORE HEADERONLY FROM DISK = @BackupFile_FullBackup;
RESTORE HEADERONLY FROM DISK = @BackupFile_DiffBackup;
RESTORE HEADERONLY FROM DISK = @BackupFile_LogBackup;

-- Step 2: Check the backup file list (files included in the backup)
RESTORE FILELISTONLY FROM DISK = @BackupFile_FullBackup;
RESTORE FILELISTONLY FROM DISK = @BackupFile_DiffBackup;
RESTORE FILELISTONLY FROM DISK = @BackupFile_LogBackup;

-- Step 3: Verify that the backup is not corrupted and can be restored
RESTORE VERIFYONLY FROM DISK = @BackupFile_FullBackup;
RESTORE VERIFYONLY FROM DISK = @BackupFile_DiffBackup;
RESTORE VERIFYONLY FROM DISK = @BackupFile_LogBackup;

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the backups are verified, we can start the restoration process. First, we must restore the full backup, then secondly, we must restore the differential backup and finally if the database is in full recovery model, we must restore all transaction log backups.&lt;/p&gt;

&lt;p&gt;Here is the command to restore the full backup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RESTORE DATABASE [Stackoverflow2010_Restore] FROM  DISK = N'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_full_backup.bak' WITH  FILE = 1,  MOVE N' Stackoverflow2010' TO N'D:\MS_SQL\DATA\Stackoverflow2010_Restore.mdf',  MOVE N' Stackoverflow2010_log' TO N'D:\MS_SQL\LOG\Stackoverflow2010_Restore_1.ldf',  NORECOVERY,  NOUNLOAD,  STATS = 5 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the command to restore the differential backup.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RESTORE DATABASE [Stackoverflow2010_Restore] FROM  DISK = N'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_differential_backup.bak' WITH  FILE = 1,  NORECOVERY,  NOUNLOAD,  STATS = 5
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the command to restore the transaction log file.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;RESTORE LOG [Stackoverflow2010_Restore] FROM  DISK = N'D:\MS_SQL\Backups\Stackoverflow2010\Stackoverflow2010_log_backup.trn' WITH RECOVERY  NOUNLOAD,  STATS = 10
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that the transaction log file will be restored WITH RECOVERY option.&lt;br&gt;
You can refer to &lt;a href="https://www.stellarinfo.com/blog/how-to-backup-and-restore-sql-server-database/" rel="noopener noreferrer"&gt;this &lt;/a&gt;article which explains the restoration process in detail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Addressing Common Errors During Recovery
&lt;/h2&gt;

&lt;p&gt;While restoring the database, we might encounter certain errors. Here are the common errors and their root cause and potential fix. Note that these errors are generic. For detailed description of errors, refer to the Microsoft documentation.&lt;br&gt;
&lt;strong&gt;Error: The backup set is corrupted.&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Reason:&lt;/strong&gt; &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This error occurs when one or multiple backup files are corrupted. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Potential fix and Precautions&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is very critical error, and which directly indicates that backup set which we are trying to restore is not usable. Now, all we can do is to find a good working backup and restore it. In most cases, you will encounter a major data loss.&lt;/li&gt;
&lt;li&gt;Always test your backups. Keep the backups on redundant storage. In simple words, keep the backup of backups.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Error: The log or differential backup is missing:&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Reason:&lt;/strong&gt; The error indicates that while restoring the sequence of backups, there is a differential or log backup is missing. The error indicates a recovery chain has been broken due to a full backup is taken between a full backup and differential backup. Read this article to learn more about it.&lt;br&gt;
&lt;strong&gt;Potential fix and precautions:&lt;/strong&gt;&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;This is very critical error and the only way to fix it to restore the last full backup and subsequent working differential and log backup.
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;If you want to take an ad hoc backup, always use the COPY_ONLY option. It will ensure that recovery chain does not get broken and restore completes without error.&lt;br&gt;
&lt;strong&gt;Error: Database is in use&lt;/strong&gt;&lt;br&gt;
&lt;strong&gt;Reason:&lt;/strong&gt; This error occurs when you are trying to overwrite the database with backup and the database which you are restoring is ONLINE and MULTIUSER state. &lt;br&gt;
*&lt;em&gt;Potential Fix: *&lt;/em&gt;&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;This is not a critical error. The issue can be resolved by changing the state of database from MULTI_USER to SINGLE_USER. Execute following command to do that.&lt;br&gt;
&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER DATABASE &amp;lt;Database_Name&amp;gt; SET SINGLE_USER WITH ROLLBACK IMMEDIATE
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once the database is in SINGLE_USER mode, you can rerun the database restoration process.&lt;/p&gt;

&lt;h2&gt;
  
  
  Leveraging Tools for Efficient SQL Database Recovery
&lt;/h2&gt;

&lt;p&gt;When the manual recovery method that I have explained fails, you can use the third-party tools which can help to recover the data. I personally use Stellar Repair for MS SQL which is professional solution that can help to repair and restore the corrupt database. Here are some key benefits &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;It can repair MDF and LDF database files.&lt;/li&gt;
&lt;li&gt;It can recover the data and the database objects like tables, procedures and triggers.&lt;/li&gt;
&lt;li&gt;It can also help to restore the data from the corrupt backups.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;Backup corruption is one of the most challenging obstacles in database recovery. To avoid data loss, we must always verify backup integrity and restorability. Always maintain a clear recovery plan, always test the existing backups, and always store backups in multiple secure locations. In cases where standard restore methods don’t work, &lt;a href="https://www.stellarinfo.com/sql-database-toolkit.php" rel="noopener noreferrer"&gt;Stellar Repair for MS SQL&lt;/a&gt; provides a powerful way to recover SQL database from corruption and ensure business continuity.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to Patch SQL Server Safely and Handle Unexpected Database Issues Post-Patching?</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Tue, 12 Aug 2025 08:49:37 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/how-to-patch-sql-server-safely-and-handle-unexpected-database-issues-post-patching-j5j</link>
      <guid>https://dev.to/nisargupadhyay87/how-to-patch-sql-server-safely-and-handle-unexpected-database-issues-post-patching-j5j</guid>
      <description>&lt;p&gt;Microsoft regularly releases security packs, cumulative, and other updates for MS SQL Server to add new features, fix performance issues, and bugs in the server. Before SQL Server 2016, the company used to release service packs and cumulative updates regularly. However, it changed its servicing model with SQL Server 2017. It now releases only cumulate updates (CUs) and General Distribution Releases (GDRs) every 2 months.&lt;/p&gt;

&lt;p&gt;Unpatched SQL servers are prone to SQL injection attacks, cyberattacks, compliance issues, and more. So, it is recommended to install service packs, cumulative packs, and general distribution releases as and when available. Installing such updates timely ensure a secure, stable, and high-performance SQL Server environment. In this article, we will discuss the stepwise process to patch the SQL Server safely. We will also discuss some common database issues that you may encounter after patching the server and explain how to handle them. &lt;/p&gt;

&lt;h2&gt;
  
  
  Stepwise Process to Patch the SQL Server Safely
&lt;/h2&gt;

&lt;p&gt;Before patching or installing the updates, consider the following:&lt;/p&gt;

&lt;p&gt;-Check the SQL Server version you are updating. To do this, you can run the below query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT @@VERSION version
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;ul&gt;
&lt;li&gt;Ensure that the version of SQL server that you are updating is compatible with the available service packs or cumulate updates. &lt;/li&gt;
&lt;li&gt;Back up your SQL databases before installing the updates. This will help you restore the databases in case something goes wrong. &lt;/li&gt;
&lt;li&gt;Running agent tasks can disrupt the patching process. So, temporarily &lt;a href="https://learn.microsoft.com/en-us/ssms/agent/disable-or-enable-a-job" rel="noopener noreferrer"&gt;disable the SQL agent tasks&lt;/a&gt; till the patching is complete. &lt;/li&gt;
&lt;li&gt;Stop the SQL Server (MSSQLSERVER) service. To do this, open the SQL Server configuration Manager, click SQL Server Services, right-click on SQL Server (MSSQLSERVER), and click Stop. &lt;/li&gt;
&lt;li&gt;&lt;p&gt;Always perform the process in a test environment first.&lt;br&gt;
You can now download and install the latest updates according to your SQL Server version. For this,&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Go to the &lt;a href="https://learn.microsoft.com/en-us/troubleshoot/sql/releases/download-and-install-latest-updates#latest-updates-available-for-currently-supported-versions-of-sql-server" rel="noopener noreferrer"&gt;Microsoft page&lt;/a&gt; to know the latest updates and version history for SQL Server.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;According to your SQL Server version, download the patches/updates. For example, download only the cumulative updates, if you’re using SQL Server 2022. Download both service packs and cumulative packs if you’re using SQL Server 2016 and earlier versions. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Read and accept the License terms. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Select the SQL Server instance you want to update. Confirm the installation path. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Check and confirm the installation options. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Start the installation process. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Wait till the installation process is complete. Then, restart the SQL Server (MSSQLSERVER) service. &lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Verify the update by executing the SELECT @&lt;a class="mentioned-user" href="https://dev.to/version"&gt;@version&lt;/a&gt; query.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Common Issues You can Face After Patching the MS SQL Server
&lt;/h2&gt;

&lt;p&gt;After updating the SQL Server, you may encounter various issues and errors, like error codes 3417 and 917. These errors are associated with database script update failure, corruption, and inconsistences in SQL database files.&lt;br&gt;
After installing the updates if the upgrade script fails, you can check the error log for the related error messages and then &lt;a href="https://learn.microsoft.com/en-us/troubleshoot/sql/database-engine/install/windows/troubleshoot-upgrade-script-failures-apply-update" rel="noopener noreferrer"&gt;troubleshoot the upgrade script failure&lt;/a&gt;. You can also use the trace flag 902 to start the MS SQL Server.&lt;br&gt;
If you see the &lt;a href="https://www.stellarinfo.com/blog/fix-sql-server-error-3417/" rel="noopener noreferrer"&gt;SQL Server error 3417&lt;/a&gt; after patching the SQL Server, this indicates that there is corruption in the database files. In such a case, you can restore the master database from backup or try rebuilding the master database file. To rebuild the master database, you can use the following CMD command:&lt;br&gt;
Note: Make sure to read the &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/databases/rebuild-system-databases?view=sql-server-ver15#Restrictions" rel="noopener noreferrer"&gt;limitations and prerequisites of rebuilding the SQL master database&lt;/a&gt;.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;setup /QUIET /ACTION=REBUILDDATABASE /INSTANCENAME=InstanceName /SQLSYSADMINACCOUNTS=accounts [ /SAPWD= StrongPassword ] [ /SQLCOLLATION=CollationName ]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If the errors have occurred due to corruption in MDF files, then you can &lt;a href="https://www.stellarinfo.com/blog/how-to-repair-sql-database-using-dbcc-checkdb-command/" rel="noopener noreferrer"&gt;run the DBCC CHECKDB command to repair the files&lt;/a&gt;. Here’s the syntax:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;DBCC CHECKDB    
    [ ( db_name | db_id | 0   
        [ , NOINDEX    
        | , { REPAIR_ALLOW_DATA_LOSS | REPAIR_FAST | REPAIR_REBUILD } ]   
    ) ]   
    [ WITH    
        {   
            [ ALL_ERRORMSGS ]   
            [ , EXTENDED_LOGICAL_CHECKS ]    
            [ , NO_INFOMSGS ]   
            [ , TABLOCK ]   
            [ , ESTIMATEONLY ]   
            [ , { PHYSICAL_ONLY | DATA_PURITY } ]   
            [ , MAXDOP  = number_of_processors ]   
        }   
    ]   
]

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Repairing the MDF files using the DBCC CHECKDB command may cause data loss. Alternatively, you can use a professional SQL repair tool, like &lt;a href="https://www.stellarinfo.com/sql-recovery.php" rel="noopener noreferrer"&gt;Stellar Repair for MS SQL&lt;/a&gt;, to prevent data loss and quickly repair the SQL database. Its advanced algorithms can repair the MDF/NDF files easily and recover all the objects without any data loss. It can help resolve database corruption errors that may occur after updating the server. The tool supports all versions of SQL Server, including MS SQL Server 2022, MS SQL Server 2019, and MS SQL Server 2017.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;You must always install the latest service packs or cumulative updates (when available) to protect the SQL Server from threats and malicious attacks. Above, we have discussed the stepwise procedure to patch the MS SQL Server. We have also mentioned some common issues that you may face after patching or installing the updates. However, if the database gets corrupted after patching the SQL Server, then you can restore the database from backup. If the backup isn't available, use Stellar Repair for MS SQL to repair the corrupted MDF/NDF files and recover all the data. &lt;/p&gt;

</description>
    </item>
    <item>
      <title>Data Types in SQL Server: In-depth Overview</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Fri, 02 Aug 2024 05:13:21 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/data-types-in-sql-server-in-depth-overview-236i</link>
      <guid>https://dev.to/nisargupadhyay87/data-types-in-sql-server-in-depth-overview-236i</guid>
      <description>&lt;p&gt;The data type in an SQL Server is an attribute that defines what type of data will be stored in a column of a table. When we create any table in a database, we must define columns with their respective data types. The SQL Server has various system data types that can store all data types, such as numbers, alphabets, special characters, etc.&lt;br&gt;
The data types supported by SQL Server, can be categorized in the following categories.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Exact numeric data types like int, bigint.&lt;/li&gt;
&lt;li&gt;Approximate numeric data types like real and float.&lt;/li&gt;
&lt;li&gt;Date and Time data types like datetime, datetime2.&lt;/li&gt;
&lt;li&gt;Character string data types like char, varchar.&lt;/li&gt;
&lt;li&gt;Unicode character strings like nChar and nVarchar.&lt;/li&gt;
&lt;li&gt;Binary string data types like varbinary.&lt;/li&gt;
&lt;li&gt;Special data types like XML, SQL_Variant, rowversion, etc.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Note that you must choose the data type carefully because choosing the wrong one might adversely impact the application's performance. Also, keep the data type's length adequate so the application can store data properly. For example, if you want to store an employee's address in the address column, the data type length must be adequate; otherwise, the user might encounter errors.&lt;br&gt;
This article will explain various data types, their range, usage, and simple examples. We will also see the data types that have been deprecated.&lt;/p&gt;
&lt;h2&gt;
  
  
  Exact Numeric Data Types
&lt;/h2&gt;

&lt;p&gt;Exact numeric data types are used to store precise values. As the name suggests, exact numeric data types do not lose accuracy due to rounding and approximation. These data types can be used to store financial data like the cost of a product and sales amount, statistical data like the population of a country, demographic data like human age, and many more. SQL Server supports five types of exact numeric data types.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcz27l5myo3dwwvg5qt3.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhcz27l5myo3dwwvg5qt3.png" alt="Image description" width="800" height="488"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of exact numeric data types in SQL Server&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple example which illustrates what type of data can be stored in exact numeric data type&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use BansalGroup_MainDB
go
CREATE TABLE ExactNumericDatatypes (
    ID INT PRIMARY KEY,
    BigIntColumn BIGINT,
    IntColumn INT,
    SmallIntColumn SMALLINT,
    TinyIntColumn TINYINT,
    DecimalColumn DECIMAL(10, 2),
    NumericColumn NUMERIC(8, 3)
);
INSERT INTO ExactNumericDatatypes (ID, BigIntColumn, IntColumn, SmallIntColumn, TinyIntColumn, DecimalColumn, NumericColumn)
VALUES (1, 123456789012345, 123456, 12345, 255, 123.45, 123.456);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Approximate Numeric Data Types
&lt;/h2&gt;

&lt;p&gt;The approximate numeric data types are used to store the data that has floating values. In SQL Server, you can store the approximate numeric datatypes in float and REAL data types. Here are the characteristics of float and REAL data type.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3ixnsanyt1xxkpt8u8c.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fa3ixnsanyt1xxkpt8u8c.png" alt="Image description" width="800" height="273"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of approximate numeric data types in SQL Server&lt;/strong&gt;&lt;br&gt;
Here is a simple example that illustrates what data type can be stored in approximate numeric data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE ApproximateNumericExample (

    FloatDataType FLOAT,
    RealDataType REAL
); 
INSERT INTO ApproximateNumericExample (FloatDataType, RealDataType)
VALUES (123.456, 123.456);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Date and Time Data Types
&lt;/h2&gt;

&lt;p&gt;The SQL Server has data types that are used to store the date and time. Here is a short description of each of them.&lt;br&gt;
&lt;u&gt;&lt;strong&gt;date&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
The data type is used to store only date. The data type has three part; month, year and day. The default format to store the value in data type is yyyy-MM-dd. The range of datatype is from 0001-01-01 to 9999-12-31.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;time&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
In some applications, we might have to store the time values separately. In such use cases, can use time data type. The data type is used to store only time. By default, the time datatype has seven fraction precision. The range of datatype is from 00:00:00.0000000 to 23:59:59.9999999.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;datetime&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The datetime data type is used to store the date and time. The data type can store three millisecond fractions. By default, the datetime data type format is yyyy-MM-dd HH:mm:ss:fff. The range of datatype is from 1753-01-01 00:00:00.000 to 9999-12-31 23:59:59.997.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;datetime2&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The datetime2 data type was introduced in SQL Server 2008. It is an extension of the datetime2 data type for better precision.&lt;br&gt;
Datetime2 has a larger date range, and the default precision is 7 digit which is higher than datetime data type. Also, you can specify the fraction precision. E.g. datetime(5).&lt;br&gt;
There are some limitations of datetime2 data type&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The basic mathematical operations with dates are not supported, like calculating the difference between two dates or adding days to the existing date value.&lt;/li&gt;
&lt;li&gt;When we compare the datetime2 value with specific date values, SQL Server performs an implicit conversion which may impact performance.
The range of datatype is from 0001-01-01 00:00:00.0000000 to 9999-12-31 23:59:59.9999999.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;u&gt;&lt;strong&gt;smalldatetime&lt;/strong&gt;&lt;/u&gt;&lt;br&gt;
As the name suggests, the smalldatetime data type is used to store date and time but the data type does not store seconds or a fraction of time. The range of datatype is from 0001-01-01 00:00 to 9999-12-31 23:59.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;datetimeoffset&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The datetimeoffset is an extension of datetime2 data type. The data type includes the time zone based on UTC / GMT. The range of datatype is from 0001-01-01 00:00:00.0000000 to 9999-12-31 23:59:59.9999999 with offset  (-14:00) to (14:00).&lt;/p&gt;

&lt;p&gt;You can refer to the following table to understand the difference between all datetime data types supported by SQL Server.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dsifclfsupiveea24cn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4dsifclfsupiveea24cn.png" alt="Image description" width="800" height="386"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of date and time data types in SQL Server:&lt;/strong&gt;&lt;br&gt;
Here is the simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE DateTimeDataTypes (

    DateTimeColumn DATETIME,
    SmallDateTimeColumn SMALLDATETIME,
    DateTime2Column DATETIME2,
    DateColumn DATE,
    TimeColumn TIME
);
INSERT INTO DateTimeDataTypes (DateTimeColumn, SmallDateTimeColumn, DateTime2Column, DateColumn, TimeColumn)
VALUES
(
'2024-03-18 12:30:00', -- Datetime
'2024-03-18 12:30:00', -- Small datetime
'2024-03-18 12:30:00.1234567', --Datetime2
'2024-03-18', -- Date
'12:30:00.1234567' --Time
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Character Strings
&lt;/h2&gt;

&lt;p&gt;The character string data types store the characters or strings in a database. In SQL Server, you can use char(n), varchar(n), varchar(max), and text data types to store the character strings. Here is a detailed description of all of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjzg2ob62h7ci2kufjdc.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjzg2ob62h7ci2kufjdc.png" alt="Image description" width="800" height="431"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Example of character string data types in SQL Server&lt;/strong&gt;&lt;br&gt;
Here is a simple example&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE StringDataTypeExample (

    FixedCharColumn CHAR(10),
    VarCharColumn VARCHAR(255),
    TextColumn TEXT

);
INSERT INTO StringDataTypeExample
( FixedCharColumn, VarCharColumn, TextColumn)
VALUES ('Nisarg ', 'Nisarg Upadhyay', 'Nisarg Upadhyay is a scary DBA');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Unicode Character Strings
&lt;/h2&gt;

&lt;p&gt;The Unicode character string data types are used to store the Unicode characters or strings in a database. In SQL Server, you can use nchar(n), nvarchar(n), and ntext data types to store the character strings. Here is the detailed description of all of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foysb4fq3fsqcehm7jpu6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Foysb4fq3fsqcehm7jpu6.png" alt="Image description" width="800" height="377"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is the simple example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE StringDataTypeExample (

    NCharColumn NCHAR(10),
    NVarCharColumn NVARCHAR(255),
    NTextColumn NTEXT

);
INSERT INTO StringDataTypeExample
( NCharColumn, NVarCharColumn, NTextColumn)
VALUES ('निसर्ग ', 'निसर्ग उपाध्याय', 'निसर्ग उपाध्याय एक डरावना डीबीए है।');-- Hindi language Text

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Note that, while inserting a Unicode string, you must specify N before the Unicode string. &lt;/p&gt;

&lt;h2&gt;
  
  
  Binary Strings
&lt;/h2&gt;

&lt;p&gt;In SQL Server the binary data types are used to store the binary string. In SQL Server, there are three data types: binary, varbinary, and varbinary(max). The details of all of them are the following:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1v6fmy2izn4kmmgo0de.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw1v6fmy2izn4kmmgo0de.png" alt="Image description" width="800" height="226"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here is a simple example. I am inserting “Nisarg Upadhyay” character string. To insert data, first we must convert the character string to varbinary. The code to create table and insert data is following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE BinaryDataTypeTable
(
    BinaryData VARBINARY(100)
);
INSERT INTO BinaryDataTypeTable (BinaryData)
VALUES (convert(varbinary,'Nisarg Upadhyay')); -- Convert to varbinary

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To view data, run the SELECT query on BinaryDataTypeTable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select BinaryData [Binary string], convert(varchar,BinaryData)[Original String] from BinaryDataTypeTable
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fczxxsnuzrx3fj27kg8eb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fczxxsnuzrx3fj27kg8eb.png" alt="Image description" width="800" height="160"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the character string is converted into a a binary string.&lt;/p&gt;

&lt;h2&gt;
  
  
  Special and Miscellaneous Data Types
&lt;/h2&gt;

&lt;p&gt;SQL Server also supports some special data types that are supported by SQL Server to handle specific data.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;XML data type&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The XML data type is used to store XML data. It can store XML documents up to 2GB in size. SQL Server has many XML functions that can be used to read, write, or update XML data. Here is a simple example that shows how to store data in XML data type.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE XMLExample (
    ID INT PRIMARY KEY,
    XMLColumn XML
);
INSERT INTO XMLExample (ID, XMLColumn)
VALUES (1, '&amp;lt;bookstore&amp;gt;
  &amp;lt;book category="Database"&amp;gt;
    &amp;lt;title lang="en"&amp;gt;How to tune database&amp;lt;/title&amp;gt;
    &amp;lt;author&amp;gt;Nisarg Upadhyay&amp;lt;/author&amp;gt;
    &amp;lt;year&amp;gt;2005&amp;lt;/year&amp;gt;
    &amp;lt;price&amp;gt;30.00&amp;lt;/price&amp;gt;
  &amp;lt;/book&amp;gt;
  &amp;lt;book category="IT Programming"&amp;gt;
    &amp;lt;title lang="en"&amp;gt;.Net Pro&amp;lt;/title&amp;gt;
    &amp;lt;author&amp;gt;Pritesh Patel&amp;lt;/author&amp;gt;
    &amp;lt;year&amp;gt;2005&amp;lt;/year&amp;gt;
    &amp;lt;price&amp;gt;29.99&amp;lt;/price&amp;gt;
  &amp;lt;/book&amp;gt;
&amp;lt;/bookstore&amp;gt;');

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Geospatial and Spatial data types&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
SQL Server supports data types to store geospatial data like GEOMETRY and GEOGRAPHY.&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Geometry&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The GEOMETRY datatype represent data in flat (Euclidean) coordinates system. Here is a simple example. I have created a table named GeometryDataTypeExample which is used to store the geometry coordinates of the city. Following is the code to create table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE GeometryDataTypeExample (
    CityID INT PRIMARY KEY,
    CityName NVARCHAR(100),
    Coordinates GEOMETRY
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following is the code to insert geometry data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO GeometryDataTypeExample (CityID, CityName, Coordinates)
VALUES
    (1, 'New York City', geometry::STGeomFromText('POINT(-74.0059 40.7128)', 4326)),
    (2, 'Los Angeles', geometry::STGeomFromText('POINT(-118.2437 34.0522)', 4326)),
    (3, 'London', geometry::STGeomFromText('POINT(-0.1276 51.5074)', 4326));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;&lt;u&gt;Geography&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
GEOGRAPHY datatype represents data in round-earth (ellipsoidal) co-ordination system like latitude and longitude of GPS system. The geography system is CLR data type in SQL Server. Here is a simple example. I have created a table named geographydatatypeexample which is used to store the geographical data of the city. Here is the code to create a table:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE GeographyDataTypeExample (
    CityID INT PRIMARY KEY,
    CityName NVARCHAR(100),
    GeoCoordinates GEOGRAPHY
);

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Following is the code to Insert data in table.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;INSERT INTO GeographyDataTypeExample (CityID, CityName, GeoCoordinates)
VALUES
    (1, 'New York City', geography::STGeomFromText('POINT(-74.0059 40.7128)', 4326)),
    (2, 'Los Angeles', geography::STGeomFromText('POINT(-118.2437 34.0522)', 4326)),
    (3, 'London',  geography::STGeomFromText('POINT(-0.1276 51.5074)', 4326));

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can read &lt;a href="https://learn.microsoft.com/en-us/sql/relational-databases/spatial/spatial-data-sql-server?view=sql-server-ver16" rel="noopener noreferrer"&gt;this &lt;/a&gt;article to learn more about the GEOMETRY and GEOGRAPHY datatypes.&lt;br&gt;
&lt;strong&gt;Rowversion (Timestamp)&lt;/strong&gt;&lt;br&gt;
The rowversion is a special datatype which is like a timestamp, but it is used internally to manage the concurrency of database. When any record is changed or updated, the SQL Server automatically generates a unique binary number which is used to track or detect a change in record.&lt;br&gt;
&lt;strong&gt;Hierarchyid&lt;/strong&gt;&lt;br&gt;
The hierarchy datatype is a special datatype used to store and query the hierarchical data in SQL Server. The datatype is optimized for representing the tree-like structure like employee hierarchy in company or a family tree. You can read this article to learn more about the hierarchy datatype. &lt;br&gt;
&lt;strong&gt;Rowversion (Timestamp)&lt;/strong&gt;&lt;br&gt;
The rowversion is a special data type that is like a timestamp, but it is used internally to manage the concurrency of database. When any record is changed or updated, the SQL Server automatically generates a unique binary number which is used to track or detect a change in record.&lt;br&gt;
&lt;strong&gt;SQL_VARIANT&lt;/strong&gt;&lt;br&gt;
The SQL_VARIANT data type can store values of various SQL Server data types. This data type is useful when you want to store different types of data in a column. For example, you want to store int and binary values in the same column. Such requirements can be fulfilled by SQL_Variant data type.  The maximum length of the SQL_Variant data type is 8016 bytes. You can read &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/data-types/sql-variant-transact-sql?view=sql-server-ver16" rel="noopener noreferrer"&gt;this &lt;/a&gt;article to learn more about the SQL_Variant data type. Here is a small example.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE SQLVariantDataType(SQLVariantColumn sql_variant) 

INSERT INTO SQLVariantDataType values ( CAST(46279.1 as decimal(8,2)))
INSERT INTO SQLVariantDataType values(CAST('Nisarg Upadhyay' as varchar(5)))

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;As you can see in above example, The table SQLVariantDataType has a column named SQLVariantColumn with SQL_Variant data type. Now, I inserted two records in a table. The first record is decimal and second record is character string. Now, execute select statement to view the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT  
             SQLVariantColumn,
             SQL_VARIANT_PROPERTY(SQLVariantColumn,'BaseType') AS 'Base Type', 
        SQL_VARIANT_PROPERTY(SQLVariantColumn,'Precision') AS 'Precision', 
        SQL_VARIANT_PROPERTY(SQLVariantColumn,'Scale') AS 'Scale' 

FROM      SQLVariantDataType 

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Output&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyiltre0rb9pbwg5y3q1n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyiltre0rb9pbwg5y3q1n.png" alt="Image description" width="800" height="165"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the column has decimal and varchar data stored in same column. &lt;/p&gt;

&lt;h2&gt;
  
  
  Deprecated Data Types
&lt;/h2&gt;

&lt;p&gt;After every release of SQL Server, some old features, syntax and data types get deprecated. So far, SQL Server has deprecated three data types. The data types can be used but it won’t be supported in future versions of SQL Server. These data types will be replaced by other data types. Here is a list.  &lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Timestamp is replaced by rowversion.&lt;/li&gt;
&lt;li&gt;Text is replaced by Varchar(max).&lt;/li&gt;
&lt;li&gt;NText is replaced by nvarchar(max).&lt;/li&gt;
&lt;li&gt;Image is replaced by varbinary(max).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;While designing a new application, avoid using the above data types. You should start changing the code with the new data type if you already use it in your application.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In any database management system, the data type selection is very important. When we design any database, we must select data types carefully to get optimum performance of the database and application. &lt;/p&gt;

&lt;p&gt;In this article, I have explained about data types, their categories, and how to use them to create tables. I have used SQL Server Management Studio and &lt;a href="https://www.devart.com/dbforge/sql/studio/?utm_source=dev.to&amp;amp;utm_medium=referral&amp;amp;utm_campaign=article"&gt;dbForge Studio for SQL Server&lt;/a&gt; to write the script that is used to create table.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>How to effectively use TRUNCATE TABLE in SQL Server</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Mon, 08 Apr 2024 16:55:03 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/how-to-effectively-use-truncate-table-in-sql-server-4ba5</link>
      <guid>https://dev.to/nisargupadhyay87/how-to-effectively-use-truncate-table-in-sql-server-4ba5</guid>
      <description>&lt;p&gt;Data manipulation in an SQL Server or any other database system includes various operations to manipulate existing data or add new data to a table. These operations contain various DML statements like INSERT, UPDATE, and DELETE. The INSERT statements are used to add new data to a table, the UPDATE statements are used to update the existing data in a table, and the DELETE statements are used to remove data from the table.&lt;br&gt;
Along with the above statements, there are a couple of other statements, like TRUNCATE TABLE, that can be used to manipulate the data.&lt;br&gt;&lt;br&gt;
The TRUNCATE TABLE statement removes all records from any table or specific partition. The purpose of DELETE and TRUNCATE is the same, but both work differently. The DELETE statement is used to remove all records or specific records from the table, but the truncate table is used to remove everything from a table. I have explained the difference between both statements in a separate section of this article.&lt;br&gt;
This article covers the basics of TRUNCATE TABLE with syntax and practical examples.  Note that the article is based on the Microsoft SQL Server database. The syntax might be different for other database platforms.&lt;/p&gt;

&lt;h2&gt;
  
  
  Understanding TRUNCATE TABLE syntax
&lt;/h2&gt;

&lt;p&gt;The syntax to truncate a table is following:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;TRUNCATE TABLE  [schema_name.table_name]  WITH ( PARTITIONS ( partition_number))&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;In the syntax,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;schema_name: Specify the schema name in which the table exists.&lt;/li&gt;
&lt;li&gt;table_name: Specify the table name that you want to truncate.&lt;/li&gt;
&lt;li&gt;partition_number: Specify the partition number from which you want to truncate the data.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let us understand the syntax with various examples. &lt;/p&gt;

&lt;h2&gt;
  
  
  Step-by-Step guide to using TRUNCATE TABLE
&lt;/h2&gt;

&lt;p&gt;But before that, we must create a same database that we can use to test our scenarios. I have created a database named BansalGroup_MainDB. The database is used to store the details of the employees and client of a company named Bansal Group. The database has three tables named BansalGroupClients, BansalGroupEmployees and BansalGroupProjects. The BansalGroupClients table holds the data of the clients, BansalGroupEmployees table holds the data of employees and BansalGroupProjects table holds the data of the projects that are being handled by the company.&lt;br&gt;
The BansalGroupProjects table has a foreign key on client_id column which references the BansalGroupClients table.&lt;br&gt;
Here is the T-SQL code to create the tables.  &lt;/p&gt;

&lt;p&gt;&lt;code&gt;CREATE TABLE BansalGroupProjects (&lt;br&gt;
    project_id INT PRIMARY KEY,&lt;br&gt;
    project_name VARCHAR(100),&lt;br&gt;
    client_id INT,&lt;br&gt;
    start_date DATE,&lt;br&gt;
    completion_date DATE,&lt;br&gt;
    budget DECIMAL(10, 2),&lt;br&gt;
    FOREIGN KEY (client_id) REFERENCES Clients(client_id)&lt;br&gt;
);&lt;br&gt;
CREATE TABLE [HR].BansalGroupEmployees (&lt;br&gt;
    employee_id INT PRIMARY KEY,&lt;br&gt;
    employee_name VARCHAR(100),&lt;br&gt;
    job_title VARCHAR(100),&lt;br&gt;
    email VARCHAR(100),&lt;br&gt;
    phone VARCHAR(20)&lt;br&gt;
);&lt;br&gt;
CREATE TABLE BansalGroupProjects (&lt;br&gt;
    project_id INT PRIMARY KEY,&lt;br&gt;
    project_name VARCHAR(100),&lt;br&gt;
    client_id INT,&lt;br&gt;
    start_date DATE,&lt;br&gt;
    completion_date DATE,&lt;br&gt;
    budget DECIMAL(10, 2),&lt;br&gt;
    FOREIGN KEY (client_id) REFERENCES Clients(client_id)&lt;br&gt;
);&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Once tables are created, run following queries to insert dummy data in all tables.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;INSERT INTO BansalGroupClients (client_id, client_name, contact_person, contact_email, contact_phone) VALUES&lt;br&gt;
(1, 'ABC Corporation', 'John Smith', 'john@example.com', '123-456-7890'),&lt;br&gt;
(2, 'XYZ Corp', 'Jane Doe', 'jane@example.com', '987-654-3210'),&lt;br&gt;
(3, 'LMN Ltd', 'Sam Brown', 'sam@example.com', '555-555-5555');&lt;br&gt;
INSERT INTO [HR].BansalGroupEmployees (employee_id, employee_name, job_title, email, phone) VALUES&lt;br&gt;
(1, 'Alice Johnson', 'Architect', 'alice@example.com', '111-222-3333'),&lt;br&gt;
(2, 'Bob Williams', 'Project Manager', 'bob@example.com', '444-555-6666'),&lt;br&gt;
(3, 'Charlie Brown', 'Draftsman', 'charlie@example.com', '777-888-9999');&lt;br&gt;
INSERT INTO BansalGroupProjects (project_id, project_name, client_id, start_date, completion_date, budget) VALUES&lt;br&gt;
(101, 'Office Building', 1, '2023-01-01', '2023-06-30', 500000.00),&lt;br&gt;
(102, 'Residential House', 2, '2023-02-15', '2023-09-30', 700000.00),&lt;br&gt;
(103, 'Shopping Complex', 3, '2023-03-20', '2024-01-31', 1200000.00);&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Now, we are ready to test the various use cases but before that, let us understand the difference between the TRUNCATE Table and other data deletion methods.&lt;/p&gt;

&lt;h2&gt;
  
  
  TRUNCATE TABLE vs. other SQL Data Deletion methods
&lt;/h2&gt;

&lt;p&gt;The TRUNCATE TABLE and DELETE statements are used to remove data from the table. But both methods work differently and there are several differences between them. Note that, this is one of the popular interview questions. The following are the differences: &lt;/p&gt;

&lt;h3&gt;
  
  
  Drop table statement:
&lt;/h3&gt;

&lt;p&gt;•DROP TABLE statement deletes a table from database.&lt;br&gt;
•If the table is not locked by any transaction, the drop table statement executes immediately.&lt;br&gt;
•DROP TABLE statement is a DDL statement and they are auto-committed so if you drop any table, it cannot be rolled back.&lt;/p&gt;

&lt;h3&gt;
  
  
  Truncate Table Statement
&lt;/h3&gt;

&lt;p&gt;•The Truncate table statement deletes all records from a table.&lt;br&gt;
•The TRUNCATE TABLE statement works faster because when we execute TRUNCATE TABLE statement, the operation is not logged in transaction logs which reduced the additional IO which occurs while writing the transaction on log files.&lt;br&gt;
•The TRUNCATE TABLE statement resets the identity.&lt;br&gt;
•The TRUNCATE TABLE cannot be rolled back, hence before executing the statement, make sure you have required backups.&lt;br&gt;
•The TRUNCATE TABLE do not fire a trigger created on a table.&lt;/p&gt;

&lt;h3&gt;
  
  
  DELETE Statement
&lt;/h3&gt;

&lt;p&gt;•The DELETE statement deletes all records or specific records. You can specify the condition in WHERE clause. So, the records that are matches with the condition will be deleted.&lt;br&gt;
•The DELETE statement is slower, specially when you are removing a lot of records from a table because the DELETE is a logged operations so the changes will be written to transaction log file which adds additional IO.&lt;br&gt;
•The DELETE statement does not reset the identity.&lt;br&gt;
•If DELETE statement is executed in a transaction, and if the changes are not committed, the transaction can be rolled back.&lt;br&gt;
•The DELETE statement executes a trigger created on a table from which you are deleting the data.&lt;/p&gt;

&lt;p&gt;Now, let us take a look at some additional recommendation and limitation of using TRUNCATE TABLE.&lt;/p&gt;

&lt;h2&gt;
  
  
  Special considerations and limitations of TRUNCATE TABLE
&lt;/h2&gt;

&lt;p&gt;The Truncate table statement cannot be executed on:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;A table which being referenced by a foreign key. I have explained that in above section.&lt;/li&gt;
&lt;li&gt;A table which being used in indexed view.&lt;/li&gt;
&lt;li&gt;A table which is being used in merge or transactional replication.&lt;/li&gt;
&lt;li&gt;A system-versioned temporal tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Before executing a TRUNCATE TABLE statement on any table, you must understand following implications.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;As I mentioned in earlier sections of the article, the TRUNCATE TABLE is a minimally logged operation. Hence the changes made in data are not logged in transaction log file. If you truncate a table or partition, the operation could not be rolled back. You must restore entire database.&lt;/li&gt;
&lt;li&gt;If the truncate table statement is in a transaction, and you try to rollback the transaction, the DML statements will be rolled back automatically but the truncate table won’t be rolled back.&lt;/li&gt;
&lt;li&gt;Always test the procedure or the application code that has TRUNCATE TABLE statement on development environment before executing them on production environment.&lt;/li&gt;
&lt;li&gt;Make sure you have well-defined and well-tested backups in your database system. Backups must be validated and tested on regular intervals.
&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Practical examples in SQL Server Database
&lt;/h2&gt;

&lt;p&gt;Now, let us take a look at some examples. I have created BansalGroup_Maindb on the demo environment. If you are working in a production environment, to keep your data safe, make a backup of the database from which you want to truncate data.&lt;/p&gt;

&lt;h4&gt;
  
  
  Example 1: Simple Truncate table statement
&lt;/h4&gt;

&lt;p&gt;First, let us truncate BansalGroupEmployees table. To do that, execute following T-SQL query.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
truncate table [BansalGroupEmployees]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Once query executed, run SELECT query to view data.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
select * from  [BansalGroupEmployees]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnwelk7u7xhi3b349tu8.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Frnwelk7u7xhi3b349tu8.JPG" alt="Image description" width="604" height="191"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, the data has been deleted from the BansalGroupEmployees table. In next example, we will truncate table of specific schema.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 2: Truncate table from specific schema
&lt;/h3&gt;

&lt;p&gt;In this example, we will truncate BansalGroupClients table. The table is created in Clients schema, so the command to truncate the BansalGroupClients table would be:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
truncate table [clients].[BansalGroupClients]&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Once query executed, run SELECT query to view data.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
select * from [clients].[BansalGroupClients]&lt;/code&gt;&lt;br&gt;
&lt;strong&gt;Query screenshot&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8112v2m0yqnk61msdntm.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8112v2m0yqnk61msdntm.JPG" alt="Image description" width="530" height="243"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, the data has been deleted from the BansalGroupClients table. In next example, we will truncate table that is referenced by foreign key.&lt;/p&gt;

&lt;h3&gt;
  
  
  Example 3: Truncate table that is referenced by foreign key
&lt;/h3&gt;

&lt;p&gt;This example shows how to run truncate table statement on a table that is referenced by foreign key. In this example, we will truncate table named BansalGroupClients table. The client_id column of the table is being referenced by client_id column of BansalGroupProjects table. Let us execute truncate table statement.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
truncate table [clients].[BansalGroupClients]&lt;/code&gt;&lt;br&gt;
Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6gf7akipvrtq9buk3ekh.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F6gf7akipvrtq9buk3ekh.JPG" alt="Image description" width="800" height="91"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the truncate table statement is failed because it is being referenced by another table. To rectify this issue, first we must drop the foreign key on BansalGroupProjects table. Execute following query to drop the foreign key constraint.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;alter table [dbo].[BansalGroupProjects] drop constraint [FK__BansalGro__clien__4CA06362]&lt;/code&gt;&lt;br&gt;
Once constraint is dropped, re-run the TRUNCATE TABLE statement.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
truncate table [clients].[BansalGroupClients]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;The query executed successfully. To verify, run SELECT statement on BansalGroupClients table.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
select * from  [clients].[BansalGroupClients]&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8lcvdx5s86u7lpc70wd.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg8lcvdx5s86u7lpc70wd.JPG" alt="Image description" width="530" height="243"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the table has been truncated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Partitions and permissions
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Working with partitions
&lt;/h3&gt;

&lt;p&gt;The TRUNCATE TABLE statement can be used to truncate data from a specific partition. I explained the syntax in an earlier section. For demonstration, I created three partitions on the BansalGroupProjects table. You can read the article to learn more about SQL Server table partitioning. &lt;br&gt;
You can view the details of partitions created on a table by running the following query:&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT &lt;br&gt;
    [tables].name AS [Table Name],&lt;br&gt;
    [partitions].partition_number AS [Partition Number],&lt;br&gt;
    [Filegroups].name AS [FileGroup Name],&lt;br&gt;
    [partitions].rows AS [Records in partition]&lt;br&gt;
FROM &lt;br&gt;
    sys.tables AS [tables]&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.indexes AS [indexes] ON [tables].object_id = [indexes].object_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.partitions AS [partitions] ON indexes.object_id = [partitions].object_id AND indexes.index_id = [partitions].index_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.allocation_units AS [allocationUnits] ON [partitions].partition_id = [allocationUnits].container_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.filegroups AS [Filegroups] ON [allocationUnits].data_space_id = [Filegroups].data_space_id&lt;br&gt;
WHERE &lt;br&gt;
    [tables].name = 'BansalGroupProjects'&lt;br&gt;
ORDER BY &lt;br&gt;
    [tables].name, [indexes].name, [partitions].partition_number;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5swin0dxf0ifkeh2icf.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fe5swin0dxf0ifkeh2icf.JPG" alt="Image description" width="587" height="128"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, there are four partitions in a table. Let us truncate the data from partition number 3. To do that, execute the following SQL query.&lt;br&gt;
&lt;code&gt;use BansalGroup_MainDB&lt;br&gt;
go&lt;br&gt;
TRUNCATE TABLE BansalGroupProjects   WITH (PARTITIONS (3));&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Once truncate table completes successfully, execute below query to verify the changes.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;SELECT &lt;br&gt;
    [tables].name AS [Table Name],&lt;br&gt;
    [partitions].partition_number AS [Partition Number],&lt;br&gt;
    [Filegroups].name AS [FileGroup Name],&lt;br&gt;
    [partitions].rows AS [Records in partition]&lt;br&gt;
FROM &lt;br&gt;
    sys.tables AS [tables]&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.indexes AS [indexes] ON [tables].object_id = [indexes].object_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.partitions AS [partitions] ON indexes.object_id = [partitions].object_id AND indexes.index_id = [partitions].index_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.allocation_units AS [allocationUnits] ON [partitions].partition_id = [allocationUnits].container_id&lt;br&gt;
INNER JOIN &lt;br&gt;
    sys.filegroups AS [Filegroups] ON [allocationUnits].data_space_id = [Filegroups].data_space_id&lt;br&gt;
WHERE &lt;br&gt;
    [tables].name = 'BansalGroupProjects'&lt;br&gt;
ORDER BY &lt;br&gt;
    [tables].name, [indexes].name, [partitions].partition_number;&lt;br&gt;
&lt;/code&gt;&lt;br&gt;
Query Output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1x6ngktgy4x6eqct7lx.JPG" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fz1x6ngktgy4x6eqct7lx.JPG" alt="Image description" width="521" height="103"&gt;&lt;/a&gt;&lt;br&gt;
The highlighted record in the above image shows that the partition's data has been truncated.&lt;/p&gt;

&lt;h3&gt;
  
  
  Permissions
&lt;/h3&gt;

&lt;p&gt;You must assign an ALTER permission on the table to the user or sql login. If the user or sql login is already a member of the sysadmin, db_owner or db_ddladmin fixed role, it will automatically get the TRUNCATE TABLE permission on all tables created in a database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;This article taught us about the TRUNCATE TABLE statement in SQL Server and how to use it effectively in SQL Server. The TRUNCATE TABLE can be run using SQL Server Management Studio, SQLCMD utility, or &lt;a href="https://www.devart.com/dbforge/sql/studio/studio-sql.html?utm_source=dev.to&amp;amp;utm_medium=referral&amp;amp;utm_campaign=article_How_to_effectively_use_TRUNCATE_TABLE_in_SQL_Server"&gt;[dbForge Studio for SQL Server]&lt;/a&gt; tools.&lt;/p&gt;

&lt;p&gt;I have explained the data manipulation operations in any database system. We understand the syntax of the TRUNCATE TABLE statement and various examples. I have covered the difference between a TRUNCATE TABLE query and a DELETE query, which is the most common interview question. Also, we understand when we should use the truncate table and what needs to be taken care of before using it.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Creating a view in SQL Server</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Thu, 08 Feb 2024 15:21:01 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/creating-a-view-in-sql-server-3689</link>
      <guid>https://dev.to/nisargupadhyay87/creating-a-view-in-sql-server-3689</guid>
      <description>&lt;p&gt;In this article, we are going to learn about SQL Server views. I am covering the following topics in the article&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basics of view and its usage.&lt;/li&gt;
&lt;li&gt;How to create, update, and drop the view using T-SQL query.&lt;/li&gt;
&lt;li&gt;How to create the view using SQL Server Management Studio.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For a better explanation, I have created a sample database that contains the data of a hospital. The database contains four tables named [Appointments],[Patients],[MedicalHistory], and [Doctors]&lt;br&gt;
Here is the code to create the tables.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use HospitalManagementSystem
go
CREATE TABLE  Appointments (
    AppointmentID   int  NOT NULL,
    PatientID   int  NULL,
    DoctorID   int  NULL,
    AppointmentDate   datetime  NULL,
    Notes   varchar (max) NULL
)
CREATE TABLE Patients (
     PatientID   int  NOT NULL,
     FirstName   varchar (50) NULL,
     LastName   varchar (50) NULL,
     DateOfBirth   date  NULL,
     Gender   char (1) NULL,
     PhoneNumber   varchar (15) NULL,
     Email   varchar (100) NULL,
     Address   varchar (255) NULL
)
GO
CREATE TABLE MedicalHistory (
     MedicalHistoryID   int  NOT NULL,
     PatientID   int  NULL,
     Diagnosis   varchar (255) NULL,
     Prescription   varchar (max) NULL,
     DateRecorded   datetime  NULL
)
GO
CREATE TABLE Doctors (
     DoctorID   int  NOT NULL,
     FirstName   varchar (50) NULL,
     LastName   varchar (50) NULL,
     Specialization   varchar (50) NULL,
     PhoneNumber   varchar (15) NULL,
     Email   varchar (100) NULL,
     Address   varchar (255) NULL)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I have inserted dummy records in the tables using dbForge Studio for SQL Server which has a feature called Data generator. It helps to create a real-time data set that helps to run various test cases for the application.&lt;br&gt;
Now, let us understand the basics of SQL Server views.&lt;/p&gt;
&lt;h2&gt;
  
  
  Understanding the Basics of Views
&lt;/h2&gt;

&lt;p&gt;A view is a virtual table that contains the output of the SELECT query. The views are stored as a database object and like a table, it contains the columns. Note that the view contains only those columns that are returned by a query which is used to create a view. For example, if the query which is used to create a view, returns five columns then a view can contain only those five columns.&lt;/p&gt;

&lt;p&gt;Unlike tables, the views do not store actual data in a database. Whenever we execute SELECT statement on view, it executes the T-SQL query which is used to create the view and returns the data from underlying tables.&lt;/p&gt;

&lt;p&gt;The SELECT query that is used to define a view can have various string and numeric functions. Also, the query has data from one or more than one table, which can exist on the same database or different databases.&lt;/p&gt;

&lt;p&gt;The views can be considered as an added security layer because when a user executes a query against a view, it retrieves the specific subset of the data. Moreover, the underlying structure of a table remains hidden. For example, if your database contains the details of the credit card of a customer and you do not want anyone to access that column, you can create a view on top of the base table and grant permissions to access that view only.&lt;br&gt;
There are mainly five types of views.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt; Simple views: The simple views contain a simple SELECT statement which populates data from one table. As simple view references to one table, we can perform INSERT, UPDATE , and DELETE operations.&lt;/li&gt;
&lt;li&gt;Complex Views: The complex views contain a SELECT statement that joins multiple tables and may contain aggregate functions, string manipulation functions and other complex business logic. The complex views cannot be updated.
&lt;/li&gt;
&lt;li&gt;Updatable Views: The updatable views allows us to perform INSERT, UPDATE , and DELETE operations. When we perform any DML operation on UPDATABLE view, the chages will be applied on the underlying tables. Note that, the updatable view cannot be complex, meaning it cannot contain an aggregate function or group by clause. It must not contain a DISTINCT keyword. The view must reference one table and if the view references multiple tables, then all tables must be joined using INNER JOIN only.&lt;/li&gt;
&lt;li&gt;Indexed views: The indexed views are materialized views. As the name suggests, we can add a unique index on it. The index views have performance benefits when we are using a query that has aggregate functions on various columns. However, if the data of the underlying tables of the materialized views is frequently updated, it is not a good idea to use an indexed view. You can read Create indexed views article to learn more about the indexed views. &lt;/li&gt;
&lt;li&gt;Partitioned views: The partitioned views are like SQL Server table partitions but unlike table partitions, we do not need to create various partition functions or schema. However, the partition views are not as flexible as table partitions but if configured properly, they give good results in terms of performance, data manageability, and archival process. You can read SQL Server Partitioned Views article to learn more about partitioned views. &lt;/li&gt;
&lt;li&gt;System Views: The system views are used to show the SQL Server metadata. When we query a system catalog view, it provides the details of the metadata information of the SQL Server database and its configurations. The simple example is sys.tables view which shows the details of the tables created in a database. You can read System Views article to learn more about the system catalog views.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Now, let us understand how to create and manage SQL Server views.    &lt;/p&gt;
&lt;h2&gt;
  
  
  Creating a View in SQL Server
&lt;/h2&gt;

&lt;p&gt;The syntax to create a view is following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create view [view_name]
as
sql_query..
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the syntax,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;view_name: Specify the name of the view.&lt;/li&gt;
&lt;li&gt;sql_query: Specify the SELECT query to fetch the data from one or multiple tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, I want to create a view that populates the details of the patients admitted to a hospital. The details of registered patients are stored on a [Patient] table. Hence, we are going to execute the SELECT statement on the [Patient] table.&lt;br&gt;
Here is the query to create a view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create view vw_patientDetails
as
select patientID, firstname, lastname, dateofbirth,gender, phonenumber,email,address from [Patients]
where  (FirstName is not null and lastname is not null)
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When you execute the above query, SQL Server will create a view named [vw_patientDetails]. Now, to fetch the data from the view, you can execute a select statement against the view.&lt;br&gt;
Here is the query to view the data of a patient from view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Select * from vw_patientDetails
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here is the output.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwya76vjzhkjxuckzuw0t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwya76vjzhkjxuckzuw0t.jpg" alt="Image description" width="800" height="426"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As I mentioned in an earlier section, you can query a view just like the way you run a query on a SQL Server physical table. You can apply aggregate and string manipulation and other SQL Server built-in functions. Now, let us understand how we can use a query with joins to create a view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a View with Multiple Tables
&lt;/h2&gt;

&lt;p&gt;This section explains how we can use a query that joins multiple tables in a view. The syntax is same as creating view&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create view [view_name]
as
sql_query..

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the syntax,&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;view_name: Specify the name of the view.&lt;/li&gt;
&lt;li&gt;sql_query: Specify the SELECT query with Joins to fetch the data from multiple tables.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, we want to create a view that shows the list of patients and the name of the doctor who is treating them. For that, we are joining [Patients] and [MedicalHistory] tables.&lt;br&gt;&lt;br&gt;
The join query is the following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE HospitalManagementSystem
go
SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,mh.Diagnosis,mh.Prescription from [Patients] p INNER join
[MedicalHistory] mh ON p.PatientID=mh.PatientID
WHERE
  (FirstName is not null and lastname is not null)
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to create a view named vw_patients_dignosis, execute the following query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create view vw_patients_dignosis
as
SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,mh.Diagnosis,mh.Prescription from [Patients] p INNER join
[MedicalHistory] mh ON p.PatientID=mh.PatientID
WHERE
  (FirstName is not null and lastname is not null)
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Once a view is created, run the following query to get the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE HospitalManagementSystem
go
select * from vw_patients_dignosis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wso7853p1jsm7vcsdwn.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8wso7853p1jsm7vcsdwn.jpg" alt="Image description" width="800" height="531"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us understand how to update a view.&lt;/p&gt;

&lt;h2&gt;
  
  
  Updating a View in SQL Server
&lt;/h2&gt;

&lt;p&gt;If you want to modify the SELECT statement in a view meaning altering the view, we can use ALTER VIEW statement. The syntax is following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER VIEW [your_view_name]
as
updated_SQL_Query
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the syntax&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;your_view_name: Specify the name of the view that you want to modify.&lt;/li&gt;
&lt;li&gt;updated_SQL_Query: Specify the modified T-SQL query.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;For example, we want to alter the view named vw_patients_dignosis. The view shows the list of patients whose diagnosis is null. To do that, we will add another condition in the WHERE clause of the query:&lt;br&gt;
Here is the updated SQL query.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,mh.Diagnosis,mh.Prescription from [Patients] p INNER join  
[MedicalHistory] mh ON p.PatientID=mh.PatientID 
WHERE  
  (FirstName is not null and lastname is not null) 
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'') 
and (Diagnosis is not null and Diagnosis&amp;lt;&amp;gt;'')
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Execute the code below to ALTER the view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;ALTER view vw_patients_dignosis 
as 
SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,mh.Diagnosis,mh.Prescription from [Patients] p INNER join  
[MedicalHistory] mh ON p.PatientID=mh.PatientID 
WHERE  
  (FirstName is not null and lastname is not null) 
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'') 
and (Diagnosis is not null and Diagnosis&amp;lt;&amp;gt;'')

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Verify the changes by running the SELECT statement against the vw_patients_dignosis view.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use HospitalManagementSystem
go
select * from vw_patients_dignosis
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fke5chkamouqahm6unuad.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fke5chkamouqahm6unuad.jpg" alt="Image description" width="800" height="372"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see the query has returned a list of patients whose diagnosis is not null which shows that the view has been updated.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a View with Aggregate Functions
&lt;/h2&gt;

&lt;p&gt;This section shows how to create a view using the aggregate function. We can also use a query that has the aggregate functions to create a view. The syntax is the same that we used to create a regular view.&lt;/p&gt;

&lt;p&gt;For example, we want to create a view that contains a list of patients and the total number of prescribed medications. For that, we will use count() and group BY() functions. You can read &lt;a href="https://learn.microsoft.com/en-us/sql/t-sql/functions/aggregate-functions-transact-sql?view=sql-server-ver16"&gt;Aggregate Functions (Transact-SQL) - SQL Server&lt;/a&gt; article to learn more about the aggregate functions and their usage in SQL Server.&lt;br&gt;
The query to view a list of patients with medication is following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,Sum(mh.Prescription)TotalMedications from [Patients] p INNER join  
[MedicalHistory] mh ON p.PatientID=mh.PatientID 
WHERE  
  (FirstName is not null and lastname is not null) 
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'') 
and (Diagnosis is not null and Diagnosis&amp;lt;&amp;gt;'')
group by p.FirstName ,p.LastName,p.Gender,p.PhoneNumber

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;You can encapsulate this query in vw_patient_medication by running the following statement.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;create view vw_patient_medication  
as 
SELECT p.FirstName ,p.LastName,p.Gender,p.PhoneNumber,count(mh.Prescription)TotalMedications from [Patients] p INNER join  
[MedicalHistory] mh ON p.PatientID=mh.PatientID 
WHERE  
  (FirstName is not null and lastname is not null) 
and  (FirstName &amp;lt;&amp;gt;'' and lastname &amp;lt;&amp;gt;'') 
and (Diagnosis is not null and Diagnosis&amp;lt;&amp;gt;'')
group by p.FirstName ,p.LastName,p.Gender,p.PhoneNumber

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Run the following query to view the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;use HospitalManagementSystem
go
select * from vw_patient_medication order by TotalMedications desc

&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffi59uwzgjc3t1d1khaj9.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ffi59uwzgjc3t1d1khaj9.jpg" alt="Image description" width="800" height="472"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us see, how we can create a view using SQL Server Management Studio.&lt;/p&gt;

&lt;h2&gt;
  
  
  Creating a View Using SSMS
&lt;/h2&gt;

&lt;p&gt;We can also create a view using SQL Server Management Studio. For example, we want to create view that shows list of patients with their appointment dates with the respective doctors. We will use patient, doctors, and appointment tables.&lt;/p&gt;

&lt;p&gt;To create a view, connect to SQL Server instance. → Expand HospitalManagementSystem database → Right-click on Views → Select New View.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficxsavvxewbk5wiksnj7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ficxsavvxewbk5wiksnj7.jpg" alt="Image description" width="496" height="566"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A query designer opens which shows the list of tables created in the database. Select the desired tables and click Add.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxir2gsgu0af58tgjr8t.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbxir2gsgu0af58tgjr8t.jpg" alt="Image description" width="800" height="459"&gt;&lt;/a&gt;&lt;br&gt;
In a query designer, you can select the desired columns. As shown in below screenshot, the query designer automatically creates the query based on the selected columns.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnubavhusfgw6hvppzsf5.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fnubavhusfgw6hvppzsf5.jpg" alt="Image description" width="800" height="407"&gt;&lt;/a&gt;&lt;br&gt;
You can also make changes in auto-generated queries. Once the desired query is ready, click on the Save button which is in the menu bar. The SQL Server management studio prompts to provide the name of the view. Specify vw_patient_appointment as a name of the view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjdch4trvi9e51jn9lxv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fwjdch4trvi9e51jn9lxv.jpg" alt="Image description" width="800" height="481"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the view is created, Execute following T-SQL query to verify the data.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;select * from vw_patient_appointments
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Query output&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6rqfqcsj75u6bujbtbd.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/cdn-cgi/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fs6rqfqcsj75u6bujbtbd.jpg" alt="Image description" width="800" height="497"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  Dropping a View in SQL Server
&lt;/h2&gt;

&lt;p&gt;We can use the DROP VIEW statement to drop any view. When we drop a view from the database, make sure that the same view is not referenced by any other stored procedure or function. If it is being referenced by any other database object, the application code will break. Note that, the ALTER or CONTROL permission is needed on the view that you are dropping. The T-SQL query is following:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Drop view [view_name]
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above syntax view_name represents the name of the view that you want to drop. If the view exists in a specific schema, you must specify the schema name. The naming will be [Schema_name].[view_name]. &lt;/p&gt;

&lt;p&gt;Suppose, we want to delete a view named vw_patient_medication from the database. The view exists on the dbo schema, hence we do not need to specify the schema name in DROP VIEW statement. To drop the view, execute following T-SQL query:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;drop view vw_patient_medication
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  Conclusion
&lt;/h2&gt;

&lt;p&gt;In this article, we learned about the basics of SQL Server views and different types of views.  The article covers following topics with syntax and various examples.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Basics of view and its usage.&lt;/li&gt;
&lt;li&gt;How to create, update, and drop the view using T-SQL query.&lt;/li&gt;
&lt;li&gt;How to create the view using SQL Server Management Studio.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Creating a SQL Server view using SQL Server management studio is fairly simple for the developers who has limited knowledge. But if you are a seasoned developer and creating a view that has very complex SQL Queries, you might consider using the &lt;a href="https://www.devart.com/dbforge/sql/studio/?utm_source=article&amp;amp;utm_medium=referral&amp;amp;utm_content=creating+a+view+in+sql+server"&gt;dbForge Studio for SQL Server&lt;/a&gt; to create views. The dbForge Studio has lot of features that helps programmers to develop the database code very easily. It has a powerful feature named auto-complete. It helps to write complex SQL queries that are used to create views.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>Defragment Your Indexes for Better Database Performance</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Mon, 28 Nov 2022 18:57:58 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/defragment-your-indexes-for-better-database-performance-7nf</link>
      <guid>https://dev.to/nisargupadhyay87/defragment-your-indexes-for-better-database-performance-7nf</guid>
      <description>&lt;p&gt;Index fragmentation occurs when the physical order of the index pages does not match the logical ordering of the index. When we insert, update or delete any data from the SQL Server table, the SQL Server automatically updates the table's indexes. &lt;/p&gt;

&lt;p&gt;For example, when inserting records in a table, a page split might occur to accommodate the data in 8K pages of index, and eventually, the data becomes scattered. Suppose we have a table with 1500 pages that are 100% full. But due to various delete and update operations, now the table has 2000 pages, which are 60% full. &lt;/p&gt;

&lt;p&gt;Due to that, when we access data, instead of reading data from 1500 pages, the SQL has to read 2000 pages. Also, the pages are scattered, so SQL Server has to perform several random IO operations. Note that the performance of the storage subsystem is always better when a sequential IO is performed in the data retrieval process. Due to index fragmentation, random I/O is performed, which adds overhead to storage.&lt;/p&gt;

&lt;p&gt;The index fragmentation can be fixed by using the following methods&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Index Reorganize&lt;/li&gt;
&lt;li&gt;Index Rebuild&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  What is Index Rebuild
&lt;/h2&gt;

&lt;p&gt;The index rebuild operation drops and recreates an index. The index rebuild blocks access to the table whose index we are rebuilding; therefore, the table will be inaccessible, and the performance will be degraded. We can rebuild the index without blocking access using the ONLINE = ON option, which is only supported in the SQL Server enterprise edition. Index rebuild creates new statistics, and the changes will be recorded in the Transaction Log file; therefore, you must keep track of the transaction log’s growth while rebuilding a large index.&lt;/p&gt;

&lt;p&gt;To gain the optimum performance, the index should be rebuilt when the index fragmentation is higher than 30% to gain optimum performance.&lt;/p&gt;

&lt;h2&gt;
  
  
  What is index reorganize
&lt;/h2&gt;

&lt;p&gt;The index reorganize is a lightweight index maintenance operation that reorders the physical pages according to the logical ordering of the indexes. The reorganize operation does not block access to the table. The index reorganize operation never drops the index, so the statistics will not be updated.&lt;/p&gt;

&lt;p&gt;To gain the optimum performance, the index reorganize should be performed when the index fragmentation is between 10% and 30%.&lt;/p&gt;

&lt;p&gt;We can use the following methods to defrag the indexes of the SQL Server.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;ALTER INDEX T-SQL statement.&lt;/li&gt;
&lt;li&gt;
&lt;p&gt;Using SQL Server management studio.&lt;/p&gt;
&lt;h2&gt;
  
  
  Devart Index Manager
&lt;/h2&gt;

&lt;p&gt;In this article, we will learn how to reduce the fragmentation of indexes using the Devart &lt;a href="https://www.devart.com/dbforge/sql/index-manager/?utm_source=guest+article&amp;amp;utm_medium=referral&amp;amp;utm_content=defragment+your+indexes+for+better+database+performance" rel="noopener noreferrer"&gt;SQL Index manager&lt;/a&gt;. The Devart index manager is a part of the Devart dbForge SQL tool professional. The Devart Index manager provides the following benefits&lt;/p&gt;
&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is a central place where you can see a database list of all fragmented indexes. You can connect to the different database servers using the connection manager and populate the list of fragmented indexes from all databases.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can rebuild all or specific indexes using a user-friendly interface and easy to use. Complex scripting is not required.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can generate a CSV report of fragmented indexes and use it for review.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;You can generate a SQL script to rebuild or reorganize fragmented indexes. You can use those scripts to automate index maintenance.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;It is a SQL Server management studio plugin, so you do not have to maintain the additional software.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  How to use SQL Index Manager
&lt;/h2&gt;

&lt;p&gt;Open &lt;strong&gt;SQL Server Management Studio&lt;/strong&gt; → Connect to &lt;strong&gt;SQL Server database engine&lt;/strong&gt; → Expand &lt;strong&gt;Databases *&lt;em&gt;→ Right-click on **WideWorldImportors *&lt;/em&gt;→ Hover **Index Manager&lt;/strong&gt; → Select &lt;strong&gt;Manage Index Fragmentation.&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3fsyas540rag0xcapjh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fo3fsyas540rag0xcapjh.jpg" alt="Image description" width="665" height="670"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Devart Index Manager opens in a new SQL Server Management Studio tab. It is divided into three ribbons. The first ribbon contains the following configuration parameters.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Search Textbox:&lt;/strong&gt; If the database has a lot of indexes, you can enter the keyword of the tables or index name. The tool will populate the index's name containing the specified keyword.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Reanalyze:&lt;/strong&gt; This button scans the entire database and populates the list of all indexes.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Options:&lt;/strong&gt; Here, you can configure the following index rebuild options.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Reorganize threshold:&lt;/strong&gt; Specify the index reorganize threshold. The index will be reorganized if the fragmentation is higher than the specified value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rebuild threshold:&lt;/strong&gt; Specify the index rebuild threshold. The index will be rebuilt if the fragmentation exceeds the specified value.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Sort in tempdb:&lt;/strong&gt; If you enable the Sort in tempdb option, the index rebuild operation will be performed in SQL Server temporary database (TempDB) &lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Online:&lt;/strong&gt; When enabling this option, the index rebuild operation will be performed without locking the table. This feature is only supported in the SQL Server enterprise edition.&lt;/li&gt;
&lt;/ol&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Database Name:&lt;/strong&gt; The list of databases created in a server.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Server Name:&lt;/strong&gt; The list of SQL Server instance names.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The third section is a grid view which contains the following details:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Index Name:&lt;/strong&gt; The list of fragmented Indexes above the specified threshold.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fix Type:&lt;/strong&gt; The fix type is an action suggested by the Devart Index Manager. The fix type is Index Reorganize or Index Rebuild. The fix type is also determined based on the threshold specified in the option.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Owner Object:&lt;/strong&gt; The list of tables and schema whose indexes are fragmented. A table contains multiple indexes, but you can only view those with fragmentation higher than the specified threshold.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Type:&lt;/strong&gt; Type of Index. It can be clustered index or a non-clustered index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Size (Pages):&lt;/strong&gt; Size of fragmented indexes. The size is 8kb pages.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Index Size (MB):&lt;/strong&gt; Size of fragmented indexes. The size is in MB.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Fragmentation:&lt;/strong&gt; Percentage fragmentation in listed indexes.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Partition number:&lt;/strong&gt; Partition number of the table.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Rows in Partition:&lt;/strong&gt; Total records in a partition.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Reason:&lt;/strong&gt; Reason for index maintenance (Index rebuild to index reorganize).&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The following screenshot shows the list of fragmented indexes in the &lt;strong&gt;WideWorldImportors&lt;/strong&gt; database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmpnxq69fh2uftocpu62.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Ftmpnxq69fh2uftocpu62.jpg" alt="Image description" width="698" height="357"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In this demo, we want to reorganize the indexes of the WideworldImportors database when fragmentation is higher than 10%, rebuild the indexes when fragmentation is higher than 25%, and the maximum index size must be higher than 1000 Pages.&lt;br&gt;
To do that, we will change the index rebuild, reorganize the threshold, and specify the minimum index size. &lt;/p&gt;

&lt;p&gt;To do that, Click on the Options button.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1p0xl9vrk8t0fbegl0d.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fg1p0xl9vrk8t0fbegl0d.jpg" alt="Image description" width="698" height="34"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A dialog box opens. Enter 10% in Reorganize threshold, 25% in the Rebuild threshold, and 1000 in the Minimum index size textbox. Enable the Sort in TempDB option. Click OK to save the changes.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7loulisg92su4skohpfh.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F7loulisg92su4skohpfh.jpg" alt="Image description" width="402" height="332"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, we will rescan all tables and populate the list of the fragmented index. Click on Analyze.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgot2x63c82b8m6y212k7.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fgot2x63c82b8m6y212k7.jpg" alt="Image description" width="698" height="34"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Devart index manager will populate the list of fragmented indexes which matches the above criteria. Following is the list in a grid view.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7o0vbsma8w97wih7obv.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj7o0vbsma8w97wih7obv.jpg" alt="Image description" width="698" height="302"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Before we rebuild the indexes, you can see three options in a ribbon that will be enabled.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Fix:&lt;/strong&gt; When we click on Fix, the Index Manager will automatically rebuild/reorganize the index.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Script Changes:&lt;/strong&gt; When we click on Script Changes, the index manager will generate an ALTER INDEX REBUILD script. This option gives us the flexibility to change the index rebuild parameters.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the following screenshot:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4fkmxll5m5ptmm6aqkk.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fh4fkmxll5m5ptmm6aqkk.jpg" alt="Image description" width="698" height="189"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will script the changes in this demo, so I selected all indexes and clicked the Script Changes button. See the following image:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzz2el1pp0nj4z0hh1fi.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fyzz2el1pp0nj4z0hh1fi.jpg" alt="Image description" width="698" height="108"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A SQL Script to rebuild the indexes has been created.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjn9n7md3ffbhflsrf2m.jpg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsjn9n7md3ffbhflsrf2m.jpg" alt="Image description" width="698" height="319"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We can execute the script immediately, or we can create a SQL job to execute it during off-business hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;This article explains the SQL Server indexes, how they get fragmented, and how to defrag them using the Devart dbForge Index manager.&lt;/p&gt;

</description>
      <category>tutorial</category>
      <category>productivity</category>
      <category>discuss</category>
    </item>
    <item>
      <title>Different ways to export SQL Database using Devart Data Pump</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Thu, 23 Jun 2022 08:09:22 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/different-ways-to-export-sql-database-using-devart-data-pump-59nc</link>
      <guid>https://dev.to/nisargupadhyay87/different-ways-to-export-sql-database-using-devart-data-pump-59nc</guid>
      <description>&lt;p&gt;The Devart Data Pump is a SQL Server Management Studio add-in that helps us migrate data between SQL Server databases to various data formats. The Devart Data Pump supports import and export of Microsoft Excel, Microsoft Access, CSV, Text, JSON, PDF, HTML, ODBC, etc. The Devart Data Pump is highly customizable, so the data export process becomes much easier.&lt;/p&gt;

&lt;p&gt;In this article, we will learn the step-by-step process of exporting data from the Azure SQL database to an on-premises SQL database server with the help of efficient SQL tools. We are using the ODBC data source to connect to the on-prem server. &lt;/p&gt;

&lt;h2&gt;
  
  
  Environment Setup
&lt;/h2&gt;

&lt;p&gt;I have configured Azure SQL Server and configured a sample database named AdventureWorksLT database. The details of the Azure SQL Server details are as follows:&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv95hu26w69xz7pakcx5s.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fv95hu26w69xz7pakcx5s.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;We will export the records of the customer table, which is created in the AdventureWorksLT database.&lt;br&gt;
I have installed SQL Server 2019 on my workstation and created a database named DemoDatabase. I have scripted out  the table definition of the customer table from the AdventureWorksLT database and re-created it on DemoDatabase. &lt;br&gt;
The script to create the table is as follows:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE  [Customer]
(
    [CustomerID] [int] IDENTITY(1,1) NOT NULL,
    [NameStyle] BIT NOT NULL,
    [Title] [nvarchar](8) NULL,
    [FirstName] VARCHAR(100) NOT NULL,
    [MiddleName] VARCHAR(100) NULL,
    [LastName] VARCHAR(100) NOT NULL,
    [Suffix] [nvarchar](10) NULL,
    [CompanyName] [nvarchar](128) NULL,
    [SalesPerson] [nvarchar](256) NULL,
    [EmailAddress] [nvarchar](50) NULL,
    [Phone] NVARCHAR(25) NULL,
    [PasswordHash] [varchar](128) NOT NULL,
    [PasswordSalt] [varchar](10) NOT NULL,
    [rowguid] [uniqueidentifier] NOT NULL,
    [ModifiedDate] [datetime] NOT NULL) ON [PRIMARY]
GO
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now, to copy data to DemoDatabase, we must first create an ODBC data source.&lt;/p&gt;

&lt;h2&gt;
  
  
  Configure ODBC Driver for SQL Server.
&lt;/h2&gt;

&lt;p&gt;First, let us configure an ODBC data source. Open ODBC Data Sources 🡪  Click Add on User DSN tab.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho8hpl0ds4evg8c0lg9m.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fho8hpl0ds4evg8c0lg9m.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Create New Data Source on screen select SQL Server Native Client 11.0 🡪 Click Finish  &lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wq25fi8u9lnmy0cjjgw.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F2wq25fi8u9lnmy0cjjgw.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The Create ODBC driver for SQL Server configuration wizard opens. On the first screen, specify the desired ODBC driver name. Specify the server hostname on which SQL Server is installed in the Server Name box and click Next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqfvcw9fu1ioqqmxw2lm.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fzqfvcw9fu1ioqqmxw2lm.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the next screen, specify the authentication method used to connect to the SQL Server. We are using Windows Authentication.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1bodwvpukbeacidmzzo.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fj1bodwvpukbeacidmzzo.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the next screen, we can configure the following parameters.&lt;br&gt;
Set the default database. In our demo, I set the default database to DemoDatabase.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Specify the SPN for mirroring server&lt;/li&gt;
&lt;li&gt;Enable ANSI quoted identifier, ANSI nulls, paddings, and warnings.&lt;/li&gt;
&lt;li&gt;Application Intent. It can be READWRITE or READONLY.&lt;/li&gt;
&lt;li&gt;Enable multi-subnet failover.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In our demo, I have not made any changes. Click Next.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jmn62j1fscbxk5l9154.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4jmn62j1fscbxk5l9154.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;On the wizard's next screen, you can specify the following options.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Change the SQL Server system message language.&lt;/li&gt;
&lt;li&gt;Enable strong encryption&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Enable the regional settings for currency, number, date, and time.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Specify the location of the log of long-running queries and query     timeout&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Specify the ODBC driver statistics log file.&lt;br&gt;
In our demo, I have not made any changes. Click Finish.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqm0vkozupfqcx9i367mb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fqm0vkozupfqcx9i367mb.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;A popup will be displayed. In the popup, you can see all the configuration parameters and the value set by us. &lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i5qwh70jbxnw9dhxhwk.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5i5qwh70jbxnw9dhxhwk.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Click OK to create the data source.&lt;/p&gt;

&lt;p&gt;Once the data source has been created successfully, you can view it in the User DSN tab of the ODBC Data Source Administrator.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdebgtcly321m8keqv3w6.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fdebgtcly321m8keqv3w6.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, let us configure the Devart Data Pump to export data from Azure SQL Database to On-prem SQL Server.&lt;/p&gt;
&lt;h2&gt;
  
  
  Configure Devart Data Pump
&lt;/h2&gt;

&lt;p&gt;The &lt;strong&gt;Devart Data Pump&lt;/strong&gt; SQL tool is an efficient &lt;a href="https://www.devart.com/dbforge/sql/data-pump/" rel="noopener noreferrer"&gt;SQL server import and export wizard&lt;/a&gt;. It can be launched from &lt;strong&gt;SQL Server Management Studio&lt;/strong&gt;. I am exporting data from AdventureWorksLT (Azure SQL Database) to DemoDatabase (on-prem SQL Server database). Open &lt;strong&gt;SQL Server Management Studio&lt;/strong&gt; 🡪 Connect to &lt;strong&gt;Azure SQL Server instance&lt;/strong&gt; 🡪 Expand &lt;strong&gt;Databases&lt;/strong&gt; 🡪 Right-click the &lt;strong&gt;AdventureWorksLT&lt;/strong&gt; database 🡪 Hover on Data Pump 🡪 Select &lt;strong&gt;Export Data&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb5t8iylj9p1blcefpdl.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fbb5t8iylj9p1blcefpdl.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The Data Export wizard starts. On the first screen, you can see the various export formats supported by the Devart Data Pump. I am exporting the data in ODBC format, so I have selected ODBC.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fpinbvs1tuuml8otdli.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F5fpinbvs1tuuml8otdli.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We need to specify the &lt;strong&gt;source&lt;/strong&gt; database and schema on the Source screen. We are exporting data from the customer table created in the &lt;strong&gt;AdventureWorksLT&lt;/strong&gt; database. Select &lt;strong&gt;AdventureWorksLT&lt;/strong&gt; from the Databases drop-down box and select &lt;strong&gt;dbo&lt;/strong&gt; from the Schema drop-down box. The list of tables created in the &lt;strong&gt;dbo&lt;/strong&gt; schema will be loaded in the grid view. We are exporting data from the customer table; therefore, select it from the grid and click &lt;strong&gt;Next&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrwhvf4t0xa1t5janpha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fvrwhvf4t0xa1t5janpha.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We must set the specification of the ODBC data providers from the &lt;strong&gt;Options&lt;/strong&gt; screen. In the data source specification section, we must specify the ODBC connection string or select the pre-configured ODBC system or user data source. We have already configured an ODBC data source named &lt;strong&gt;AdventureWorksDB&lt;/strong&gt;, so select it from the &lt;strong&gt;Use system to the user data source name&lt;/strong&gt; drop-down box. Click &lt;strong&gt;Next&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4liefnw7iwsbxegs1yja.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4liefnw7iwsbxegs1yja.png" alt="Image description"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;You can select the table where you want to export the data from the table. In our demo, we have created a customer table in DemoDatabase; therefore, choose &lt;strong&gt;demodatabase.dbo.customer&lt;/strong&gt; from the list.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7ba29albh71p4aw4t2x.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fw7ba29albh71p4aw4t2x.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Suppose you want to export data from specific columns of a table. To do that, you can select those columns from the &lt;strong&gt;Data Format&lt;/strong&gt; screen. This option is useful when exporting data from specific columns to &lt;strong&gt;Excel&lt;/strong&gt; or &lt;strong&gt;CSV&lt;/strong&gt; files. In this demo, we are exporting all columns. Click &lt;strong&gt;Next&lt;/strong&gt;.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5n5qm9504kfroce8kxb.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr5n5qm9504kfroce8kxb.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We can export all rows of tables or specific rows of tables from the &lt;strong&gt;Exported rows&lt;/strong&gt; screen. In this demo, I am exporting all records from the customer table; therefore, I have selected the &lt;strong&gt;Export all rows&lt;/strong&gt; option.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifikegaipm9z5rfmhm6i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fifikegaipm9z5rfmhm6i.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
We can configure the error handling process and behavior on the Error Handling screen. In this demo, I am not changing any parameters. Click &lt;strong&gt;Export&lt;/strong&gt; to begin the data export between the Azure SQL database to the In-Prem SQL Server database.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr1wquuvbo2g38bx9sgxg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fr1wquuvbo2g38bx9sgxg.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The data export process begins.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8zztpa8ohgvs15gcoti7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F8zztpa8ohgvs15gcoti7.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
The data has been exported successfully.&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioqf1qpjts06lr70h3dx.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fioqf1qpjts06lr70h3dx.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
Run the SELECT statement to verify that data has been copied successfully.&lt;br&gt;
&lt;strong&gt;SQL Query&lt;/strong&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;USE demodatabase
GO
SELECT c.CustomerID,c.Title,c.FirstName+' '+c.MiddleName +' '+c.LastName,c.CompanyName,c.SalesPerson,
c.EmailAddress,c.Phone,c.ModifiedDate FROM Customer c
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;strong&gt;Query output&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qk3p0qfpz235gtdpmwq.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F4qk3p0qfpz235gtdpmwq.png" alt="Image description"&gt;&lt;/a&gt;&lt;br&gt;
As you can see, all records have been transferred.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;In this article, we have explored several ways of using Devart Data Pump for exporting SQL databases. In my next article, we will learn how to transfer data from the Azure SQL database to Microsoft Excel files and other data formats using the Devart Data Pump.&lt;/p&gt;

</description>
    </item>
    <item>
      <title>An overview of dbForge Schema Compare</title>
      <dc:creator>nisargupadhyay87</dc:creator>
      <pubDate>Fri, 13 May 2022 17:10:30 +0000</pubDate>
      <link>https://dev.to/nisargupadhyay87/an-overview-of-dbforge-schema-compare-56nk</link>
      <guid>https://dev.to/nisargupadhyay87/an-overview-of-dbforge-schema-compare-56nk</guid>
      <description>&lt;p&gt;The dbForge Schema compare tool is a SQL tool that is used to compare the schema of two databases that exists on one or multiple instances of the SQL Server. &lt;br&gt;
Recently, one of my company's clients was facing an issue in their application that accessed the archived database. After troubleshooting the issue, we came to know that the archived database was out of sync with the production database. The schema was out of sync because the execution of a few DDL scripts failed, and the schema remained unchanged. We fixed the issue by writing a stored procedure that compares the schema between production and archived databases and provides the list of missing objects.&lt;br&gt;
Later, we learned about the dbForge Schema Compare tool that can perform the comparison of two SQL databases very easily. It provides the following benefits&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Compare the source and destination database and objects. &lt;/li&gt;
&lt;li&gt;Generate the scripts that can be used to create an object missing  in the source/target database.&lt;/li&gt;
&lt;li&gt;Generate the report which provides the details of the missing objects.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;We can compare the following types of objects in the dbForge Schema Compare tool.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;SQL Server database:&lt;/strong&gt; This option is used to compare the database objects of the source and destination database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Backup of SQL Server database:&lt;/strong&gt; This option is used to compare the backup of the SQL Server database.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Script Folder:&lt;/strong&gt; This option compares the source and destination folders containing the script to create database objects.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Source control repository:&lt;/strong&gt; This option compares two source control repositories that contain the T-SQL database scripts.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;In this article, we will learn to compare the objects of two databases. To demonstrate, I have installed SQL Server 2019 and restored two databases named &lt;strong&gt;&lt;em&gt;statckoverflow2010&lt;/em&gt;&lt;/strong&gt; and &lt;strong&gt;&lt;em&gt;Stackoverflow2010_old&lt;/em&gt;&lt;/strong&gt;. The source database is &lt;strong&gt;stackoverflow2010&lt;/strong&gt;, and the target database is &lt;strong&gt;stackoverflow2010_old&lt;/strong&gt;. We will find the list of objects that are changed/missing in the target database (Stackoverflow2010_old).&lt;br&gt;
First, click on New schema compare located below the menu bar.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--E4lz4Joz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izgscvp30ay93yfn24w7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--E4lz4Joz--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/izgscvp30ay93yfn24w7.png" alt="Image description" width="880" height="594"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The New schema comparison wizard starts. You can configure the following options:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Source and target databases which you want to compare.&lt;/li&gt;
&lt;li&gt;Set advance option to customize &lt;a href="https://www.devart.com/dbforge/sql/schemacompare/"&gt;sql compare&lt;/a&gt; process.
Schema and table mapping.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Configure the source and target database.
&lt;/h2&gt;

&lt;p&gt;First, let us configure the source and destination database. In our case, the source database is Stackoverflow2010, and the target database is stackoverflow2010_old.&lt;br&gt;
We compare the databases; therefore, select &lt;strong&gt;Database&lt;/strong&gt; from the Type drop-down box. In the Connection drop-down box, you can select the existing connection or create a SQL Server connection on which the source and destination databases are created. We are creating a new connection; therefore, select &lt;strong&gt;Manage&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--M_sT4eBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82cd787ny384y0n3vpwn.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--M_sT4eBm--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/82cd787ny384y0n3vpwn.png" alt="Image description" width="740" height="503"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Another dialog box named opens. Here you can see the various parameters and configuration options to create a SQL Server database connection. Click on New to create a &lt;strong&gt;New&lt;/strong&gt; connection. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--sOD7htSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikzpv9csyhpk8f2v9y79.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--sOD7htSb--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ikzpv9csyhpk8f2v9y79.png" alt="Image description" width="554" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In our case, the configuration parameters are specified as follows.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server Name:&lt;/strong&gt; Specify the server's hostname on which the SQL Server is installed. In our case, the hostname is Nisarg-PC.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication:&lt;/strong&gt; select the authentication type. You can use windows authentication or SQL Server authentication. In our demo, we are using Windows authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database:&lt;/strong&gt; select the source database. In this demo, the source database is stackoverflow2010.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection name:&lt;/strong&gt; Enter the desired connection name. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click Connect. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--_mwGtYX4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tfb6m42nvl05bvkvppz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--_mwGtYX4--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/3tfb6m42nvl05bvkvppz.png" alt="Image description" width="436" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Back to the connection manager dialog box, you can see the new connection has been created. Following is the screenshot of the Connection Manager dialog box.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--OaX1KmVL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elgv7kzfgeoee25v2e32.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--OaX1KmVL--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/elgv7kzfgeoee25v2e32.png" alt="Image description" width="554" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Now, similarly, configure the Target connection. In this demo, the target database is Stackoverflow2010_old on the same SQL Server instance. Therefore, we will change the database name in the Target SQL Server connection Manager. The parameters in the dialog box named Database Connection Properties are following:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Server Name:&lt;/strong&gt; Specify the hostname of the server on which the SQL Server is installed. In our case, the hostname is Nisarg-PC.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Authentication:&lt;/strong&gt; select the authentication type. You can use windows authentication or SQL Server authentication. In our demo, we are using Windows authentication.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Database:&lt;/strong&gt; select the source database. In this demo, the target database is stackoverflow2010_old.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Connection name:&lt;/strong&gt; Enter the desired connection name. &lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Click Connect. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QXSwL0yT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqx7076y0j00qc7f32c4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QXSwL0yT--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lqx7076y0j00qc7f32c4.png" alt="Image description" width="436" height="487"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In the Connection Manager dialog box, you can see the new connection has been created. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--QOzx8ppD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8z99ptp6zre1keyf271.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--QOzx8ppD--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t8z99ptp6zre1keyf271.png" alt="Image description" width="554" height="391"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The source and target screen look like the following image. Click Next to configure the options.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--PVaHy7Xi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w7u3icomhgmkm7036yf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--PVaHy7Xi--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9w7u3icomhgmkm7036yf.png" alt="Image description" width="860" height="645"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Schema Mapping&lt;/strong&gt;&lt;br&gt;
You can set the correspondence between the source and target schema owners in the schema mapping screen. Suppose you are comparing the two databases. The source database has two schemas that the target database doesn't have. The stackoverflow2010 database has two schemas in this demo, but the stackoverflow2010_old database does not have any schema. The wizard automatically sets  in the target schema grid-view on the schema mapping screen.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--6KpTudeW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z1cepp9sjn4h35icmnt.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--6KpTudeW--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9z1cepp9sjn4h35icmnt.png" alt="Image description" width="860" height="645"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Table Mapping&lt;/strong&gt;&lt;br&gt;
You can choose the list of the database objects you want to compare on the table mapping screen. Also, you can map columns to compare the data type. We will learn more about it in our next article. If you want to exclude any objects from the mapping list, right-click on the object name and click on &lt;strong&gt;Unmap&lt;/strong&gt;.&lt;br&gt;
In our demo, we are comparing all objects.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--ntLLf92P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fihn9czspb87197hcvv4.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--ntLLf92P--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/fihn9czspb87197hcvv4.png" alt="Image description" width="860" height="645"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Begin schema compare process&lt;/strong&gt;&lt;br&gt;
Once mappings are completed, click on Compare to begin the database comparison process. If the Database is large and contains many objects, the process takes a while to complete. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--CHVtZvBK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jayoj1vhbs3ciqt60lg7.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--CHVtZvBK--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jayoj1vhbs3ciqt60lg7.png" alt="Image description" width="416" height="346"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Once the comparison completes, you can see the object difference shown in the following image. &lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--m8yFji7O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrnrw32bq86xk6fotecz.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--m8yFji7O--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lrnrw32bq86xk6fotecz.png" alt="Image description" width="880" height="549"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;The comparison project provides the following details&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Objects that exist in the only source database.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Objects that exist in both databases but have different values of parameters.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Objects that exist in the target database.&lt;br&gt;
The comparison project shows the T-SQL queries which can be used to synchronize/change the source and destination database.&lt;br&gt;
In our demo, the details provided by the schema comparison is following:&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Only in the Source database&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
The Stackoverflow2010 database contains two schemas named &lt;strong&gt;UserPosts&lt;/strong&gt; and &lt;strong&gt;Users&lt;/strong&gt;, which do not exist in the &lt;strong&gt;&lt;em&gt;Stackoverflow2010_old&lt;/em&gt;&lt;/strong&gt; database. The comparison project also generates the &lt;strong&gt;CREATE SCHEMA&lt;/strong&gt; statement. See the following image:&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--2Da76kyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkxh6n5kzcw53w6o0prf.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--2Da76kyC--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkxh6n5kzcw53w6o0prf.png" alt="Image description" width="880" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Different in source and destination database&lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
There are two differences found in the source and destination databases.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The database recovery model.

&lt;ul&gt;
&lt;li&gt;Stackoverflow2010: SIMPLE recovery model.&lt;/li&gt;
&lt;li&gt;Stackoverflow2010_old: FULL recovery model.&lt;/li&gt;
&lt;/ul&gt;


&lt;/li&gt;
&lt;li&gt;An additional column named description is in the comment table.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;See the following images for reference:&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Image 1: Database recovery model &lt;/u&gt;&lt;/strong&gt;&lt;br&gt;
&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--fvE-JVRs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa40lcx1t040vxk9o17i.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--fvE-JVRs--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sa40lcx1t040vxk9o17i.png" alt="Image description" width="880" height="445"&gt;&lt;/a&gt;&lt;br&gt;
&lt;strong&gt;&lt;u&gt;Image 2: Column differences in the comment table&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--iUucduae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph3niicul4pabg2g0vkd.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--iUucduae--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ph3niicul4pabg2g0vkd.png" alt="Image description" width="880" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;&lt;u&gt;Only in the Target database&lt;/u&gt;&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The Stackoverflow2010_old database contains the tblArchivedPosts table that does not exist in the stackoverflow2010 Database. The comparison project also generates the CREATE TABLE statement that might be used to create a table in the source database.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--A9BvknHg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dcvhhml31wmhho7yfss.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--A9BvknHg--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1dcvhhml31wmhho7yfss.png" alt="Image description" width="880" height="445"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;As you can see, the schema compare tool has provided detailed information on the differences between the source and target database.&lt;/p&gt;

&lt;h2&gt;
  
  
  Summary
&lt;/h2&gt;

&lt;p&gt;The above article explains the process of comparing the SQL databases using dbForge schema compare. In my upcoming articles, we will review the process to compare the backups of the SQL databases. &lt;/p&gt;

</description>
      <category>database</category>
      <category>sqlserver</category>
      <category>productivity</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
